jtulach@1334: /*
jaroslav@1337: * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@1337: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1334: *
jaroslav@1337: * This code is free software; you can redistribute it and/or modify it
jaroslav@1337: * under the terms of the GNU General Public License version 2 only, as
jaroslav@1337: * published by the Free Software Foundation. Oracle designates this
jaroslav@1337: * particular file as subject to the "Classpath" exception as provided
jaroslav@1337: * by Oracle in the LICENSE file that accompanied this code.
jtulach@1334: *
jaroslav@1337: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1337: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1337: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@1337: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1337: * accompanied this code).
jtulach@1334: *
jaroslav@1337: * You should have received a copy of the GNU General Public License version
jaroslav@1337: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1337: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1334: *
jaroslav@1337: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1337: * or visit www.oracle.com if you need additional information or have any
jaroslav@1337: * questions.
jtulach@1334: */
jtulach@1334:
jtulach@1334: // -- This file was mechanically generated: Do not edit! -- //
jtulach@1334:
jtulach@1334: package java.nio.charset;
jtulach@1334:
jtulach@1334: import java.nio.Buffer;
jtulach@1334: import java.nio.ByteBuffer;
jtulach@1334: import java.nio.CharBuffer;
jtulach@1334: import java.nio.BufferOverflowException;
jtulach@1334: import java.nio.BufferUnderflowException;
jtulach@1334: import java.lang.ref.WeakReference;
jtulach@1334: import java.nio.charset.CoderMalfunctionError; // javadoc
jtulach@1334:
jtulach@1334:
jtulach@1334: /**
jtulach@1334: * An engine that can transform a sequence of bytes in a specific charset into a sequence of
jtulach@1334: * sixteen-bit Unicode characters.
jtulach@1334: *
jtulach@1334: *
jtulach@1334: *
jtulach@1334: * The input byte sequence is provided in a byte buffer or a series
jtulach@1334: * of such buffers. The output character sequence is written to a character buffer
jtulach@1334: * or a series of such buffers. A decoder should always be used by making
jtulach@1334: * the following sequence of method invocations, hereinafter referred to as a
jtulach@1334: * decoding operation:
jtulach@1334: *
jtulach@1334: * Reset the decoder via the {@link #reset reset} method, unless it
jtulach@1334: * has not been used before; Invoke the {@link #decode decode} method zero or more times, as
jtulach@1334: * long as additional input may be available, passing false for the
jtulach@1334: * endOfInput argument and filling the input buffer and flushing the
jtulach@1334: * output buffer between invocations; Invoke the {@link #decode decode} method one final time, passing
jtulach@1334: * true for the endOfInput argument; and then Invoke the {@link #flush flush} method so that the decoder can
jtulach@1334: * flush any internal state to the output buffer.
jtulach@1334: *
jtulach@1334: *
jtulach@1334: *
jtulach@1334: * Each invocation of the {@link #decode decode} method will decode as many
jtulach@1334: * bytes as possible from the input buffer, writing the resulting characters
jtulach@1334: * to the output buffer. The {@link #decode decode} method returns when more
jtulach@1334: * input is required, when there is not enough room in the output buffer, or
jtulach@1334: * when a decoding error has occurred. In each case a {@link CoderResult}
jtulach@1334: * object is returned to describe the reason for termination. An invoker can
jtulach@1334: * examine this object and fill the input buffer, flush the output buffer, or
jtulach@1334: * attempt to recover from a decoding error, as appropriate, and try again.
jtulach@1334: *
jtulach@1334: *
jtulach@1334: *
jtulach@1334: *
jtulach@1334: *
jtulach@1334: * @param averageCharsPerByte
jtulach@1334: * A positive float value indicating the expected number of
jtulach@1334: * characters that will be produced for each input byte
jtulach@1334: *
jtulach@1334: * @param maxCharsPerByte
jtulach@1334: * A positive float value indicating the maximum number of
jtulach@1334: * characters that will be produced for each input byte
jtulach@1334: *
jtulach@1334: * @throws IllegalArgumentException
jtulach@1334: * If the preconditions on the parameters do not hold
jtulach@1334: */
jtulach@1334: protected CharsetDecoder(Charset cs,
jtulach@1334: float averageCharsPerByte,
jtulach@1334: float maxCharsPerByte)
jtulach@1334: {
jtulach@1334: this(cs,
jtulach@1334: averageCharsPerByte, maxCharsPerByte,
jtulach@1334: "\uFFFD");
jtulach@1334: }
jtulach@1334:
jtulach@1334: /**
jtulach@1334: * Returns the charset that created this decoder.
This method invokes the {@link #implReplaceWith implReplaceWith} jtulach@1334: * method, passing the new replacement, after checking that the new jtulach@1334: * replacement is acceptable.
jtulach@1334: * jtulach@1334: * @param newReplacement jtulach@1334: * jtulach@1334: jtulach@1334: * The new replacement; must not be null jtulach@1334: * and must have non-zero length jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: * jtulach@1334: * @return This decoder jtulach@1334: * jtulach@1334: * @throws IllegalArgumentException jtulach@1334: * If the preconditions on the parameter do not hold jtulach@1334: */ jtulach@1334: public final CharsetDecoder replaceWith(String newReplacement) { jtulach@1334: if (newReplacement == null) jtulach@1334: throw new IllegalArgumentException("Null replacement"); jtulach@1334: int len = newReplacement.length(); jtulach@1334: if (len == 0) jtulach@1334: throw new IllegalArgumentException("Empty replacement"); jtulach@1334: if (len > maxCharsPerByte) jtulach@1334: throw new IllegalArgumentException("Replacement too long"); jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: this.replacement = newReplacement; jtulach@1334: implReplaceWith(newReplacement); jtulach@1334: return this; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Reports a change to this decoder's replacement value. jtulach@1334: * jtulach@1334: *The default implementation of this method does nothing. This method jtulach@1334: * should be overridden by decoders that require notification of changes to jtulach@1334: * the replacement.
jtulach@1334: * jtulach@1334: * @param newReplacement jtulach@1334: */ jtulach@1334: protected void implReplaceWith(String newReplacement) { jtulach@1334: } jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns this decoder's current action for malformed-input errors. jtulach@1334: * jtulach@1334: * @return The current malformed-input action, which is never null jtulach@1334: */ jtulach@1334: public CodingErrorAction malformedInputAction() { jtulach@1334: return malformedInputAction; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Changes this decoder's action for malformed-input errors. jtulach@1334: * jtulach@1334: *This method invokes the {@link #implOnMalformedInput jtulach@1334: * implOnMalformedInput} method, passing the new action.
jtulach@1334: * jtulach@1334: * @param newAction The new action; must not be null jtulach@1334: * jtulach@1334: * @return This decoder jtulach@1334: * jtulach@1334: * @throws IllegalArgumentException jtulach@1334: * If the precondition on the parameter does not hold jtulach@1334: */ jtulach@1334: public final CharsetDecoder onMalformedInput(CodingErrorAction newAction) { jtulach@1334: if (newAction == null) jtulach@1334: throw new IllegalArgumentException("Null action"); jtulach@1334: malformedInputAction = newAction; jtulach@1334: implOnMalformedInput(newAction); jtulach@1334: return this; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Reports a change to this decoder's malformed-input action. jtulach@1334: * jtulach@1334: *The default implementation of this method does nothing. This method jtulach@1334: * should be overridden by decoders that require notification of changes to jtulach@1334: * the malformed-input action.
jtulach@1334: */ jtulach@1334: protected void implOnMalformedInput(CodingErrorAction newAction) { } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns this decoder's current action for unmappable-character errors. jtulach@1334: * jtulach@1334: * jtulach@1334: * @return The current unmappable-character action, which is never jtulach@1334: * null jtulach@1334: */ jtulach@1334: public CodingErrorAction unmappableCharacterAction() { jtulach@1334: return unmappableCharacterAction; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Changes this decoder's action for unmappable-character errors. jtulach@1334: * jtulach@1334: *This method invokes the {@link #implOnUnmappableCharacter jtulach@1334: * implOnUnmappableCharacter} method, passing the new action.
jtulach@1334: * jtulach@1334: * @param newAction The new action; must not be null jtulach@1334: * jtulach@1334: * @return This decoder jtulach@1334: * jtulach@1334: * @throws IllegalArgumentException jtulach@1334: * If the precondition on the parameter does not hold jtulach@1334: */ jtulach@1334: public final CharsetDecoder onUnmappableCharacter(CodingErrorAction jtulach@1334: newAction) jtulach@1334: { jtulach@1334: if (newAction == null) jtulach@1334: throw new IllegalArgumentException("Null action"); jtulach@1334: unmappableCharacterAction = newAction; jtulach@1334: implOnUnmappableCharacter(newAction); jtulach@1334: return this; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Reports a change to this decoder's unmappable-character action. jtulach@1334: * jtulach@1334: *The default implementation of this method does nothing. This method jtulach@1334: * should be overridden by decoders that require notification of changes to jtulach@1334: * the unmappable-character action.
jtulach@1334: */ jtulach@1334: protected void implOnUnmappableCharacter(CodingErrorAction newAction) { } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the average number of characters that will be produced for each jtulach@1334: * byte of input. This heuristic value may be used to estimate the size jtulach@1334: * of the output buffer required for a given input sequence. jtulach@1334: * jtulach@1334: * @return The average number of characters produced jtulach@1334: * per byte of input jtulach@1334: */ jtulach@1334: public final float averageCharsPerByte() { jtulach@1334: return averageCharsPerByte; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the maximum number of characters that will be produced for each jtulach@1334: * byte of input. This value may be used to compute the worst-case size jtulach@1334: * of the output buffer required for a given input sequence. jtulach@1334: * jtulach@1334: * @return The maximum number of characters that will be produced per jtulach@1334: * byte of input jtulach@1334: */ jtulach@1334: public final float maxCharsPerByte() { jtulach@1334: return maxCharsPerByte; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Decodes as many bytes as possible from the given input buffer, jtulach@1334: * writing the results to the given output buffer. jtulach@1334: * jtulach@1334: *The buffers are read from, and written to, starting at their current jtulach@1334: * positions. At most {@link Buffer#remaining in.remaining()} bytes jtulach@1334: * will be read and at most {@link Buffer#remaining out.remaining()} jtulach@1334: * characters will be written. The buffers' positions will be advanced to jtulach@1334: * reflect the bytes read and the characters written, but their marks and jtulach@1334: * limits will not be modified. jtulach@1334: * jtulach@1334: *
In addition to reading bytes from the input buffer and writing jtulach@1334: * characters to the output buffer, this method returns a {@link CoderResult} jtulach@1334: * object to describe its reason for termination: jtulach@1334: * jtulach@1334: *
{@link CoderResult#UNDERFLOW} indicates that as much of the jtulach@1334: * input buffer as possible has been decoded. If there is no further jtulach@1334: * input then the invoker can proceed to the next step of the jtulach@1334: * decoding operation. Otherwise this method jtulach@1334: * should be invoked again with further input.
{@link CoderResult#OVERFLOW} indicates that there is jtulach@1334: * insufficient space in the output buffer to decode any more bytes. jtulach@1334: * This method should be invoked again with an output buffer that has jtulach@1334: * more {@linkplain Buffer#remaining remaining} characters. This is jtulach@1334: * typically done by draining any decoded characters from the output jtulach@1334: * buffer.
A {@link CoderResult#malformedForLength
jtulach@1334: * malformed-input} result indicates that a malformed-input
jtulach@1334: * error has been detected. The malformed bytes begin at the input
jtulach@1334: * buffer's (possibly incremented) position; the number of malformed
jtulach@1334: * bytes may be determined by invoking the result object's {@link
jtulach@1334: * CoderResult#length() length} method. This case applies only if the
jtulach@1334: * {@link #onMalformedInput
malformed action} of this decoder
jtulach@1334: * is {@link CodingErrorAction#REPORT}; otherwise the malformed input
jtulach@1334: * will be ignored or replaced, as requested.
An {@link CoderResult#unmappableForLength
jtulach@1334: * unmappable-character} result indicates that an
jtulach@1334: * unmappable-character error has been detected. The bytes that
jtulach@1334: * decode the unmappable character begin at the input buffer's (possibly
jtulach@1334: * incremented) position; the number of such bytes may be determined
jtulach@1334: * by invoking the result object's {@link CoderResult#length() length}
jtulach@1334: * method. This case applies only if the {@link #onUnmappableCharacter
jtulach@1334: *
unmappable action} of this decoder is {@link
jtulach@1334: * CodingErrorAction#REPORT}; otherwise the unmappable character will be
jtulach@1334: * ignored or replaced, as requested.
The endOfInput parameter advises this method as to whether jtulach@1334: * the invoker can provide further input beyond that contained in the given jtulach@1334: * input buffer. If there is a possibility of providing additional input jtulach@1334: * then the invoker should pass false for this parameter; if there jtulach@1334: * is no possibility of providing further input then the invoker should jtulach@1334: * pass true. It is not erroneous, and in fact it is quite jtulach@1334: * common, to pass false in one invocation and later discover that jtulach@1334: * no further input was actually available. It is critical, however, that jtulach@1334: * the final invocation of this method in a sequence of invocations always jtulach@1334: * pass true so that any remaining undecoded input will be treated jtulach@1334: * as being malformed. jtulach@1334: * jtulach@1334: *
This method works by invoking the {@link #decodeLoop decodeLoop} jtulach@1334: * method, interpreting its results, handling error conditions, and jtulach@1334: * reinvoking it as necessary.
jtulach@1334: * jtulach@1334: * jtulach@1334: * @param in jtulach@1334: * The input byte buffer jtulach@1334: * jtulach@1334: * @param out jtulach@1334: * The output character buffer jtulach@1334: * jtulach@1334: * @param endOfInput jtulach@1334: * true if, and only if, the invoker can provide no jtulach@1334: * additional input bytes beyond those in the given buffer jtulach@1334: * jtulach@1334: * @return A coder-result object describing the reason for termination jtulach@1334: * jtulach@1334: * @throws IllegalStateException jtulach@1334: * If a decoding operation is already in progress and the previous jtulach@1334: * step was an invocation neither of the {@link #reset reset} jtulach@1334: * method, nor of this method with a value of false for jtulach@1334: * the endOfInput parameter, nor of this method with a jtulach@1334: * value of true for the endOfInput parameter jtulach@1334: * but a return value indicating an incomplete decoding operation jtulach@1334: * jtulach@1334: * @throws CoderMalfunctionError jtulach@1334: * If an invocation of the decodeLoop method threw jtulach@1334: * an unexpected exception jtulach@1334: */ jtulach@1334: public final CoderResult decode(ByteBuffer in, CharBuffer out, jtulach@1334: boolean endOfInput) jtulach@1334: { jtulach@1334: int newState = endOfInput ? ST_END : ST_CODING; jtulach@1334: if ((state != ST_RESET) && (state != ST_CODING) jtulach@1334: && !(endOfInput && (state == ST_END))) jtulach@1334: throwIllegalStateException(state, newState); jtulach@1334: state = newState; jtulach@1334: jtulach@1334: for (;;) { jtulach@1334: jtulach@1334: CoderResult cr; jtulach@1334: try { jtulach@1334: cr = decodeLoop(in, out); jtulach@1334: } catch (BufferUnderflowException x) { jtulach@1334: throw new CoderMalfunctionError(x); jtulach@1334: } catch (BufferOverflowException x) { jtulach@1334: throw new CoderMalfunctionError(x); jtulach@1334: } jtulach@1334: jtulach@1334: if (cr.isOverflow()) jtulach@1334: return cr; jtulach@1334: jtulach@1334: if (cr.isUnderflow()) { jtulach@1334: if (endOfInput && in.hasRemaining()) { jtulach@1334: cr = CoderResult.malformedForLength(in.remaining()); jtulach@1334: // Fall through to malformed-input case jtulach@1334: } else { jtulach@1334: return cr; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: CodingErrorAction action = null; jtulach@1334: if (cr.isMalformed()) jtulach@1334: action = malformedInputAction; jtulach@1334: else if (cr.isUnmappable()) jtulach@1334: action = unmappableCharacterAction; jtulach@1334: else jtulach@1334: assert false : cr.toString(); jtulach@1334: jtulach@1334: if (action == CodingErrorAction.REPORT) jtulach@1334: return cr; jtulach@1334: jtulach@1334: if (action == CodingErrorAction.REPLACE) { jtulach@1334: if (out.remaining() < replacement.length()) jtulach@1334: return CoderResult.OVERFLOW; jtulach@1334: out.put(replacement); jtulach@1334: } jtulach@1334: jtulach@1334: if ((action == CodingErrorAction.IGNORE) jtulach@1334: || (action == CodingErrorAction.REPLACE)) { jtulach@1334: // Skip erroneous input either way jtulach@1334: in.position(in.position() + cr.length()); jtulach@1334: continue; jtulach@1334: } jtulach@1334: jtulach@1334: assert false; jtulach@1334: } jtulach@1334: jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Flushes this decoder. jtulach@1334: * jtulach@1334: *Some decoders maintain internal state and may need to write some jtulach@1334: * final characters to the output buffer once the overall input sequence has jtulach@1334: * been read. jtulach@1334: * jtulach@1334: *
Any additional output is written to the output buffer beginning at jtulach@1334: * its current position. At most {@link Buffer#remaining out.remaining()} jtulach@1334: * characters will be written. The buffer's position will be advanced jtulach@1334: * appropriately, but its mark and limit will not be modified. jtulach@1334: * jtulach@1334: *
If this method completes successfully then it returns {@link jtulach@1334: * CoderResult#UNDERFLOW}. If there is insufficient room in the output jtulach@1334: * buffer then it returns {@link CoderResult#OVERFLOW}. If this happens jtulach@1334: * then this method must be invoked again, with an output buffer that has jtulach@1334: * more room, in order to complete the current decoding jtulach@1334: * operation. jtulach@1334: * jtulach@1334: *
If this decoder has already been flushed then invoking this method jtulach@1334: * has no effect. jtulach@1334: * jtulach@1334: *
This method invokes the {@link #implFlush implFlush} method to jtulach@1334: * perform the actual flushing operation.
jtulach@1334: * jtulach@1334: * @param out jtulach@1334: * The output character buffer jtulach@1334: * jtulach@1334: * @return A coder-result object, either {@link CoderResult#UNDERFLOW} or jtulach@1334: * {@link CoderResult#OVERFLOW} jtulach@1334: * jtulach@1334: * @throws IllegalStateException jtulach@1334: * If the previous step of the current decoding operation was an jtulach@1334: * invocation neither of the {@link #flush flush} method nor of jtulach@1334: * the three-argument {@link jtulach@1334: * #decode(ByteBuffer,CharBuffer,boolean) decode} method jtulach@1334: * with a value of true for the endOfInput jtulach@1334: * parameter jtulach@1334: */ jtulach@1334: public final CoderResult flush(CharBuffer out) { jtulach@1334: if (state == ST_END) { jtulach@1334: CoderResult cr = implFlush(out); jtulach@1334: if (cr.isUnderflow()) jtulach@1334: state = ST_FLUSHED; jtulach@1334: return cr; jtulach@1334: } jtulach@1334: jtulach@1334: if (state != ST_FLUSHED) jtulach@1334: throwIllegalStateException(state, ST_FLUSHED); jtulach@1334: jtulach@1334: return CoderResult.UNDERFLOW; // Already flushed jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Flushes this decoder. jtulach@1334: * jtulach@1334: *The default implementation of this method does nothing, and always jtulach@1334: * returns {@link CoderResult#UNDERFLOW}. This method should be overridden jtulach@1334: * by decoders that may need to write final characters to the output buffer jtulach@1334: * once the entire input sequence has been read.
jtulach@1334: * jtulach@1334: * @param out jtulach@1334: * The output character buffer jtulach@1334: * jtulach@1334: * @return A coder-result object, either {@link CoderResult#UNDERFLOW} or jtulach@1334: * {@link CoderResult#OVERFLOW} jtulach@1334: */ jtulach@1334: protected CoderResult implFlush(CharBuffer out) { jtulach@1334: return CoderResult.UNDERFLOW; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Resets this decoder, clearing any internal state. jtulach@1334: * jtulach@1334: *This method resets charset-independent state and also invokes the jtulach@1334: * {@link #implReset() implReset} method in order to perform any jtulach@1334: * charset-specific reset actions.
jtulach@1334: * jtulach@1334: * @return This decoder jtulach@1334: * jtulach@1334: */ jtulach@1334: public final CharsetDecoder reset() { jtulach@1334: implReset(); jtulach@1334: state = ST_RESET; jtulach@1334: return this; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Resets this decoder, clearing any charset-specific internal state. jtulach@1334: * jtulach@1334: *The default implementation of this method does nothing. This method jtulach@1334: * should be overridden by decoders that maintain internal state.
jtulach@1334: */ jtulach@1334: protected void implReset() { } jtulach@1334: jtulach@1334: /** jtulach@1334: * Decodes one or more bytes into one or more characters. jtulach@1334: * jtulach@1334: *This method encapsulates the basic decoding loop, decoding as many jtulach@1334: * bytes as possible until it either runs out of input, runs out of room jtulach@1334: * in the output buffer, or encounters a decoding error. This method is jtulach@1334: * invoked by the {@link #decode decode} method, which handles result jtulach@1334: * interpretation and error recovery. jtulach@1334: * jtulach@1334: *
The buffers are read from, and written to, starting at their current jtulach@1334: * positions. At most {@link Buffer#remaining in.remaining()} bytes jtulach@1334: * will be read, and at most {@link Buffer#remaining out.remaining()} jtulach@1334: * characters will be written. The buffers' positions will be advanced to jtulach@1334: * reflect the bytes read and the characters written, but their marks and jtulach@1334: * limits will not be modified. jtulach@1334: * jtulach@1334: *
This method returns a {@link CoderResult} object to describe its jtulach@1334: * reason for termination, in the same manner as the {@link #decode decode} jtulach@1334: * method. Most implementations of this method will handle decoding errors jtulach@1334: * by returning an appropriate result object for interpretation by the jtulach@1334: * {@link #decode decode} method. An optimized implementation may instead jtulach@1334: * examine the relevant error action and implement that action itself. jtulach@1334: * jtulach@1334: *
An implementation of this method may perform arbitrary lookahead by jtulach@1334: * returning {@link CoderResult#UNDERFLOW} until it receives sufficient jtulach@1334: * input.
jtulach@1334: * jtulach@1334: * @param in jtulach@1334: * The input byte buffer jtulach@1334: * jtulach@1334: * @param out jtulach@1334: * The output character buffer jtulach@1334: * jtulach@1334: * @return A coder-result object describing the reason for termination jtulach@1334: */ jtulach@1334: protected abstract CoderResult decodeLoop(ByteBuffer in, jtulach@1334: CharBuffer out); jtulach@1334: jtulach@1334: /** jtulach@1334: * Convenience method that decodes the remaining content of a single input jtulach@1334: * byte buffer into a newly-allocated character buffer. jtulach@1334: * jtulach@1334: *This method implements an entire decoding jtulach@1334: * operation; that is, it resets this decoder, then it decodes the jtulach@1334: * bytes in the given byte buffer, and finally it flushes this jtulach@1334: * decoder. This method should therefore not be invoked if a decoding jtulach@1334: * operation is already in progress.
jtulach@1334: * jtulach@1334: * @param in jtulach@1334: * The input byte buffer jtulach@1334: * jtulach@1334: * @return A newly-allocated character buffer containing the result of the jtulach@1334: * decoding operation. The buffer's position will be zero and its jtulach@1334: * limit will follow the last character written. jtulach@1334: * jtulach@1334: * @throws IllegalStateException jtulach@1334: * If a decoding operation is already in progress jtulach@1334: * jtulach@1334: * @throws MalformedInputException jtulach@1334: * If the byte sequence starting at the input buffer's current jtulach@1334: * position is not legal for this charset and the current malformed-input action jtulach@1334: * is {@link CodingErrorAction#REPORT} jtulach@1334: * jtulach@1334: * @throws UnmappableCharacterException jtulach@1334: * If the byte sequence starting at the input buffer's current jtulach@1334: * position cannot be mapped to an equivalent character sequence and jtulach@1334: * the current unmappable-character action is {@link jtulach@1334: * CodingErrorAction#REPORT} jtulach@1334: */ jtulach@1334: public final CharBuffer decode(ByteBuffer in) jtulach@1334: throws CharacterCodingException jtulach@1334: { jtulach@1334: int n = (int)(in.remaining() * averageCharsPerByte()); jtulach@1334: CharBuffer out = CharBuffer.allocate(n); jtulach@1334: jtulach@1334: if ((n == 0) && (in.remaining() == 0)) jtulach@1334: return out; jtulach@1334: reset(); jtulach@1334: for (;;) { jtulach@1334: CoderResult cr = in.hasRemaining() ? jtulach@1334: decode(in, out, true) : CoderResult.UNDERFLOW; jtulach@1334: if (cr.isUnderflow()) jtulach@1334: cr = flush(out); jtulach@1334: jtulach@1334: if (cr.isUnderflow()) jtulach@1334: break; jtulach@1334: if (cr.isOverflow()) { jtulach@1334: n = 2*n + 1; // Ensure progress; n might be 0! jtulach@1334: CharBuffer o = CharBuffer.allocate(n); jtulach@1334: out.flip(); jtulach@1334: o.put(out); jtulach@1334: out = o; jtulach@1334: continue; jtulach@1334: } jtulach@1334: cr.throwException(); jtulach@1334: } jtulach@1334: out.flip(); jtulach@1334: return out; jtulach@1334: } jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: /** jtulach@1334: * Tells whether or not this decoder implements an auto-detecting charset. jtulach@1334: * jtulach@1334: *The default implementation of this method always returns jtulach@1334: * false; it should be overridden by auto-detecting decoders to jtulach@1334: * return true.
jtulach@1334: * jtulach@1334: * @return true if, and only if, this decoder implements an jtulach@1334: * auto-detecting charset jtulach@1334: */ jtulach@1334: public boolean isAutoDetecting() { jtulach@1334: return false; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Tells whether or not this decoder has yet detected a jtulach@1334: * charset (optional operation). jtulach@1334: * jtulach@1334: *If this decoder implements an auto-detecting charset then at a jtulach@1334: * single point during a decoding operation this method may start returning jtulach@1334: * true to indicate that a specific charset has been detected in jtulach@1334: * the input byte sequence. Once this occurs, the {@link #detectedCharset jtulach@1334: * detectedCharset} method may be invoked to retrieve the detected charset. jtulach@1334: * jtulach@1334: *
That this method returns false does not imply that no bytes jtulach@1334: * have yet been decoded. Some auto-detecting decoders are capable of jtulach@1334: * decoding some, or even all, of an input byte sequence without fixing on jtulach@1334: * a particular charset. jtulach@1334: * jtulach@1334: *
The default implementation of this method always throws an {@link jtulach@1334: * UnsupportedOperationException}; it should be overridden by jtulach@1334: * auto-detecting decoders to return true once the input charset jtulach@1334: * has been determined.
jtulach@1334: * jtulach@1334: * @return true if, and only if, this decoder has detected a jtulach@1334: * specific charset jtulach@1334: * jtulach@1334: * @throws UnsupportedOperationException jtulach@1334: * If this decoder does not implement an auto-detecting charset jtulach@1334: */ jtulach@1334: public boolean isCharsetDetected() { jtulach@1334: throw new UnsupportedOperationException(); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Retrieves the charset that was detected by this jtulach@1334: * decoder (optional operation). jtulach@1334: * jtulach@1334: *If this decoder implements an auto-detecting charset then this jtulach@1334: * method returns the actual charset once it has been detected. After that jtulach@1334: * point, this method returns the same value for the duration of the jtulach@1334: * current decoding operation. If not enough input bytes have yet been jtulach@1334: * read to determine the actual charset then this method throws an {@link jtulach@1334: * IllegalStateException}. jtulach@1334: * jtulach@1334: *
The default implementation of this method always throws an {@link jtulach@1334: * UnsupportedOperationException}; it should be overridden by jtulach@1334: * auto-detecting decoders to return the appropriate value.
jtulach@1334: * jtulach@1334: * @return The charset detected by this auto-detecting decoder, jtulach@1334: * or null if the charset has not yet been determined jtulach@1334: * jtulach@1334: * @throws IllegalStateException jtulach@1334: * If insufficient bytes have been read to determine a charset jtulach@1334: * jtulach@1334: * @throws UnsupportedOperationException jtulach@1334: * If this decoder does not implement an auto-detecting charset jtulach@1334: */ jtulach@1334: public Charset detectedCharset() { jtulach@1334: throw new UnsupportedOperationException(); jtulach@1334: } jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: private void throwIllegalStateException(int from, int to) { jtulach@1334: throw new IllegalStateException("Current state = " + stateNames[from] jtulach@1334: + ", new state = " + stateNames[to]); jtulach@1334: } jtulach@1334: jtulach@1334: }