001: /*
002: * @(#)CharacterDecoder.java 1.23 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.misc;
029:
030: import java.io.OutputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.io.InputStream;
033: import java.io.ByteArrayInputStream;
034: import java.io.IOException;
035:
036: /**
037: * This class defines the decoding half of character encoders.
038: * A character decoder is an algorithim for transforming 8 bit
039: * binary data that has been encoded into text by a character
040: * encoder, back into original binary form.
041: *
042: * The character encoders, in general, have been structured
043: * around a central theme that binary data can be encoded into
044: * text that has the form:
045: *
046: * <pre>
047: * [Buffer Prefix]
048: * [Line Prefix][encoded data atoms][Line Suffix]
049: * [Buffer Suffix]
050: * </pre>
051: *
052: * Of course in the simplest encoding schemes, the buffer has no
053: * distinct prefix of suffix, however all have some fixed relationship
054: * between the text in an 'atom' and the binary data itself.
055: *
056: * In the CharacterEncoder and CharacterDecoder classes, one complete
057: * chunk of data is referred to as a <i>buffer</i>. Encoded buffers
058: * are all text, and decoded buffers (sometimes just referred to as
059: * buffers) are binary octets.
060: *
061: * To create a custom decoder, you must, at a minimum, overide three
062: * abstract methods in this class.
063: * <DL>
064: * <DD>bytesPerAtom which tells the decoder how many bytes to
065: * expect from decodeAtom
066: * <DD>decodeAtom which decodes the bytes sent to it as text.
067: * <DD>bytesPerLine which tells the encoder the maximum number of
068: * bytes per line.
069: * </DL>
070: *
071: * In general, the character decoders return error in the form of a
072: * CEFormatException. The syntax of the detail string is
073: * <pre>
074: * DecoderClassName: Error message.
075: * </pre>
076: *
077: * Several useful decoders have already been written and are
078: * referenced in the See Also list below.
079: *
080: * @version 05/03/00, 1.17
081: * @author Chuck McManis
082: * @see CEFormatException
083: * @see CharacterEncoder
084: * @see UCDecoder
085: * @see UUDecoder
086: * @see BASE64Decoder
087: */
088:
089: public abstract class CharacterDecoder {
090:
091: /** Return the number of bytes per atom of decoding */
092: abstract protected int bytesPerAtom();
093:
094: /** Return the maximum number of bytes that can be encoded per line */
095: abstract protected int bytesPerLine();
096:
097: /** decode the beginning of the buffer, by default this is a NOP. */
098: protected void decodeBufferPrefix(InputStream aStream,
099: OutputStream bStream) throws IOException {
100: }
101:
102: /** decode the buffer suffix, again by default it is a NOP. */
103: protected void decodeBufferSuffix(InputStream aStream,
104: OutputStream bStream) throws IOException {
105: }
106:
107: /**
108: * This method should return, if it knows, the number of bytes
109: * that will be decoded. Many formats such as uuencoding provide
110: * this information. By default we return the maximum bytes that
111: * could have been encoded on the line.
112: */
113: protected int decodeLinePrefix(InputStream aStream,
114: OutputStream bStream) throws IOException {
115: return (bytesPerLine());
116: }
117:
118: /**
119: * This method post processes the line, if there are error detection
120: * or correction codes in a line, they are generally processed by
121: * this method. The simplest version of this method looks for the
122: * (newline) character.
123: */
124: protected void decodeLineSuffix(InputStream aStream,
125: OutputStream bStream) throws IOException {
126: }
127:
128: /**
129: * This method does an actual decode. It takes the decoded bytes and
130: * writes them to the OuputStream. The integer <i>l</i> tells the
131: * method how many bytes are required. This is always <= bytesPerAtom().
132: */
133: protected void decodeAtom(InputStream aStream,
134: OutputStream bStream, int l) throws IOException {
135: throw new CEStreamExhausted();
136: }
137:
138: /**
139: * This method works around the bizarre semantics of BufferedInputStream's
140: * read method.
141: */
142: protected int readFully(InputStream in, byte buffer[], int offset,
143: int len) throws java.io.IOException {
144: for (int i = 0; i < len; i++) {
145: int q = in.read();
146: if (q == -1)
147: return ((i == 0) ? -1 : i);
148: buffer[i + offset] = (byte) q;
149: }
150: return len;
151: }
152:
153: /**
154: * Decode the text from the InputStream and write the decoded
155: * octets to the OutputStream. This method runs until the stream
156: * is exhausted.
157: * @exception CEFormatException An error has occured while decoding
158: * @exception CEStreamExhausted The input stream is unexpectedly out of data
159: */
160: public void decodeBuffer(InputStream aStream, OutputStream bStream)
161: throws IOException {
162: int i;
163: int totalBytes = 0;
164:
165: decodeBufferPrefix(aStream, bStream);
166: while (true) {
167: int length;
168:
169: try {
170: length = decodeLinePrefix(aStream, bStream);
171: for (i = 0; (i + bytesPerAtom()) < length; i += bytesPerAtom()) {
172: decodeAtom(aStream, bStream, bytesPerAtom());
173: totalBytes += bytesPerAtom();
174: }
175: if ((i + bytesPerAtom()) == length) {
176: decodeAtom(aStream, bStream, bytesPerAtom());
177: totalBytes += bytesPerAtom();
178: } else {
179: decodeAtom(aStream, bStream, length - i);
180: totalBytes += (length - i);
181: }
182: decodeLineSuffix(aStream, bStream);
183: } catch (CEStreamExhausted e) {
184: break;
185: }
186: }
187: decodeBufferSuffix(aStream, bStream);
188: }
189:
190: /**
191: * Alternate decode interface that takes a String containing the encoded
192: * buffer and returns a byte array containing the data.
193: * @exception CEFormatException An error has occured while decoding
194: */
195: public byte decodeBuffer(String inputString)[] throws IOException {
196: byte inputBuffer[] = new byte[inputString.length()];
197: ByteArrayInputStream inStream;
198: ByteArrayOutputStream outStream;
199:
200: inputBuffer = inputString.getBytes();
201: inStream = new ByteArrayInputStream(inputBuffer);
202: outStream = new ByteArrayOutputStream();
203: decodeBuffer(inStream, outStream);
204: return (outStream.toByteArray());
205: }
206:
207: /**
208: * Decode the contents of the inputstream into a buffer.
209: */
210: public byte decodeBuffer(InputStream in)[] throws IOException {
211: ByteArrayOutputStream outStream = new ByteArrayOutputStream();
212: decodeBuffer(in, outStream);
213: return (outStream.toByteArray());
214: }
215: }
|