2 // System.Web.Mail.UUAttachmentEncoder.cs
5 // Per Arneng <pt99par@student.bth.se>
12 namespace System.Web.Mail {
14 // a class that handles UU encoding for attachments
15 internal class UUAttachmentEncoder : IAttachmentEncoder {
17 protected byte[] beginTag;
18 protected byte[] endTag;
19 protected byte[] endl;
21 public UUAttachmentEncoder( int mode , string fileName ) {
22 string endlstr = "\r\n";
25 Encoding.ASCII.GetBytes( "begin " + mode + " " + fileName + endlstr);
28 Encoding.ASCII.GetBytes( "`" + endlstr + "end" + endlstr );
30 endl = Encoding.ASCII.GetBytes( endlstr );
33 // uu encodes a stream in to another stream
34 public void EncodeStream( Stream ins , Stream outs ) {
36 // write the start tag
37 outs.Write( beginTag , 0 , beginTag.Length );
39 // create the uu transfom and the buffers
40 ToUUEncodingTransform tr = new ToUUEncodingTransform();
41 byte[] input = new byte[ tr.InputBlockSize ];
42 byte[] output = new byte[ tr.OutputBlockSize ];
46 // read from the stream until no more data is available
47 int check = ins.Read( input , 0 , input.Length );
48 if( check < 1 ) break;
50 // if the read length is not InputBlockSize
51 // write a the final block
52 if( check == tr.InputBlockSize ) {
53 tr.TransformBlock( input , 0 , check , output , 0 );
54 outs.Write( output , 0 , output.Length );
55 outs.Write( endl , 0 , endl.Length );
57 byte[] finalBlock = tr.TransformFinalBlock( input , 0 , check );
58 outs.Write( finalBlock , 0 , finalBlock.Length );
59 outs.Write( endl , 0 , endl.Length );
66 outs.Write( endTag , 0 , endTag.Length );