Added comments to the code
[mono.git] / mcs / class / System.Web / System.Web.Mail / UUAttachmentEncoder.cs
1 //
2 // System.Web.Mail.UUAttachmentEncoder.cs
3 //
4 // Author(s):
5 //   Per Arneng <pt99par@student.bth.se>
6 //
7 //
8 using System;
9 using System.IO;
10 using System.Text;
11
12 namespace System.Web.Mail {
13
14     // a class that handles UU encoding for attachments
15     internal class UUAttachmentEncoder : IAttachmentEncoder {
16         
17         protected byte[] beginTag;
18         protected byte[] endTag;
19         protected byte[] endl;
20         
21         public UUAttachmentEncoder( int mode , string fileName ) {
22             string endlstr = "\r\n";
23             
24             beginTag = 
25                 Encoding.ASCII.GetBytes( "begin " + mode + " " + fileName + endlstr); 
26             
27             endTag = 
28                 Encoding.ASCII.GetBytes( "`" + endlstr + "end" + endlstr ); 
29             
30             endl = Encoding.ASCII.GetBytes( endlstr );
31         }
32         
33         // uu encodes a stream in to another stream
34         public void EncodeStream(  Stream ins , Stream outs ) {
35             
36             // write the start tag
37             outs.Write( beginTag , 0 , beginTag.Length );          
38             
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 ];
43             
44             while( true ) {
45                         
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;
49                 
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 );
56                 } else {
57                     byte[] finalBlock = tr.TransformFinalBlock( input , 0 , check );
58                     outs.Write( finalBlock , 0 , finalBlock.Length );
59                     outs.Write( endl , 0 , endl.Length );
60                     break;
61                 }
62                                 
63             }
64             
65             // write the end tag.
66             outs.Write( endTag , 0 , endTag.Length );
67         }
68         
69         
70         
71         
72         
73         
74     }
75     
76 }