UUAttachmentEncoder.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // System.Web.Mail.UUAttachmentEncoder.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. namespace System.Web.Mail {
  12. // a class that handles UU encoding for attachments
  13. internal class UUAttachmentEncoder : IAttachmentEncoder {
  14. protected byte[] beginTag;
  15. protected byte[] endTag;
  16. protected byte[] endl;
  17. public UUAttachmentEncoder( int mode , string fileName ) {
  18. string endlstr = "\r\n";
  19. beginTag =
  20. Encoding.ASCII.GetBytes( "begin " + mode + " " + fileName + endlstr);
  21. endTag =
  22. Encoding.ASCII.GetBytes( "`" + endlstr + "end" + endlstr );
  23. endl = Encoding.ASCII.GetBytes( endlstr );
  24. }
  25. // uu encodes a stream in to another stream
  26. public void EncodeStream( Stream ins , Stream outs ) {
  27. // write the start tag
  28. outs.Write( beginTag , 0 , beginTag.Length );
  29. // create the uu transfom and the buffers
  30. ToUUEncodingTransform tr = new ToUUEncodingTransform();
  31. byte[] input = new byte[ tr.InputBlockSize ];
  32. byte[] output = new byte[ tr.OutputBlockSize ];
  33. while( true ) {
  34. // read from the stream until no more data is available
  35. int check = ins.Read( input , 0 , input.Length );
  36. if( check < 1 ) break;
  37. // if the read length is not InputBlockSize
  38. // write a the final block
  39. if( check == tr.InputBlockSize ) {
  40. tr.TransformBlock( input , 0 , check , output , 0 );
  41. outs.Write( output , 0 , output.Length );
  42. outs.Write( endl , 0 , endl.Length );
  43. } else {
  44. byte[] finalBlock = tr.TransformFinalBlock( input , 0 , check );
  45. outs.Write( finalBlock , 0 , finalBlock.Length );
  46. outs.Write( endl , 0 , endl.Length );
  47. break;
  48. }
  49. }
  50. // write the end tag.
  51. outs.Write( endTag , 0 , endTag.Length );
  52. }
  53. }
  54. }