Copied remotely
[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
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.IO;
31 using System.Text;
32
33 namespace System.Web.Mail {
34
35     // a class that handles UU encoding for attachments
36     internal class UUAttachmentEncoder : IAttachmentEncoder {
37         
38         protected byte[] beginTag;
39         protected byte[] endTag;
40         protected byte[] endl;
41         
42         public UUAttachmentEncoder( int mode , string fileName ) {
43             string endlstr = "\r\n";
44             
45             beginTag = 
46                 Encoding.ASCII.GetBytes( "begin " + mode + " " + fileName + endlstr); 
47             
48             endTag = 
49                 Encoding.ASCII.GetBytes( "`" + endlstr + "end" + endlstr ); 
50             
51             endl = Encoding.ASCII.GetBytes( endlstr );
52         }
53         
54         // uu encodes a stream in to another stream
55         public void EncodeStream(  Stream ins , Stream outs ) {
56             
57             // write the start tag
58             outs.Write( beginTag , 0 , beginTag.Length );          
59             
60             // create the uu transfom and the buffers
61             ToUUEncodingTransform tr = new ToUUEncodingTransform();
62             byte[] input = new byte[ tr.InputBlockSize ];
63             byte[] output = new byte[ tr.OutputBlockSize ];
64             
65             while( true ) {
66                         
67                 // read from the stream until no more data is available
68                 int check = ins.Read( input , 0 , input.Length );
69                 if( check < 1 ) break;
70                 
71                 // if the read length is not InputBlockSize
72                 // write a the final block
73                 if( check == tr.InputBlockSize ) {
74                     tr.TransformBlock( input , 0 , check , output , 0 );
75                     outs.Write( output , 0 , output.Length );
76                     outs.Write( endl , 0 , endl.Length );
77                 } else {
78                     byte[] finalBlock = tr.TransformFinalBlock( input , 0 , check );
79                     outs.Write( finalBlock , 0 , finalBlock.Length );
80                     outs.Write( endl , 0 , endl.Length );
81                     break;
82                 }
83                                 
84             }
85             
86             // write the end tag.
87             outs.Write( endTag , 0 , endTag.Length );
88         }
89         
90         
91         
92         
93         
94         
95     }
96     
97 }