2 // System.Web.Mail.UUAttachmentEncoder.cs
5 // Per Arneng <pt99par@student.bth.se>
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
33 namespace System.Web.Mail {
35 // a class that handles UU encoding for attachments
36 internal class UUAttachmentEncoder : IAttachmentEncoder {
38 protected byte[] beginTag;
39 protected byte[] endTag;
40 protected byte[] endl;
42 public UUAttachmentEncoder( int mode , string fileName ) {
43 string endlstr = "\r\n";
46 Encoding.ASCII.GetBytes( "begin " + mode + " " + fileName + endlstr);
49 Encoding.ASCII.GetBytes( "`" + endlstr + "end" + endlstr );
51 endl = Encoding.ASCII.GetBytes( endlstr );
54 // uu encodes a stream in to another stream
55 public void EncodeStream( Stream ins , Stream outs ) {
57 // write the start tag
58 outs.Write( beginTag , 0 , beginTag.Length );
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 ];
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;
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 );
78 byte[] finalBlock = tr.TransformFinalBlock( input , 0 , check );
79 outs.Write( finalBlock , 0 , finalBlock.Length );
80 outs.Write( endl , 0 , endl.Length );
87 outs.Write( endTag , 0 , endTag.Length );