0N/A/*
2362N/A * Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
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 *
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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.misc;
0N/A
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.PushbackInputStream;
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.nio.ByteBuffer;
0N/A
0N/A/**
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 *
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 *
0N/A * <pre>
0N/A * [Buffer Prefix]
0N/A * [Line Prefix][encoded data atoms][Line Suffix]
0N/A * [Buffer Suffix]
0N/A * </pre>
0N/A *
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 *
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 *
0N/A * To create a custom decoder, you must, at a minimum, overide three
0N/A * abstract methods in this class.
0N/A * <DL>
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 * bytes per line.
0N/A * </DL>
0N/A *
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 * <pre>
0N/A * DecoderClassName: Error message.
0N/A * </pre>
0N/A *
0N/A * Several useful decoders have already been written and are
0N/A * referenced in the See Also list below.
0N/A *
0N/A * @author Chuck McManis
0N/A * @see CEFormatException
0N/A * @see CharacterEncoder
0N/A * @see UCDecoder
0N/A * @see UUDecoder
0N/A * @see BASE64Decoder
0N/A */
0N/A
0N/Apublic abstract class CharacterDecoder {
0N/A
0N/A /** Return the number of bytes per atom of decoding */
0N/A abstract protected int bytesPerAtom();
0N/A
0N/A /** Return the maximum number of bytes that can be encoded per line */
0N/A abstract protected int bytesPerLine();
0N/A
0N/A /** decode the beginning of the buffer, by default this is a NOP. */
0N/A protected void decodeBufferPrefix(PushbackInputStream aStream, OutputStream bStream) throws IOException { }
0N/A
0N/A /** decode the buffer suffix, again by default it is a NOP. */
0N/A protected void decodeBufferSuffix(PushbackInputStream aStream, OutputStream bStream) throws IOException { }
0N/A
0N/A /**
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 */
0N/A protected int decodeLinePrefix(PushbackInputStream aStream, OutputStream bStream) throws IOException {
0N/A return (bytesPerLine());
0N/A }
0N/A
0N/A /**
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 */
0N/A protected void decodeLineSuffix(PushbackInputStream aStream, OutputStream bStream) throws IOException { }
0N/A
0N/A /**
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 */
0N/A protected void decodeAtom(PushbackInputStream aStream, OutputStream bStream, int l) throws IOException {
0N/A throw new CEStreamExhausted();
0N/A }
0N/A
0N/A /**
0N/A * This method works around the bizarre semantics of BufferedInputStream's
0N/A * read method.
0N/A */
0N/A protected int readFully(InputStream in, byte buffer[], int offset, int len)
0N/A throws java.io.IOException {
0N/A for (int i = 0; i < len; i++) {
0N/A int q = in.read();
0N/A if (q == -1)
0N/A return ((i == 0) ? -1 : i);
0N/A buffer[i+offset] = (byte)q;
0N/A }
0N/A return len;
0N/A }
0N/A
0N/A /**
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 * is exhausted.
0N/A * @exception CEFormatException An error has occured while decoding
0N/A * @exception CEStreamExhausted The input stream is unexpectedly out of data
0N/A */
0N/A public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException {
0N/A int i;
0N/A int totalBytes = 0;
0N/A
0N/A PushbackInputStream ps = new PushbackInputStream (aStream);
0N/A decodeBufferPrefix(ps, bStream);
0N/A while (true) {
0N/A int length;
0N/A
0N/A try {
0N/A length = decodeLinePrefix(ps, bStream);
0N/A for (i = 0; (i+bytesPerAtom()) < length; i += bytesPerAtom()) {
0N/A decodeAtom(ps, bStream, bytesPerAtom());
0N/A totalBytes += bytesPerAtom();
0N/A }
0N/A if ((i + bytesPerAtom()) == length) {
0N/A decodeAtom(ps, bStream, bytesPerAtom());
0N/A totalBytes += bytesPerAtom();
0N/A } else {
0N/A decodeAtom(ps, bStream, length - i);
0N/A totalBytes += (length - i);
0N/A }
0N/A decodeLineSuffix(ps, bStream);
0N/A } catch (CEStreamExhausted e) {
0N/A break;
0N/A }
0N/A }
0N/A decodeBufferSuffix(ps, bStream);
0N/A }
0N/A
0N/A /**
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 */
0N/A public byte decodeBuffer(String inputString)[] throws IOException {
0N/A byte inputBuffer[] = new byte[inputString.length()];
0N/A ByteArrayInputStream inStream;
0N/A ByteArrayOutputStream outStream;
0N/A
0N/A inputString.getBytes(0, inputString.length(), inputBuffer, 0);
0N/A inStream = new ByteArrayInputStream(inputBuffer);
0N/A outStream = new ByteArrayOutputStream();
0N/A decodeBuffer(inStream, outStream);
0N/A return (outStream.toByteArray());
0N/A }
0N/A
0N/A /**
0N/A * Decode the contents of the inputstream into a buffer.
0N/A */
0N/A public byte decodeBuffer(InputStream in)[] throws IOException {
0N/A ByteArrayOutputStream outStream = new ByteArrayOutputStream();
0N/A decodeBuffer(in, outStream);
0N/A return (outStream.toByteArray());
0N/A }
0N/A
0N/A /**
0N/A * Decode the contents of the String into a ByteBuffer.
0N/A */
0N/A public ByteBuffer decodeBufferToByteBuffer(String inputString)
0N/A throws IOException {
0N/A return ByteBuffer.wrap(decodeBuffer(inputString));
0N/A }
0N/A
0N/A /**
0N/A * Decode the contents of the inputStream into a ByteBuffer.
0N/A */
0N/A public ByteBuffer decodeBufferToByteBuffer(InputStream in)
0N/A throws IOException {
0N/A return ByteBuffer.wrap(decodeBuffer(in));
0N/A }
0N/A}