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: *

    jtulach@1334: * jtulach@1334: *
  1. Reset the decoder via the {@link #reset reset} method, unless it jtulach@1334: * has not been used before;

  2. jtulach@1334: * jtulach@1334: *
  3. 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;

  4. jtulach@1334: * jtulach@1334: *
  5. Invoke the {@link #decode decode} method one final time, passing jtulach@1334: * true for the endOfInput argument; and then

  6. jtulach@1334: * jtulach@1334: *
  7. Invoke the {@link #flush flush} method so that the decoder can jtulach@1334: * flush any internal state to the output buffer.

  8. 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: *

There are two general types of decoding errors. If the input byte jtulach@1334: * sequence is not legal for this charset then the input is considered malformed. If jtulach@1334: * the input byte sequence is legal but cannot be mapped to a valid jtulach@1334: * Unicode character then an unmappable character has been encountered. jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: *

How a decoding error is handled depends upon the action requested for jtulach@1334: * that type of error, which is described by an instance of the {@link jtulach@1334: * CodingErrorAction} class. The possible error actions are to {@link jtulach@1334: * CodingErrorAction#IGNORE ignore} the erroneous input, {@link jtulach@1334: * CodingErrorAction#REPORT report} the error to the invoker via jtulach@1334: * the returned {@link CoderResult} object, or {@link CodingErrorAction#REPLACE jtulach@1334: * replace} the erroneous input with the current value of the jtulach@1334: * replacement string. The replacement jtulach@1334: * jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: jtulach@1334: * has the initial value "\uFFFD"; jtulach@1334: jtulach@1334: * jtulach@1334: * its value may be changed via the {@link #replaceWith(java.lang.String) jtulach@1334: * replaceWith} method. jtulach@1334: * jtulach@1334: *

The default action for malformed-input and unmappable-character errors jtulach@1334: * is to {@link CodingErrorAction#REPORT report} them. The jtulach@1334: * malformed-input error action may be changed via the {@link jtulach@1334: * #onMalformedInput(CodingErrorAction) onMalformedInput} method; the jtulach@1334: * unmappable-character action may be changed via the {@link jtulach@1334: * #onUnmappableCharacter(CodingErrorAction) onUnmappableCharacter} method. jtulach@1334: * jtulach@1334: *

This class is designed to handle many of the details of the decoding jtulach@1334: * process, including the implementation of error actions. A decoder for a jtulach@1334: * specific charset, which is a concrete subclass of this class, need only jtulach@1334: * implement the abstract {@link #decodeLoop decodeLoop} method, which jtulach@1334: * encapsulates the basic decoding loop. A subclass that maintains internal jtulach@1334: * state should, additionally, override the {@link #implFlush implFlush} and jtulach@1334: * {@link #implReset implReset} methods. jtulach@1334: * jtulach@1334: *

Instances of this class are not safe for use by multiple concurrent jtulach@1334: * threads.

jtulach@1334: * jtulach@1334: * jtulach@1334: * @author Mark Reinhold jtulach@1334: * @author JSR-51 Expert Group jtulach@1334: * @since 1.4 jtulach@1334: * jtulach@1334: * @see ByteBuffer jtulach@1334: * @see CharBuffer jtulach@1334: * @see Charset jtulach@1334: * @see CharsetEncoder jtulach@1334: */ jtulach@1334: jtulach@1334: public abstract class CharsetDecoder { jtulach@1334: jtulach@1334: private final Charset charset; jtulach@1334: private final float averageCharsPerByte; jtulach@1334: private final float maxCharsPerByte; jtulach@1334: jtulach@1334: private String replacement; jtulach@1334: private CodingErrorAction malformedInputAction jtulach@1334: = CodingErrorAction.REPORT; jtulach@1334: private CodingErrorAction unmappableCharacterAction jtulach@1334: = CodingErrorAction.REPORT; jtulach@1334: jtulach@1334: // Internal states jtulach@1334: // jtulach@1334: private static final int ST_RESET = 0; jtulach@1334: private static final int ST_CODING = 1; jtulach@1334: private static final int ST_END = 2; jtulach@1334: private static final int ST_FLUSHED = 3; jtulach@1334: jtulach@1334: private int state = ST_RESET; jtulach@1334: jtulach@1334: private static String stateNames[] jtulach@1334: = { "RESET", "CODING", "CODING_END", "FLUSHED" }; jtulach@1334: jtulach@1334: jtulach@1334: /** jtulach@1334: * Initializes a new decoder. The new decoder will have the given jtulach@1334: * chars-per-byte and replacement values.

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: * @param replacement jtulach@1334: * The initial replacement; must not be null, must have jtulach@1334: * non-zero length, must not be longer than maxCharsPerByte, jtulach@1334: * and must be {@link #isLegalReplacement
legal} jtulach@1334: * jtulach@1334: * @throws IllegalArgumentException jtulach@1334: * If the preconditions on the parameters do not hold jtulach@1334: */ jtulach@1334: private jtulach@1334: CharsetDecoder(Charset cs, jtulach@1334: float averageCharsPerByte, jtulach@1334: float maxCharsPerByte, jtulach@1334: String replacement) jtulach@1334: { jtulach@1334: this.charset = cs; jtulach@1334: if (averageCharsPerByte <= 0.0f) jtulach@1334: throw new IllegalArgumentException("Non-positive " jtulach@1334: + "averageCharsPerByte"); jtulach@1334: if (maxCharsPerByte <= 0.0f) jtulach@1334: throw new IllegalArgumentException("Non-positive " jtulach@1334: + "maxCharsPerByte"); jtulach@1334: if (!Charset.atBugLevel("1.4")) { jtulach@1334: if (averageCharsPerByte > maxCharsPerByte) jtulach@1334: throw new IllegalArgumentException("averageCharsPerByte" jtulach@1334: + " exceeds " jtulach@1334: + "maxCharsPerByte"); jtulach@1334: } jtulach@1334: this.replacement = replacement; jtulach@1334: this.averageCharsPerByte = averageCharsPerByte; jtulach@1334: this.maxCharsPerByte = maxCharsPerByte; jtulach@1334: replaceWith(replacement); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Initializes a new decoder. The new decoder will have the given jtulach@1334: * chars-per-byte values and its replacement will be the jtulach@1334: * string "\uFFFD".

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.

jtulach@1334: * jtulach@1334: * @return This decoder's charset jtulach@1334: */ jtulach@1334: public final Charset charset() { jtulach@1334: return charset; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns this decoder's replacement value.

jtulach@1334: * jtulach@1334: * @return This decoder's current replacement, jtulach@1334: * which is never null and is never empty jtulach@1334: */ jtulach@1334: public final String replacement() { jtulach@1334: return replacement; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Changes this decoder's replacement value. jtulach@1334: * jtulach@1334: *

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: *

jtulach@1334: * jtulach@1334: * In any case, if this method is to be reinvoked in the same decoding jtulach@1334: * operation then care should be taken to preserve any bytes remaining jtulach@1334: * in the input buffer so that they are available to the next invocation. jtulach@1334: * jtulach@1334: *

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: }