CharacterDecoder.java revision 0
0N/A * Copyright 1995-2004 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * This class defines the decoding half of character encoders. 0N/A * A character decoder is an algorithim for transforming 8 bit 0N/A * binary data that has been encoded into text by a character 0N/A * encoder, back into original binary form. 0N/A * The character encoders, in general, have been structured 0N/A * around a central theme that binary data can be encoded into 0N/A * text that has the form: 0N/A * [Line Prefix][encoded data atoms][Line Suffix] 0N/A * Of course in the simplest encoding schemes, the buffer has no 0N/A * distinct prefix of suffix, however all have some fixed relationship 0N/A * between the text in an 'atom' and the binary data itself. 0N/A * In the CharacterEncoder and CharacterDecoder classes, one complete 0N/A * chunk of data is referred to as a <i>buffer</i>. Encoded buffers 0N/A * are all text, and decoded buffers (sometimes just referred to as 0N/A * buffers) are binary octets. 0N/A * To create a custom decoder, you must, at a minimum, overide three 0N/A * abstract methods in this class. 0N/A * <DD>bytesPerAtom which tells the decoder how many bytes to 0N/A * expect from decodeAtom 0N/A * <DD>decodeAtom which decodes the bytes sent to it as text. 0N/A * <DD>bytesPerLine which tells the encoder the maximum number of 0N/A * In general, the character decoders return error in the form of a 0N/A * CEFormatException. The syntax of the detail string is 0N/A * DecoderClassName: Error message. 0N/A * Several useful decoders have already been written and are 0N/A * referenced in the See Also list below. 0N/A * @author Chuck McManis 0N/A * @see CEFormatException 0N/A * @see CharacterEncoder 0N/A * @see BASE64Decoder 0N/A /** Return the number of bytes per atom of decoding */ 0N/A /** Return the maximum number of bytes that can be encoded per line */ 0N/A /** decode the beginning of the buffer, by default this is a NOP. */ 0N/A /** decode the buffer suffix, again by default it is a NOP. */ 0N/A * This method should return, if it knows, the number of bytes 0N/A * that will be decoded. Many formats such as uuencoding provide 0N/A * this information. By default we return the maximum bytes that 0N/A * could have been encoded on the line. 0N/A * This method post processes the line, if there are error detection 0N/A * or correction codes in a line, they are generally processed by 0N/A * this method. The simplest version of this method looks for the 0N/A * (newline) character. 0N/A * This method does an actual decode. It takes the decoded bytes and 0N/A * writes them to the OutputStream. The integer <i>l</i> tells the 0N/A * method how many bytes are required. This is always <= bytesPerAtom(). 0N/A * This method works around the bizarre semantics of BufferedInputStream's 0N/A for (
int i =
0; i <
len; i++) {
0N/A return ((i ==
0) ? -
1 : i);
0N/A * Decode the text from the InputStream and write the decoded 0N/A * octets to the OutputStream. This method runs until the stream 0N/A * @exception CEFormatException An error has occured while decoding 0N/A * @exception CEStreamExhausted The input stream is unexpectedly out of data 0N/A * Alternate decode interface that takes a String containing the encoded 0N/A * buffer and returns a byte array containing the data. 0N/A * @exception CEFormatException An error has occured while decoding 0N/A * Decode the contents of the inputstream into a buffer. 0N/A * Decode the contents of the String into a ByteBuffer. 0N/A * Decode the contents of the inputStream into a ByteBuffer.