Base64AttachmentEncoder.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // System.Web.Mail.Base64AttachmentEncoder.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Text;
  31. using System.Security.Cryptography;
  32. namespace System.Web.Mail {
  33. // a class that handles Base64 encoding for attachments
  34. internal class Base64AttachmentEncoder : IAttachmentEncoder {
  35. // reads bytes from a stream and writes the encoded
  36. // as base64 encoded characters. ( 60 chars on each row)
  37. public void EncodeStream( Stream ins , Stream outs ) {
  38. if( ( ins == null ) || ( outs == null ) )
  39. throw new ArgumentNullException( "The input and output streams may not " +
  40. "be null.");
  41. ICryptoTransform base64 = new ToBase64Transform();
  42. // the buffers
  43. byte[] plainText = new byte[ base64.InputBlockSize ];
  44. byte[] cipherText = new byte[ base64.OutputBlockSize ];
  45. int readLength = 0;
  46. int count = 0;
  47. byte[] newln = new byte[] { 13 , 10 }; //CR LF with mail
  48. // read through the stream until there
  49. // are no more bytes left
  50. while( true ) {
  51. // read some bytes
  52. readLength = ins.Read( plainText , 0 , plainText.Length );
  53. // break when there is no more data
  54. if( readLength < 1 ) break;
  55. // transfrom and write the blocks. If the block size
  56. // is less than the InputBlockSize then write the final block
  57. if( readLength == plainText.Length ) {
  58. base64.TransformBlock( plainText , 0 ,
  59. plainText.Length ,
  60. cipherText , 0 );
  61. // write the data
  62. outs.Write( cipherText , 0 , cipherText.Length );
  63. // do this to output lines that
  64. // are 60 chars long
  65. count += cipherText.Length;
  66. if( count == 60 ) {
  67. outs.Write( newln , 0 , newln.Length );
  68. count = 0;
  69. }
  70. } else {
  71. // convert the final blocks of bytes and write them
  72. cipherText = base64.TransformFinalBlock( plainText , 0 , readLength );
  73. outs.Write( cipherText , 0 , cipherText.Length );
  74. }
  75. }
  76. outs.Write( newln , 0 , newln.Length );
  77. }
  78. }
  79. }