KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > util > UUEncoderStream


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)UUEncoderStream.java 1.4 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.util;
29
30 import java.io.*;
31
32 /**
33  * This class implements a UUEncoder. It is implemented as
34  * a FilterOutputStream, so one can just wrap this class around
35  * any output stream and write bytes into this filter. The Encoding
36  * is done as the bytes are written out.
37  *
38  * @author John Mani
39  */

40
41 public class UUEncoderStream extends FilterOutputStream {
42     private byte[] buffer; // cache of bytes that are yet to be encoded
43
private int bufsize = 0; // size of the cache
44
private boolean wrotePrefix = false;
45
46     protected String JavaDoc name; // name of file
47
protected int mode; // permissions mode
48

49     /**
50      * Create a UUencoder that encodes the specified input stream
51      * @param out the output stream
52      */

53     public UUEncoderStream(OutputStream out) {
54     this(out, "encoder.buf", 644);
55     }
56
57     /**
58      * Create a UUencoder that encodes the specified input stream
59      * @param out the output stream
60      * @param name Specifies a name for the encoded buffer
61      */

62     public UUEncoderStream(OutputStream out, String JavaDoc name) {
63     this(out, name, 644);
64     }
65
66     /**
67      * Create a UUencoder that encodes the specified input stream
68      * @param out the output stream
69      * @param name Specifies a name for the encoded buffer
70      * @param mode Specifies permission mode for the encoded buffer
71      */

72     public UUEncoderStream(OutputStream out, String JavaDoc name, int mode) {
73     super(out);
74     this.name = name;
75     this.mode = mode;
76     buffer = new byte[45];
77     }
78
79     /**
80      * Set up the buffer name and permission mode.
81      * This method has any effect only if it is invoked before
82      * you start writing into the output stream
83      */

84     public void setNameMode(String JavaDoc name, int mode) {
85     this.name = name;
86     this.mode = mode;
87     }
88
89     public void write(byte[] b, int off, int len) throws IOException {
90     for (int i = 0; i < len; i++)
91         write(b[off + i]);
92     }
93
94     public void write(byte[] data) throws IOException {
95     write(data, 0, data.length);
96     }
97
98     public void write(int c) throws IOException {
99     /* buffer up characters till we get a line's worth, then encode
100      * and write them out. Max number of characters allowed per
101      * line is 45.
102      */

103     buffer[bufsize++] = (byte)c;
104     if (bufsize == 45) {
105         writePrefix();
106         encode();
107         bufsize = 0;
108     }
109     }
110
111     public void flush() throws IOException {
112     if (bufsize > 0) { // If there's unencoded characters in the buffer
113
writePrefix();
114         encode(); // .. encode them
115
}
116     writeSuffix();
117     out.flush();
118     }
119
120     public void close() throws IOException {
121     flush();
122     out.close();
123     }
124
125     /**
126      * Write out the prefix: "begin <mode> <name>"
127      */

128     private void writePrefix() throws IOException {
129     if (!wrotePrefix) {
130         PrintStream ps = new PrintStream(out);
131         ps.println("begin " + mode + " " + name);
132         ps.flush();
133         wrotePrefix = true;
134     }
135     }
136
137     /**
138      * Write a single line containing space and the suffix line
139      * containing the single word "end" (terminated by a newline)
140      */

141     private void writeSuffix() throws IOException {
142     PrintStream ps = new PrintStream(out);
143     ps.println(" \nend");
144     ps.flush();
145     }
146
147     /**
148      * Encode a line.
149      * Start off with the character count, followed by the encoded atoms
150      * and terminate with LF. (or is it CRLF or the local line-terminator ?)
151      * Take three bytes and encodes them into 4 characters
152      * If bufsize if not a multiple of 3, the remaining bytes are filled
153      * with '1'. This insures that the last line won't end in spaces
154      * and potentiallly be truncated.
155      */

156     private void encode() throws IOException {
157     byte a, b, c;
158     int c1, c2, c3, c4;
159     int i = 0;
160
161     // Start off with the count of characters in the line
162
out.write((bufsize & 0x3f) + ' ');
163
164     while (i < bufsize) {
165         a = buffer[i++];
166         if (i < bufsize) {
167         b = buffer[i++];
168         if (i < bufsize)
169             c = buffer[i++];
170         else // default c to 1
171
c = 1;
172         }
173         else { // default b & c to 1
174
b = 1;
175         c = 1;
176         }
177
178         c1 = (a >>> 2) & 0x3f;
179         c2 = ((a << 4) & 0x30) | ((b >>> 4) & 0xf);
180         c3 = ((b << 2) & 0x3c) | ((c >>> 6) & 0x3);
181         c4 = c & 0x3f;
182         out.write(c1 + ' ');
183         out.write(c2 + ' ');
184         out.write(c3 + ' ');
185         out.write(c4 + ' ');
186     }
187     // Terminate with LF. (should it be CRLF or local line-terminator ?)
188
out.write('\n');
189     }
190
191     /**** begin TEST program *****
192     public static void main(String argv[]) throws Exception {
193     FileInputStream infile = new FileInputStream(argv[0]);
194     UUEncoderStream encoder = new UUEncoderStream(System.out);
195     int c;
196
197     while ((c = infile.read()) != -1)
198         encoder.write(c);
199     encoder.close();
200     }
201     **** end TEST program *****/

202 }
203
Popular Tags