0N/A/*
2362N/A * Copyright (c) 1995, 2005, 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.InputStream;
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.IOException;
0N/Aimport java.nio.ByteBuffer;
0N/A
0N/A
0N/A/**
0N/A * This class defines the encoding half of character encoders.
0N/A * A character encoder is an algorithim for transforming 8 bit binary
0N/A * data into text (generally 7 bit ASCII or 8 bit ISO-Latin-1 text)
0N/A * for transmition over text channels such as e-mail and network news.
0N/A *
0N/A * The character encoders have been structured around a central theme
0N/A * that, in general, the encoded text 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 * 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 encoder, you must, at a minimum, overide three
0N/A * abstract methods in this class.
0N/A * <DL>
0N/A * <DD>bytesPerAtom which tells the encoder how many bytes to
0N/A * send to encodeAtom
0N/A * <DD>encodeAtom which encodes 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 * Several useful encoders 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 CharacterDecoder;
0N/A * @see UCEncoder
0N/A * @see UUEncoder
0N/A * @see BASE64Encoder
0N/A */
0N/Apublic abstract class CharacterEncoder {
0N/A
0N/A /** Stream that understands "printing" */
0N/A protected PrintStream pStream;
0N/A
0N/A /** Return the number of bytes per atom of encoding */
0N/A abstract protected int bytesPerAtom();
0N/A
0N/A /** Return the number of bytes that can be encoded per line */
0N/A abstract protected int bytesPerLine();
0N/A
0N/A /**
0N/A * Encode the prefix for the entire buffer. By default is simply
0N/A * opens the PrintStream for use by the other functions.
0N/A */
0N/A protected void encodeBufferPrefix(OutputStream aStream) throws IOException {
0N/A pStream = new PrintStream(aStream);
0N/A }
0N/A
0N/A /**
0N/A * Encode the suffix for the entire buffer.
0N/A */
0N/A protected void encodeBufferSuffix(OutputStream aStream) throws IOException {
0N/A }
0N/A
0N/A /**
0N/A * Encode the prefix that starts every output line.
0N/A */
0N/A protected void encodeLinePrefix(OutputStream aStream, int aLength)
0N/A throws IOException {
0N/A }
0N/A
0N/A /**
0N/A * Encode the suffix that ends every output line. By default
0N/A * this method just prints a <newline> into the output stream.
0N/A */
0N/A protected void encodeLineSuffix(OutputStream aStream) throws IOException {
0N/A pStream.println();
0N/A }
0N/A
0N/A /** Encode one "atom" of information into characters. */
0N/A abstract protected void encodeAtom(OutputStream aStream, byte someBytes[],
0N/A int anOffset, int aLength) throws IOException;
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[])
0N/A throws java.io.IOException {
0N/A for (int i = 0; i < buffer.length; i++) {
0N/A int q = in.read();
0N/A if (q == -1)
0N/A return i;
0N/A buffer[i] = (byte)q;
0N/A }
0N/A return buffer.length;
0N/A }
0N/A
0N/A /**
0N/A * Encode bytes from the input stream, and write them as text characters
0N/A * to the output stream. This method will run until it exhausts the
0N/A * input stream, but does not print the line suffix for a final
0N/A * line that is shorter than bytesPerLine().
0N/A */
0N/A public void encode(InputStream inStream, OutputStream outStream)
0N/A throws IOException {
0N/A int j;
0N/A int numBytes;
0N/A byte tmpbuffer[] = new byte[bytesPerLine()];
0N/A
0N/A encodeBufferPrefix(outStream);
0N/A
0N/A while (true) {
0N/A numBytes = readFully(inStream, tmpbuffer);
0N/A if (numBytes == 0) {
0N/A break;
0N/A }
0N/A encodeLinePrefix(outStream, numBytes);
0N/A for (j = 0; j < numBytes; j += bytesPerAtom()) {
0N/A
0N/A if ((j + bytesPerAtom()) <= numBytes) {
0N/A encodeAtom(outStream, tmpbuffer, j, bytesPerAtom());
0N/A } else {
0N/A encodeAtom(outStream, tmpbuffer, j, (numBytes)- j);
0N/A }
0N/A }
0N/A if (numBytes < bytesPerLine()) {
0N/A break;
0N/A } else {
0N/A encodeLineSuffix(outStream);
0N/A }
0N/A }
0N/A encodeBufferSuffix(outStream);
0N/A }
0N/A
0N/A /**
0N/A * Encode the buffer in <i>aBuffer</i> and write the encoded
0N/A * result to the OutputStream <i>aStream</i>.
0N/A */
0N/A public void encode(byte aBuffer[], OutputStream aStream)
0N/A throws IOException {
0N/A ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
0N/A encode(inStream, aStream);
0N/A }
0N/A
0N/A /**
0N/A * A 'streamless' version of encode that simply takes a buffer of
0N/A * bytes and returns a string containing the encoded buffer.
0N/A */
0N/A public String encode(byte aBuffer[]) {
0N/A ByteArrayOutputStream outStream = new ByteArrayOutputStream();
0N/A ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
0N/A String retVal = null;
0N/A try {
0N/A encode(inStream, outStream);
0N/A // explicit ascii->unicode conversion
0N/A retVal = outStream.toString("8859_1");
0N/A } catch (Exception IOException) {
0N/A // This should never happen.
0N/A throw new Error("CharacterEncoder.encode internal error");
0N/A }
0N/A return (retVal);
0N/A }
0N/A
0N/A /**
0N/A * Return a byte array from the remaining bytes in this ByteBuffer.
0N/A * <P>
0N/A * The ByteBuffer's position will be advanced to ByteBuffer's limit.
0N/A * <P>
0N/A * To avoid an extra copy, the implementation will attempt to return the
0N/A * byte array backing the ByteBuffer. If this is not possible, a
0N/A * new byte array will be created.
0N/A */
0N/A private byte [] getBytes(ByteBuffer bb) {
0N/A /*
0N/A * This should never return a BufferOverflowException, as we're
0N/A * careful to allocate just the right amount.
0N/A */
0N/A byte [] buf = null;
0N/A
0N/A /*
0N/A * If it has a usable backing byte buffer, use it. Use only
0N/A * if the array exactly represents the current ByteBuffer.
0N/A */
0N/A if (bb.hasArray()) {
0N/A byte [] tmp = bb.array();
0N/A if ((tmp.length == bb.capacity()) &&
0N/A (tmp.length == bb.remaining())) {
0N/A buf = tmp;
0N/A bb.position(bb.limit());
0N/A }
0N/A }
0N/A
0N/A if (buf == null) {
0N/A /*
0N/A * This class doesn't have a concept of encode(buf, len, off),
0N/A * so if we have a partial buffer, we must reallocate
0N/A * space.
0N/A */
0N/A buf = new byte[bb.remaining()];
0N/A
0N/A /*
0N/A * position() automatically updated
0N/A */
0N/A bb.get(buf);
0N/A }
0N/A
0N/A return buf;
0N/A }
0N/A
0N/A /**
0N/A * Encode the <i>aBuffer</i> ByteBuffer and write the encoded
0N/A * result to the OutputStream <i>aStream</i>.
0N/A * <P>
0N/A * The ByteBuffer's position will be advanced to ByteBuffer's limit.
0N/A */
0N/A public void encode(ByteBuffer aBuffer, OutputStream aStream)
0N/A throws IOException {
0N/A byte [] buf = getBytes(aBuffer);
0N/A encode(buf, aStream);
0N/A }
0N/A
0N/A /**
0N/A * A 'streamless' version of encode that simply takes a ByteBuffer
0N/A * and returns a string containing the encoded buffer.
0N/A * <P>
0N/A * The ByteBuffer's position will be advanced to ByteBuffer's limit.
0N/A */
0N/A public String encode(ByteBuffer aBuffer) {
0N/A byte [] buf = getBytes(aBuffer);
0N/A return encode(buf);
0N/A }
0N/A
0N/A /**
0N/A * Encode bytes from the input stream, and write them as text characters
0N/A * to the output stream. This method will run until it exhausts the
0N/A * input stream. It differs from encode in that it will add the
0N/A * line at the end of a final line that is shorter than bytesPerLine().
0N/A */
0N/A public void encodeBuffer(InputStream inStream, OutputStream outStream)
0N/A throws IOException {
0N/A int j;
0N/A int numBytes;
0N/A byte tmpbuffer[] = new byte[bytesPerLine()];
0N/A
0N/A encodeBufferPrefix(outStream);
0N/A
0N/A while (true) {
0N/A numBytes = readFully(inStream, tmpbuffer);
0N/A if (numBytes == 0) {
0N/A break;
0N/A }
0N/A encodeLinePrefix(outStream, numBytes);
0N/A for (j = 0; j < numBytes; j += bytesPerAtom()) {
0N/A if ((j + bytesPerAtom()) <= numBytes) {
0N/A encodeAtom(outStream, tmpbuffer, j, bytesPerAtom());
0N/A } else {
0N/A encodeAtom(outStream, tmpbuffer, j, (numBytes)- j);
0N/A }
0N/A }
0N/A encodeLineSuffix(outStream);
0N/A if (numBytes < bytesPerLine()) {
0N/A break;
0N/A }
0N/A }
0N/A encodeBufferSuffix(outStream);
0N/A }
0N/A
0N/A /**
0N/A * Encode the buffer in <i>aBuffer</i> and write the encoded
0N/A * result to the OutputStream <i>aStream</i>.
0N/A */
0N/A public void encodeBuffer(byte aBuffer[], OutputStream aStream)
0N/A throws IOException {
0N/A ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
0N/A encodeBuffer(inStream, aStream);
0N/A }
0N/A
0N/A /**
0N/A * A 'streamless' version of encode that simply takes a buffer of
0N/A * bytes and returns a string containing the encoded buffer.
0N/A */
0N/A public String encodeBuffer(byte aBuffer[]) {
0N/A ByteArrayOutputStream outStream = new ByteArrayOutputStream();
0N/A ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
0N/A try {
0N/A encodeBuffer(inStream, outStream);
0N/A } catch (Exception IOException) {
0N/A // This should never happen.
0N/A throw new Error("CharacterEncoder.encodeBuffer internal error");
0N/A }
0N/A return (outStream.toString());
0N/A }
0N/A
0N/A /**
0N/A * Encode the <i>aBuffer</i> ByteBuffer and write the encoded
0N/A * result to the OutputStream <i>aStream</i>.
0N/A * <P>
0N/A * The ByteBuffer's position will be advanced to ByteBuffer's limit.
0N/A */
0N/A public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream)
0N/A throws IOException {
0N/A byte [] buf = getBytes(aBuffer);
0N/A encodeBuffer(buf, aStream);
0N/A }
0N/A
0N/A /**
0N/A * A 'streamless' version of encode that simply takes a ByteBuffer
0N/A * and returns a string containing the encoded buffer.
0N/A * <P>
0N/A * The ByteBuffer's position will be advanced to ByteBuffer's limit.
0N/A */
0N/A public String encodeBuffer(ByteBuffer aBuffer) {
0N/A byte [] buf = getBytes(aBuffer);
0N/A return encodeBuffer(buf);
0N/A }
0N/A
0N/A}