jdk/src/share/classes/sun/io/ByteToCharConverter.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
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) 1996, 2004, 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.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An abstract base class for subclasses which convert character data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * in an external encoding into Unicode characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Asmus Freytag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Lloyd Honomichl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @deprecated Replaced by {@link java.nio.charset}.  THIS API WILL BE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * REMOVED IN J2SE 1.6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public abstract class ByteToCharConverter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Substitution mode flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected boolean subMode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Characters to use for automatic substitution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected char[] subChars = { '\uFFFD' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Offset of next character to be output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected int charOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Offset of next byte to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected int byteOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Length of bad input that caused a MalformedInputException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected int badInputLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Create an instance of the default ByteToCharConverter subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static ByteToCharConverter getDefault() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Object cvt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        cvt = Converters.newDefaultConverter(Converters.BYTE_TO_CHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return (ByteToCharConverter)cvt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Returns appropriate ByteToCharConverter subclass instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param string represents encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static ByteToCharConverter getConverter(String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Object cvt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        cvt = Converters.newConverter(Converters.BYTE_TO_CHAR, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return (ByteToCharConverter)cvt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns the character set id for the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public abstract String getCharacterEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Converts an array of bytes containing characters in an external
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * encoding into an array of Unicode characters.  This  method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * a buffer by buffer conversion of a data stream.  The state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * conversion is saved between calls to convert.  Among other things,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * this means multibyte input sequences can be split between calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * If a call to convert results in an exception, the conversion may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * continued by calling convert again with suitably modified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * All conversions should be finished with a call to the flush method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return the number of bytes written to output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param input byte array containing text to be converted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param inStart begin conversion at this offset in input array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param inEnd stop conversion at this offset in input array (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param output character array to receive conversion result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param outStart start writing to output array at this offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param outEnd stop writing to output array at this offset (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @exception MalformedInputException if the input buffer contains any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * sequence of bytes that is illegal for the input character set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @exception UnknownCharacterException for any character that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * that cannot be converted to Unicode. Thrown only when converter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * is not in substitution mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @exception ConversionBufferFullException if output array is filled prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * to converting all the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public abstract int convert(byte[] input, int inStart, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                char[] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throws MalformedInputException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                   UnknownCharacterException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                   ConversionBufferFullException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Converts an array of bytes containing characters in an external
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * encoding into an array of Unicode characters.  Unlike convert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * this method does not do incremental conversion.  It assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * the given input array contains all the characters to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * converted. The state of the converter is reset at the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * of this method and is left in the reset state on successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * termination.  The converter is not reset if an exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * thrown.  This allows the caller to determine where the bad input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * was encountered by calling nextByteIndex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * This method uses substitution mode when performing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * conversion.  The method setSubstitutionChars may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * determine what characters are substituted.  Even though substitution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * mode is used, the state of the converter's substitution mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * not changed at the end of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return an array of chars containing the converted characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param input array containing Unicode characters to be converted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @exception MalformedInputException if the input buffer contains any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * sequence of chars that is illegal in the input character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * After this exception is thrown,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * the method nextByteIndex can be called to obtain the index of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * first invalid input byte and getBadInputLength can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * to determine the length of the invalid input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @see   #nextByteIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see   #setSubstitutionMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see   sun.io.CharToByteConverter#setSubstitutionBytes(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see   #getBadInputLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public char[] convertAll( byte input[] ) throws MalformedInputException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        boolean savedSubMode = subMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        subMode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        char[] output = new char[ getMaxCharsPerByte() * input.length ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            int outputLength = convert( input, 0, input.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                        output, 0, output.length );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            outputLength += flush( output, outputLength, output.length );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            char [] returnedOutput = new char[ outputLength ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            System.arraycopy( output, 0, returnedOutput, 0, outputLength );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return returnedOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        catch( ConversionBufferFullException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            //Not supposed to happen.  If it does, getMaxCharsPerByte() lied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                InternalError("this.getMaxCharsBerByte returned bad value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        catch( UnknownCharacterException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // Not supposed to happen since we're in substitution mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            subMode = savedSubMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Writes any remaining output to the output buffer and resets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * converter to its initial state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param output char array to receive flushed output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param outStart start writing to output array at this offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param outEnd stop writing to output array at this offset (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @exception MalformedInputException if the output to be flushed contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * a partial or invalid multibyte character sequence.  flush will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * write what it can to the output buffer and reset the converter before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * throwing this exception.  An additional call to flush is not required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception ConversionBufferFullException if output array is filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * before all the output can be flushed. flush will write what it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * to the output buffer and remember its state.  An additional call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * flush with a new output buffer will conclude the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public abstract int flush( char[] output, int outStart, int outEnd )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        throws MalformedInputException, ConversionBufferFullException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Resets converter to its initial state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public abstract void reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Returns the maximum number of characters needed to convert a byte. Useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * for calculating the maximum output buffer size needed for a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public int getMaxCharsPerByte() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // Until UTF-16, this will do for every encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns the length, in bytes, of the input which caused a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * MalformedInputException.  Always refers to the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * MalformedInputException thrown by the converter.  If none have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * ever been thrown, returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public int getBadInputLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return badInputLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Returns the index of the  character just past the last character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * written by the previous call to convert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public int nextCharIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return charOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Returns the index of the byte just past the last byte successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * converted by the previous call to convert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public int nextByteIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return byteOff;
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
     * Sets converter into substitution mode.  In substitution mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * the converter will replace untranslatable characters in the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * encoding with the substitution character set by setSubstitionChars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * When not in substitution mode, the converter will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * UnknownCharacterException when it encounters untranslatable input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param doSub if true, enable substitution mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @see #setSubstitutionChars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public void setSubstitutionMode(boolean doSub) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        subMode = doSub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Sets the substitution characters to use when the converter is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * substitution mode.  The given chars must not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * longer than the value returned by getMaxCharsPerByte for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * converter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param newSubBytes the substitution bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @exception IllegalArgumentException if given byte array is longer than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *    the value returned by the method getMaxBytesPerChar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see #setSubstitutionMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see #getMaxBytesPerChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * sets the substitution character to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param c the substitution character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public void setSubstitutionChars(char[] c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if( c.length > getMaxCharsPerByte() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        subChars = new char[ c.length ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        System.arraycopy( c, 0, subChars, 0, c.length );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * returns a string representation of the character conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return "ByteToCharConverter: " + getCharacterEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}