jdk/src/share/classes/sun/misc/CharacterEncoder.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 23742 c2b6216ef41d
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class defines the encoding half of character encoders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A character encoder is an algorithim for transforming 8 bit binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * data into text (generally 7 bit ASCII or 8 bit ISO-Latin-1 text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * for transmition over text channels such as e-mail and network news.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The character encoders have been structured around a central theme
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * that, in general, the encoded text has the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      [Buffer Prefix]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      [Line Prefix][encoded data atoms][Line Suffix]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      [Buffer Suffix]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * In the CharacterEncoder and CharacterDecoder classes, one complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * chunk of data is referred to as a <i>buffer</i>. Encoded buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * are all text, and decoded buffers (sometimes just referred to as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * buffers) are binary octets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * To create a custom encoder, you must, at a minimum,  overide three
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * abstract methods in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <DL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <DD>bytesPerAtom which tells the encoder how many bytes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * send to encodeAtom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <DD>encodeAtom which encodes the bytes sent to it as text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <DD>bytesPerLine which tells the encoder the maximum number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * bytes per line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * </DL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Several useful encoders have already been written and are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * referenced in the See Also list below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author      Chuck McManis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @see         CharacterDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @see         UCEncoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @see         UUEncoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see         BASE64Encoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
public abstract class CharacterEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** Stream that understands "printing" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected PrintStream pStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /** Return the number of bytes per atom of encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    abstract protected int bytesPerAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /** Return the number of bytes that can be encoded per line */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    abstract protected int bytesPerLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Encode the prefix for the entire buffer. By default is simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * opens the PrintStream for use by the other functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected void encodeBufferPrefix(OutputStream aStream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        pStream = new PrintStream(aStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Encode the suffix for the entire buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected void encodeBufferSuffix(OutputStream aStream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Encode the prefix that starts every output line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected void encodeLinePrefix(OutputStream aStream, int aLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Encode the suffix that ends every output line. By default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * this method just prints a <newline> into the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected void encodeLineSuffix(OutputStream aStream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        pStream.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /** Encode one "atom" of information into characters. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    abstract protected void encodeAtom(OutputStream aStream, byte someBytes[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                int anOffset, int aLength) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * This method works around the bizarre semantics of BufferedInputStream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * read method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected int readFully(InputStream in, byte buffer[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (int i = 0; i < buffer.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            int q = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (q == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            buffer[i] = (byte)q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Encode bytes from the input stream, and write them as text characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * to the output stream. This method will run until it exhausts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * input stream, but does not print the line suffix for a final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * line that is shorter than bytesPerLine().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void encode(InputStream inStream, OutputStream outStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int     j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int     numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        byte    tmpbuffer[] = new byte[bytesPerLine()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        encodeBufferPrefix(outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            numBytes = readFully(inStream, tmpbuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (numBytes == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            encodeLinePrefix(outStream, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            for (j = 0; j < numBytes; j += bytesPerAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if ((j + bytesPerAtom()) <= numBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    encodeAtom(outStream, tmpbuffer, j, bytesPerAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    encodeAtom(outStream, tmpbuffer, j, (numBytes)- j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (numBytes < bytesPerLine()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                encodeLineSuffix(outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        encodeBufferSuffix(outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Encode the buffer in <i>aBuffer</i> and write the encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * result to the OutputStream <i>aStream</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public void encode(byte aBuffer[], OutputStream aStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        encode(inStream, aStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * A 'streamless' version of encode that simply takes a buffer of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * bytes and returns a string containing the encoded buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public String encode(byte aBuffer[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        ByteArrayOutputStream   outStream = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        ByteArrayInputStream    inStream = new ByteArrayInputStream(aBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        String retVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            encode(inStream, outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // explicit ascii->unicode conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            retVal = outStream.toString("8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } catch (Exception IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // This should never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new Error("CharacterEncoder.encode internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return (retVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Return a byte array from the remaining bytes in this ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * The ByteBuffer's position will be advanced to ByteBuffer's limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * To avoid an extra copy, the implementation will attempt to return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * byte array backing the ByteBuffer.  If this is not possible, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * new byte array will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private byte [] getBytes(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         * This should never return a BufferOverflowException, as we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * careful to allocate just the right amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        byte [] buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * If it has a usable backing byte buffer, use it.  Use only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * if the array exactly represents the current ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (bb.hasArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            byte [] tmp = bb.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if ((tmp.length == bb.capacity()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    (tmp.length == bb.remaining())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                buf = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                bb.position(bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
             * This class doesn't have a concept of encode(buf, len, off),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
             * so if we have a partial buffer, we must reallocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
             * space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            buf = new byte[bb.remaining()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
             * position() automatically updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            bb.get(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Encode the <i>aBuffer</i> ByteBuffer and write the encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * result to the OutputStream <i>aStream</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * The ByteBuffer's position will be advanced to ByteBuffer's limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public void encode(ByteBuffer aBuffer, OutputStream aStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        byte [] buf = getBytes(aBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        encode(buf, aStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * A 'streamless' version of encode that simply takes a ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * and returns a string containing the encoded buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * The ByteBuffer's position will be advanced to ByteBuffer's limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public String encode(ByteBuffer aBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        byte [] buf = getBytes(aBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return encode(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Encode bytes from the input stream, and write them as text characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * to the output stream. This method will run until it exhausts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * input stream. It differs from encode in that it will add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * line at the end of a final line that is shorter than bytesPerLine().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public void encodeBuffer(InputStream inStream, OutputStream outStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int     j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int     numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        byte    tmpbuffer[] = new byte[bytesPerLine()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        encodeBufferPrefix(outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            numBytes = readFully(inStream, tmpbuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (numBytes == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            encodeLinePrefix(outStream, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            for (j = 0; j < numBytes; j += bytesPerAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if ((j + bytesPerAtom()) <= numBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    encodeAtom(outStream, tmpbuffer, j, bytesPerAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    encodeAtom(outStream, tmpbuffer, j, (numBytes)- j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            encodeLineSuffix(outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (numBytes < bytesPerLine()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        encodeBufferSuffix(outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Encode the buffer in <i>aBuffer</i> and write the encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * result to the OutputStream <i>aStream</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void encodeBuffer(byte aBuffer[], OutputStream aStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        encodeBuffer(inStream, aStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * A 'streamless' version of encode that simply takes a buffer of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * bytes and returns a string containing the encoded buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public String encodeBuffer(byte aBuffer[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        ByteArrayOutputStream   outStream = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        ByteArrayInputStream    inStream = new ByteArrayInputStream(aBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            encodeBuffer(inStream, outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } catch (Exception IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            // This should never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw new Error("CharacterEncoder.encodeBuffer internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return (outStream.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Encode the <i>aBuffer</i> ByteBuffer and write the encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * result to the OutputStream <i>aStream</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * The ByteBuffer's position will be advanced to ByteBuffer's limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        byte [] buf = getBytes(aBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        encodeBuffer(buf, aStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * A 'streamless' version of encode that simply takes a ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * and returns a string containing the encoded buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * The ByteBuffer's position will be advanced to ByteBuffer's limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public String encodeBuffer(ByteBuffer aBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        byte [] buf = getBytes(aBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return encodeBuffer(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}