KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > UUEncoder


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.PrintStream JavaDoc;
24
25 /**
26  * UUEncoding of an input stream placed into an outputstream.
27  * This class is meant to be a drop in replacement for
28  * sun.misc.UUEncoder, which was previously used by Ant.
29  * The uuencode algorithm code has been copied from the
30  * geronimo project.
31  **/

32
33 public class UUEncoder {
34     protected static final int DEFAULT_MODE = 644;
35     private static final int MAX_CHARS_PER_LINE = 45;
36     private OutputStream JavaDoc out;
37     private String JavaDoc name;
38
39     /**
40      * Constructor specifing a name for the encoded buffer, begin
41      * line will be:
42      * <pre>
43      * begin 644 [NAME]
44      * </pre>
45      * @param name the name of the encoded buffer.
46      */

47     public UUEncoder(String JavaDoc name) {
48         this.name = name;
49     }
50
51     /**
52      * UUEncode bytes from the input stream, and write them as text characters
53      * to the output stream. This method will run until it exhausts the
54      * input stream.
55      * @param is the input stream.
56      * @param out the output stream.
57      * @throws IOException if there is an error.
58      */

59     public void encode(InputStream JavaDoc is, OutputStream JavaDoc out)
60         throws IOException JavaDoc {
61         this.out = out;
62         encodeBegin();
63         byte[] buffer = new byte[MAX_CHARS_PER_LINE * 100];
64         int count;
65         while ((count = is.read(buffer, 0, buffer.length)) != -1) {
66             int pos = 0;
67             while (count > 0) {
68                 int num = count > MAX_CHARS_PER_LINE
69                     ? MAX_CHARS_PER_LINE
70                     : count;
71                 encodeLine(buffer, pos, num, out);
72                 pos += num;
73                 count -= num;
74             }
75         }
76         out.flush();
77         encodeEnd();
78     }
79
80     /**
81      * Encode a string to the output.
82      */

83     private void encodeString(String JavaDoc n) throws IOException JavaDoc {
84         PrintStream JavaDoc writer = new PrintStream JavaDoc(out);
85         writer.print(n);
86         writer.flush();
87     }
88
89     private void encodeBegin() throws IOException JavaDoc {
90         encodeString("begin " + DEFAULT_MODE + " " + name + "\n");
91     }
92
93     private void encodeEnd() throws IOException JavaDoc {
94         encodeString(" \nend\n");
95     }
96
97     /**
98      * Encode a single line of data (less than or equal to 45 characters).
99      *
100      * @param data The array of byte data.
101      * @param off The starting offset within the data.
102      * @param length Length of the data to encode.
103      * @param out The output stream the encoded data is written to.
104      *
105      * @exception IOException
106      */

107     private void encodeLine(
108         byte[] data, int offset, int length, OutputStream JavaDoc out)
109         throws IOException JavaDoc {
110         // write out the number of characters encoded in this line.
111
out.write((byte) ((length & 0x3F) + ' '));
112         byte a;
113         byte b;
114         byte c;
115
116         for (int i = 0; i < length;) {
117             // set the padding defaults
118
b = 1;
119             c = 1;
120             // get the next 3 bytes (if we have them)
121
a = data[offset + i++];
122             if (i < length) {
123                 b = data[offset + i++];
124                 if (i < length) {
125                     c = data[offset + i++];
126                 }
127             }
128
129             byte d1 = (byte) (((a >>> 2) & 0x3F) + ' ');
130             byte d2 = (byte) ((((a << 4) & 0x30) | ((b >>> 4) & 0x0F)) + ' ');
131             byte d3 = (byte) ((((b << 2) & 0x3C) | ((c >>> 6) & 0x3)) + ' ');
132             byte d4 = (byte) ((c & 0x3F) + ' ');
133
134             out.write(d1);
135             out.write(d2);
136             out.write(d3);
137             out.write(d4);
138         }
139
140         // terminate with a linefeed alone
141
out.write('\n');
142     }
143 }
144
Popular Tags