001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: Base64EncodeStream.java 447277 2006-09-18 06:19:34Z jeremias $ */
019:
020: package org.apache.xmlgraphics.util.io;
021:
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.io.PrintStream;
025:
026: /**
027: * This class implements a Base64 Character encoder as specified in RFC1113.
028: * Unlike some other encoding schemes there is nothing in this encoding
029: * that indicates where a buffer starts or ends.
030: *
031: * This means that the encoded text will simply start with the first line
032: * of encoded text and end with the last line of encoded text.
033: *
034: * @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
035: * @author <a href="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
036: * @author Chuck McManis
037: * @version $Id: Base64EncodeStream.java 447277 2006-09-18 06:19:34Z jeremias $
038: */
039: public class Base64EncodeStream extends OutputStream {
040:
041: /** This array maps the 6 bit values to their characters */
042: private final static byte pem_array[] = {
043: // 0 1 2 3 4 5 6 7
044: 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0
045: 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1
046: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2
047: 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 3
048: 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 4
049: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 5
050: 'w', 'x', 'y', 'z', '0', '1', '2', '3', // 6
051: '4', '5', '6', '7', '8', '9', '+', '/' // 7
052: };
053:
054: byte[] atom = new byte[3];
055: int atomLen = 0;
056: byte[] encodeBuf = new byte[4];
057: int lineLen = 0;
058:
059: PrintStream out;
060: boolean closeOutOnClose;
061:
062: public Base64EncodeStream(OutputStream out) {
063: this .out = new PrintStream(out);
064: closeOutOnClose = true;
065: }
066:
067: public Base64EncodeStream(OutputStream out, boolean closeOutOnClose) {
068: this .out = new PrintStream(out);
069: this .closeOutOnClose = closeOutOnClose;
070: }
071:
072: public void close() throws IOException {
073: if (out != null) {
074: encodeAtom();
075: out.flush();
076: if (closeOutOnClose)
077: out.close();
078: out = null;
079: }
080: }
081:
082: /**
083: * This can't really flush out output since that may generate
084: * '=' chars which would indicate the end of the stream.
085: * Instead we flush out. You can only be sure all output is
086: * writen by closing this stream.
087: */
088: public void flush() throws IOException {
089: out.flush();
090: }
091:
092: public void write(int b) throws IOException {
093: atom[atomLen++] = (byte) b;
094: if (atomLen == 3)
095: encodeAtom();
096: }
097:
098: public void write(byte[] data) throws IOException {
099: encodeFromArray(data, 0, data.length);
100: }
101:
102: public void write(byte[] data, int off, int len) throws IOException {
103: encodeFromArray(data, off, len);
104: }
105:
106: /**
107: * enocodeAtom - Take three bytes of input and encode it as 4
108: * printable characters. Note that if the length in len is less
109: * than three is encodes either one or two '=' signs to indicate
110: * padding characters.
111: */
112: void encodeAtom() throws IOException {
113: byte a, b, c;
114:
115: switch (atomLen) {
116: case 0:
117: return;
118: case 1:
119: a = atom[0];
120: encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)];
121: encodeBuf[1] = pem_array[((a << 4) & 0x30)];
122: encodeBuf[2] = encodeBuf[3] = '=';
123: break;
124: case 2:
125: a = atom[0];
126: b = atom[1];
127: encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)];
128: encodeBuf[1] = pem_array[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))];
129: encodeBuf[2] = pem_array[((b << 2) & 0x3C)];
130: encodeBuf[3] = '=';
131: break;
132: default:
133: a = atom[0];
134: b = atom[1];
135: c = atom[2];
136: encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)];
137: encodeBuf[1] = pem_array[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))];
138: encodeBuf[2] = pem_array[(((b << 2) & 0x3C) | ((c >>> 6) & 0x03))];
139: encodeBuf[3] = pem_array[c & 0x3F];
140: }
141: if (lineLen == 64) {
142: out.println();
143: lineLen = 0;
144: }
145: out.write(encodeBuf);
146:
147: lineLen += 4;
148: atomLen = 0;
149: }
150:
151: /**
152: * enocodeAtom - Take three bytes of input and encode it as 4
153: * printable characters. Note that if the length in len is less
154: * than three is encodes either one or two '=' signs to indicate
155: * padding characters.
156: */
157: void encodeFromArray(byte[] data, int offset, int len)
158: throws IOException {
159: byte a, b, c;
160: if (len == 0)
161: return;
162:
163: // System.out.println("atomLen: " + atomLen +
164: // " len: " + len +
165: // " offset: " + offset);
166:
167: if (atomLen != 0) {
168: switch (atomLen) {
169: case 1:
170: atom[1] = data[offset++];
171: len--;
172: atomLen++;
173: if (len == 0)
174: return;
175: atom[2] = data[offset++];
176: len--;
177: atomLen++;
178: break;
179: case 2:
180: atom[2] = data[offset++];
181: len--;
182: atomLen++;
183: break;
184: default:
185: }
186: encodeAtom();
187: }
188:
189: while (len >= 3) {
190: a = data[offset++];
191: b = data[offset++];
192: c = data[offset++];
193:
194: encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)];
195: encodeBuf[1] = pem_array[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))];
196: encodeBuf[2] = pem_array[(((b << 2) & 0x3C) | ((c >>> 6) & 0x03))];
197: encodeBuf[3] = pem_array[c & 0x3F];
198: out.write(encodeBuf);
199:
200: lineLen += 4;
201: if (lineLen == 64) {
202: out.println();
203: lineLen = 0;
204: }
205:
206: len -= 3;
207: }
208:
209: switch (len) {
210: case 1:
211: atom[0] = data[offset];
212: break;
213: case 2:
214: atom[0] = data[offset];
215: atom[1] = data[offset + 1];
216: break;
217: default:
218: }
219: atomLen = len;
220: }
221:
222: }
|