0N/A/*
2362N/A * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#warn This file is preprocessed before being compiled
0N/A
0N/Apackage java.nio.charset;
0N/A
0N/Aimport java.nio.Buffer;
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.CharBuffer;
0N/Aimport java.nio.BufferOverflowException;
0N/Aimport java.nio.BufferUnderflowException;
0N/Aimport java.lang.ref.WeakReference;
0N/Aimport java.nio.charset.CoderMalfunctionError; // javadoc
0N/A
0N/A
0N/A/**
0N/A * An engine that can transform a sequence of $itypesPhrase$ into a sequence of
0N/A * $otypesPhrase$.
0N/A *
0N/A * <a name="steps">
0N/A *
0N/A * <p> The input $itype$ sequence is provided in a $itype$ buffer or a series
0N/A * of such buffers. The output $otype$ sequence is written to a $otype$ buffer
0N/A * or a series of such buffers. $A$ $coder$ should always be used by making
0N/A * the following sequence of method invocations, hereinafter referred to as $a$
0N/A * <i>$coding$ operation</i>:
0N/A *
0N/A * <ol>
0N/A *
0N/A * <li><p> Reset the $coder$ via the {@link #reset reset} method, unless it
0N/A * has not been used before; </p></li>
0N/A *
0N/A * <li><p> Invoke the {@link #$code$ $code$} method zero or more times, as
0N/A * long as additional input may be available, passing <tt>false</tt> for the
0N/A * <tt>endOfInput</tt> argument and filling the input buffer and flushing the
0N/A * output buffer between invocations; </p></li>
0N/A *
0N/A * <li><p> Invoke the {@link #$code$ $code$} method one final time, passing
0N/A * <tt>true</tt> for the <tt>endOfInput</tt> argument; and then </p></li>
0N/A *
0N/A * <li><p> Invoke the {@link #flush flush} method so that the $coder$ can
0N/A * flush any internal state to the output buffer. </p></li>
0N/A *
0N/A * </ol>
0N/A *
0N/A * Each invocation of the {@link #$code$ $code$} method will $code$ as many
0N/A * $itype$s as possible from the input buffer, writing the resulting $otype$s
0N/A * to the output buffer. The {@link #$code$ $code$} method returns when more
0N/A * input is required, when there is not enough room in the output buffer, or
0N/A * when $a$ $coding$ error has occurred. In each case a {@link CoderResult}
0N/A * object is returned to describe the reason for termination. An invoker can
0N/A * examine this object and fill the input buffer, flush the output buffer, or
0N/A * attempt to recover from $a$ $coding$ error, as appropriate, and try again.
0N/A *
0N/A * <a name="ce">
0N/A *
0N/A * <p> There are two general types of $coding$ errors. If the input $itype$
0N/A * sequence is $notLegal$ then the input is considered <i>malformed</i>. If
0N/A * the input $itype$ sequence is legal but cannot be mapped to a valid
0N/A * $outSequence$ then an <i>unmappable character</i> has been encountered.
0N/A *
0N/A * <a name="cae">
0N/A *
0N/A * <p> How $a$ $coding$ error is handled depends upon the action requested for
0N/A * that type of error, which is described by an instance of the {@link
0N/A * CodingErrorAction} class. The possible error actions are to {@link
0N/A * CodingErrorAction#IGNORE </code>ignore<code>} the erroneous input, {@link
0N/A * CodingErrorAction#REPORT </code>report<code>} the error to the invoker via
0N/A * the returned {@link CoderResult} object, or {@link CodingErrorAction#REPLACE
0N/A * </code>replace<code>} the erroneous input with the current value of the
0N/A * replacement $replTypeName$. The replacement
0N/A *
0N/A#if[encoder]
0N/A * is initially set to the $coder$'s default replacement, which often
0N/A * (but not always) has the initial value&nbsp;$defaultReplName$;
0N/A#end[encoder]
0N/A#if[decoder]
0N/A * has the initial value $defaultReplName$;
0N/A#end[decoder]
0N/A *
0N/A * its value may be changed via the {@link #replaceWith($replFQType$)
0N/A * replaceWith} method.
0N/A *
0N/A * <p> The default action for malformed-input and unmappable-character errors
0N/A * is to {@link CodingErrorAction#REPORT </code>report<code>} them. The
0N/A * malformed-input error action may be changed via the {@link
0N/A * #onMalformedInput(CodingErrorAction) onMalformedInput} method; the
0N/A * unmappable-character action may be changed via the {@link
0N/A * #onUnmappableCharacter(CodingErrorAction) onUnmappableCharacter} method.
0N/A *
0N/A * <p> This class is designed to handle many of the details of the $coding$
0N/A * process, including the implementation of error actions. $A$ $coder$ for a
0N/A * specific charset, which is a concrete subclass of this class, need only
0N/A * implement the abstract {@link #$code$Loop $code$Loop} method, which
0N/A * encapsulates the basic $coding$ loop. A subclass that maintains internal
0N/A * state should, additionally, override the {@link #implFlush implFlush} and
0N/A * {@link #implReset implReset} methods.
0N/A *
0N/A * <p> Instances of this class are not safe for use by multiple concurrent
0N/A * threads. </p>
0N/A *
0N/A *
0N/A * @author Mark Reinhold
0N/A * @author JSR-51 Expert Group
0N/A * @since 1.4
0N/A *
0N/A * @see ByteBuffer
0N/A * @see CharBuffer
0N/A * @see Charset
0N/A * @see Charset$OtherCoder$
0N/A */
0N/A
0N/Apublic abstract class Charset$Coder$ {
0N/A
0N/A private final Charset charset;
0N/A private final float average$ItypesPerOtype$;
0N/A private final float max$ItypesPerOtype$;
0N/A
0N/A private $replType$ replacement;
0N/A private CodingErrorAction malformedInputAction
0N/A = CodingErrorAction.REPORT;
0N/A private CodingErrorAction unmappableCharacterAction
0N/A = CodingErrorAction.REPORT;
0N/A
0N/A // Internal states
0N/A //
0N/A private static final int ST_RESET = 0;
0N/A private static final int ST_CODING = 1;
0N/A private static final int ST_END = 2;
0N/A private static final int ST_FLUSHED = 3;
0N/A
0N/A private int state = ST_RESET;
0N/A
0N/A private static String stateNames[]
0N/A = { "RESET", "CODING", "CODING_END", "FLUSHED" };
0N/A
0N/A
0N/A /**
0N/A * Initializes a new $coder$. The new $coder$ will have the given
0N/A * $otypes-per-itype$ and replacement values. </p>
0N/A *
0N/A * @param average$ItypesPerOtype$
0N/A * A positive float value indicating the expected number of
0N/A * $otype$s that will be produced for each input $itype$
0N/A *
0N/A * @param max$ItypesPerOtype$
0N/A * A positive float value indicating the maximum number of
0N/A * $otype$s that will be produced for each input $itype$
0N/A *
0N/A * @param replacement
0N/A * The initial replacement; must not be <tt>null</tt>, must have
0N/A * non-zero length, must not be longer than max$ItypesPerOtype$,
0N/A * and must be {@link #isLegalReplacement </code>legal<code>}
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If the preconditions on the parameters do not hold
0N/A */
0N/A {#if[encoder]?protected:private}
0N/A Charset$Coder$(Charset cs,
0N/A float average$ItypesPerOtype$,
0N/A float max$ItypesPerOtype$,
0N/A $replType$ replacement)
0N/A {
0N/A this.charset = cs;
0N/A if (average$ItypesPerOtype$ <= 0.0f)
0N/A throw new IllegalArgumentException("Non-positive "
0N/A + "average$ItypesPerOtype$");
0N/A if (max$ItypesPerOtype$ <= 0.0f)
0N/A throw new IllegalArgumentException("Non-positive "
0N/A + "max$ItypesPerOtype$");
0N/A if (!Charset.atBugLevel("1.4")) {
0N/A if (average$ItypesPerOtype$ > max$ItypesPerOtype$)
0N/A throw new IllegalArgumentException("average$ItypesPerOtype$"
0N/A + " exceeds "
0N/A + "max$ItypesPerOtype$");
0N/A }
0N/A this.replacement = replacement;
0N/A this.average$ItypesPerOtype$ = average$ItypesPerOtype$;
0N/A this.max$ItypesPerOtype$ = max$ItypesPerOtype$;
0N/A replaceWith(replacement);
0N/A }
0N/A
0N/A /**
0N/A * Initializes a new $coder$. The new $coder$ will have the given
0N/A * $otypes-per-itype$ values and its replacement will be the
0N/A * $replTypeName$ $defaultReplName$. </p>
0N/A *
0N/A * @param average$ItypesPerOtype$
0N/A * A positive float value indicating the expected number of
0N/A * $otype$s that will be produced for each input $itype$
0N/A *
0N/A * @param max$ItypesPerOtype$
0N/A * A positive float value indicating the maximum number of
0N/A * $otype$s that will be produced for each input $itype$
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If the preconditions on the parameters do not hold
0N/A */
0N/A protected Charset$Coder$(Charset cs,
0N/A float average$ItypesPerOtype$,
0N/A float max$ItypesPerOtype$)
0N/A {
0N/A this(cs,
0N/A average$ItypesPerOtype$, max$ItypesPerOtype$,
0N/A $defaultRepl$);
0N/A }
0N/A
0N/A /**
0N/A * Returns the charset that created this $coder$. </p>
0N/A *
0N/A * @return This $coder$'s charset
0N/A */
0N/A public final Charset charset() {
0N/A return charset;
0N/A }
0N/A
0N/A /**
0N/A * Returns this $coder$'s replacement value. </p>
0N/A *
0N/A * @return This $coder$'s current replacement,
0N/A * which is never <tt>null</tt> and is never empty
0N/A */
0N/A public final $replType$ replacement() {
0N/A return replacement;
0N/A }
0N/A
0N/A /**
0N/A * Changes this $coder$'s replacement value.
0N/A *
0N/A * <p> This method invokes the {@link #implReplaceWith implReplaceWith}
0N/A * method, passing the new replacement, after checking that the new
0N/A * replacement is acceptable. </p>
0N/A *
0N/A * @param newReplacement
0N/A *
0N/A#if[decoder]
0N/A * The new replacement; must not be <tt>null</tt>
0N/A * and must have non-zero length
0N/A#end[decoder]
0N/A#if[encoder]
0N/A * The new replacement; must not be <tt>null</tt>, must have
0N/A * non-zero length, must not be longer than the value returned by
0N/A * the {@link #max$ItypesPerOtype$() max$ItypesPerOtype$} method, and
0N/A * must be {@link #isLegalReplacement </code>legal<code>}
0N/A#end[encoder]
0N/A *
0N/A * @return This $coder$
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If the preconditions on the parameter do not hold
0N/A */
0N/A public final Charset$Coder$ replaceWith($replType$ newReplacement) {
0N/A if (newReplacement == null)
0N/A throw new IllegalArgumentException("Null replacement");
0N/A int len = newReplacement.$replLength$;
0N/A if (len == 0)
0N/A throw new IllegalArgumentException("Empty replacement");
0N/A if (len > max$ItypesPerOtype$)
0N/A throw new IllegalArgumentException("Replacement too long");
0N/A#if[encoder]
0N/A if (!isLegalReplacement(newReplacement))
0N/A throw new IllegalArgumentException("Illegal replacement");
0N/A#end[encoder]
0N/A this.replacement = newReplacement;
0N/A implReplaceWith(newReplacement);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Reports a change to this $coder$'s replacement value.
0N/A *
0N/A * <p> The default implementation of this method does nothing. This method
0N/A * should be overridden by $coder$s that require notification of changes to
0N/A * the replacement. </p>
0N/A *
0N/A * @param newReplacement
0N/A */
0N/A protected void implReplaceWith($replType$ newReplacement) {
0N/A }
0N/A
0N/A#if[encoder]
0N/A
438N/A private WeakReference<CharsetDecoder> cachedDecoder = null;
0N/A
0N/A /**
0N/A * Tells whether or not the given byte array is a legal replacement value
0N/A * for this encoder.
0N/A *
0N/A * <p> A replacement is legal if, and only if, it is a legal sequence of
0N/A * bytes in this encoder's charset; that is, it must be possible to decode
0N/A * the replacement into one or more sixteen-bit Unicode characters.
0N/A *
0N/A * <p> The default implementation of this method is not very efficient; it
0N/A * should generally be overridden to improve performance. </p>
0N/A *
0N/A * @param repl The byte array to be tested
0N/A *
0N/A * @return <tt>true</tt> if, and only if, the given byte array
0N/A * is a legal replacement value for this encoder
0N/A */
0N/A public boolean isLegalReplacement(byte[] repl) {
438N/A WeakReference<CharsetDecoder> wr = cachedDecoder;
0N/A CharsetDecoder dec = null;
438N/A if ((wr == null) || ((dec = wr.get()) == null)) {
0N/A dec = charset().newDecoder();
0N/A dec.onMalformedInput(CodingErrorAction.REPORT);
0N/A dec.onUnmappableCharacter(CodingErrorAction.REPORT);
438N/A cachedDecoder = new WeakReference<CharsetDecoder>(dec);
0N/A } else {
0N/A dec.reset();
0N/A }
0N/A ByteBuffer bb = ByteBuffer.wrap(repl);
0N/A CharBuffer cb = CharBuffer.allocate((int)(bb.remaining()
0N/A * dec.maxCharsPerByte()));
0N/A CoderResult cr = dec.decode(bb, cb, true);
0N/A return !cr.isError();
0N/A }
0N/A
0N/A#end[encoder]
0N/A
0N/A /**
0N/A * Returns this $coder$'s current action for malformed-input errors. </p>
0N/A *
0N/A * @return The current malformed-input action, which is never <tt>null</tt>
0N/A */
0N/A public CodingErrorAction malformedInputAction() {
0N/A return malformedInputAction;
0N/A }
0N/A
0N/A /**
0N/A * Changes this $coder$'s action for malformed-input errors. </p>
0N/A *
0N/A * <p> This method invokes the {@link #implOnMalformedInput
0N/A * implOnMalformedInput} method, passing the new action. </p>
0N/A *
0N/A * @param newAction The new action; must not be <tt>null</tt>
0N/A *
0N/A * @return This $coder$
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If the precondition on the parameter does not hold
0N/A */
0N/A public final Charset$Coder$ onMalformedInput(CodingErrorAction newAction) {
0N/A if (newAction == null)
0N/A throw new IllegalArgumentException("Null action");
0N/A malformedInputAction = newAction;
0N/A implOnMalformedInput(newAction);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Reports a change to this $coder$'s malformed-input action.
0N/A *
0N/A * <p> The default implementation of this method does nothing. This method
0N/A * should be overridden by $coder$s that require notification of changes to
0N/A * the malformed-input action. </p>
0N/A */
0N/A protected void implOnMalformedInput(CodingErrorAction newAction) { }
0N/A
0N/A /**
0N/A * Returns this $coder$'s current action for unmappable-character errors.
0N/A * </p>
0N/A *
0N/A * @return The current unmappable-character action, which is never
0N/A * <tt>null</tt>
0N/A */
0N/A public CodingErrorAction unmappableCharacterAction() {
0N/A return unmappableCharacterAction;
0N/A }
0N/A
0N/A /**
0N/A * Changes this $coder$'s action for unmappable-character errors.
0N/A *
0N/A * <p> This method invokes the {@link #implOnUnmappableCharacter
0N/A * implOnUnmappableCharacter} method, passing the new action. </p>
0N/A *
0N/A * @param newAction The new action; must not be <tt>null</tt>
0N/A *
0N/A * @return This $coder$
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If the precondition on the parameter does not hold
0N/A */
0N/A public final Charset$Coder$ onUnmappableCharacter(CodingErrorAction
0N/A newAction)
0N/A {
0N/A if (newAction == null)
0N/A throw new IllegalArgumentException("Null action");
0N/A unmappableCharacterAction = newAction;
0N/A implOnUnmappableCharacter(newAction);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Reports a change to this $coder$'s unmappable-character action.
0N/A *
0N/A * <p> The default implementation of this method does nothing. This method
0N/A * should be overridden by $coder$s that require notification of changes to
0N/A * the unmappable-character action. </p>
0N/A */
0N/A protected void implOnUnmappableCharacter(CodingErrorAction newAction) { }
0N/A
0N/A /**
0N/A * Returns the average number of $otype$s that will be produced for each
0N/A * $itype$ of input. This heuristic value may be used to estimate the size
0N/A * of the output buffer required for a given input sequence. </p>
0N/A *
0N/A * @return The average number of $otype$s produced
0N/A * per $itype$ of input
0N/A */
0N/A public final float average$ItypesPerOtype$() {
0N/A return average$ItypesPerOtype$;
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum number of $otype$s that will be produced for each
0N/A * $itype$ of input. This value may be used to compute the worst-case size
0N/A * of the output buffer required for a given input sequence. </p>
0N/A *
0N/A * @return The maximum number of $otype$s that will be produced per
0N/A * $itype$ of input
0N/A */
0N/A public final float max$ItypesPerOtype$() {
0N/A return max$ItypesPerOtype$;
0N/A }
0N/A
0N/A /**
0N/A * $Code$s as many $itype$s as possible from the given input buffer,
0N/A * writing the results to the given output buffer.
0N/A *
0N/A * <p> The buffers are read from, and written to, starting at their current
0N/A * positions. At most {@link Buffer#remaining in.remaining()} $itype$s
0N/A * will be read and at most {@link Buffer#remaining out.remaining()}
0N/A * $otype$s will be written. The buffers' positions will be advanced to
0N/A * reflect the $itype$s read and the $otype$s written, but their marks and
0N/A * limits will not be modified.
0N/A *
0N/A * <p> In addition to reading $itype$s from the input buffer and writing
0N/A * $otype$s to the output buffer, this method returns a {@link CoderResult}
0N/A * object to describe its reason for termination:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li><p> {@link CoderResult#UNDERFLOW} indicates that as much of the
0N/A * input buffer as possible has been $code$d. If there is no further
0N/A * input then the invoker can proceed to the next step of the
0N/A * <a href="#steps">$coding$ operation</a>. Otherwise this method
0N/A * should be invoked again with further input. </p></li>
0N/A *
0N/A * <li><p> {@link CoderResult#OVERFLOW} indicates that there is
0N/A * insufficient space in the output buffer to $code$ any more $itype$s.
0N/A * This method should be invoked again with an output buffer that has
0N/A * more {@linkplain Buffer#remaining remaining} $otype$s. This is
0N/A * typically done by draining any $code$d $otype$s from the output
0N/A * buffer. </p></li>
0N/A *
0N/A * <li><p> A {@link CoderResult#malformedForLength
0N/A * </code>malformed-input<code>} result indicates that a malformed-input
0N/A * error has been detected. The malformed $itype$s begin at the input
0N/A * buffer's (possibly incremented) position; the number of malformed
0N/A * $itype$s may be determined by invoking the result object's {@link
0N/A * CoderResult#length() length} method. This case applies only if the
0N/A * {@link #onMalformedInput </code>malformed action<code>} of this $coder$
0N/A * is {@link CodingErrorAction#REPORT}; otherwise the malformed input
0N/A * will be ignored or replaced, as requested. </p></li>
0N/A *
0N/A * <li><p> An {@link CoderResult#unmappableForLength
0N/A * </code>unmappable-character<code>} result indicates that an
0N/A * unmappable-character error has been detected. The $itype$s that
0N/A * $code$ the unmappable character begin at the input buffer's (possibly
0N/A * incremented) position; the number of such $itype$s may be determined
0N/A * by invoking the result object's {@link CoderResult#length() length}
0N/A * method. This case applies only if the {@link #onUnmappableCharacter
0N/A * </code>unmappable action<code>} of this $coder$ is {@link
0N/A * CodingErrorAction#REPORT}; otherwise the unmappable character will be
0N/A * ignored or replaced, as requested. </p></li>
0N/A *
0N/A * </ul>
0N/A *
0N/A * In any case, if this method is to be reinvoked in the same $coding$
0N/A * operation then care should be taken to preserve any $itype$s remaining
0N/A * in the input buffer so that they are available to the next invocation.
0N/A *
0N/A * <p> The <tt>endOfInput</tt> parameter advises this method as to whether
0N/A * the invoker can provide further input beyond that contained in the given
0N/A * input buffer. If there is a possibility of providing additional input
0N/A * then the invoker should pass <tt>false</tt> for this parameter; if there
0N/A * is no possibility of providing further input then the invoker should
0N/A * pass <tt>true</tt>. It is not erroneous, and in fact it is quite
0N/A * common, to pass <tt>false</tt> in one invocation and later discover that
0N/A * no further input was actually available. It is critical, however, that
0N/A * the final invocation of this method in a sequence of invocations always
0N/A * pass <tt>true</tt> so that any remaining un$code$d input will be treated
0N/A * as being malformed.
0N/A *
0N/A * <p> This method works by invoking the {@link #$code$Loop $code$Loop}
0N/A * method, interpreting its results, handling error conditions, and
0N/A * reinvoking it as necessary. </p>
0N/A *
0N/A *
0N/A * @param in
0N/A * The input $itype$ buffer
0N/A *
0N/A * @param out
0N/A * The output $otype$ buffer
0N/A *
0N/A * @param endOfInput
0N/A * <tt>true</tt> if, and only if, the invoker can provide no
0N/A * additional input $itype$s beyond those in the given buffer
0N/A *
0N/A * @return A coder-result object describing the reason for termination
0N/A *
0N/A * @throws IllegalStateException
0N/A * If $a$ $coding$ operation is already in progress and the previous
0N/A * step was an invocation neither of the {@link #reset reset}
0N/A * method, nor of this method with a value of <tt>false</tt> for
0N/A * the <tt>endOfInput</tt> parameter, nor of this method with a
0N/A * value of <tt>true</tt> for the <tt>endOfInput</tt> parameter
0N/A * but a return value indicating an incomplete $coding$ operation
0N/A *
0N/A * @throws CoderMalfunctionError
0N/A * If an invocation of the $code$Loop method threw
0N/A * an unexpected exception
0N/A */
0N/A public final CoderResult $code$($Itype$Buffer in, $Otype$Buffer out,
0N/A boolean endOfInput)
0N/A {
0N/A int newState = endOfInput ? ST_END : ST_CODING;
0N/A if ((state != ST_RESET) && (state != ST_CODING)
0N/A && !(endOfInput && (state == ST_END)))
0N/A throwIllegalStateException(state, newState);
0N/A state = newState;
0N/A
0N/A for (;;) {
0N/A
0N/A CoderResult cr;
0N/A try {
0N/A cr = $code$Loop(in, out);
0N/A } catch (BufferUnderflowException x) {
0N/A throw new CoderMalfunctionError(x);
0N/A } catch (BufferOverflowException x) {
0N/A throw new CoderMalfunctionError(x);
0N/A }
0N/A
0N/A if (cr.isOverflow())
0N/A return cr;
0N/A
0N/A if (cr.isUnderflow()) {
0N/A if (endOfInput && in.hasRemaining()) {
0N/A cr = CoderResult.malformedForLength(in.remaining());
0N/A // Fall through to malformed-input case
0N/A } else {
0N/A return cr;
0N/A }
0N/A }
0N/A
0N/A CodingErrorAction action = null;
0N/A if (cr.isMalformed())
0N/A action = malformedInputAction;
0N/A else if (cr.isUnmappable())
0N/A action = unmappableCharacterAction;
0N/A else
0N/A assert false : cr.toString();
0N/A
0N/A if (action == CodingErrorAction.REPORT)
0N/A return cr;
0N/A
0N/A if (action == CodingErrorAction.REPLACE) {
0N/A if (out.remaining() < replacement.$replLength$)
0N/A return CoderResult.OVERFLOW;
0N/A out.put(replacement);
0N/A }
0N/A
0N/A if ((action == CodingErrorAction.IGNORE)
0N/A || (action == CodingErrorAction.REPLACE)) {
0N/A // Skip erroneous input either way
0N/A in.position(in.position() + cr.length());
0N/A continue;
0N/A }
0N/A
0N/A assert false;
0N/A }
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Flushes this $coder$.
0N/A *
0N/A * <p> Some $coder$s maintain internal state and may need to write some
0N/A * final $otype$s to the output buffer once the overall input sequence has
0N/A * been read.
0N/A *
0N/A * <p> Any additional output is written to the output buffer beginning at
0N/A * its current position. At most {@link Buffer#remaining out.remaining()}
0N/A * $otype$s will be written. The buffer's position will be advanced
0N/A * appropriately, but its mark and limit will not be modified.
0N/A *
0N/A * <p> If this method completes successfully then it returns {@link
0N/A * CoderResult#UNDERFLOW}. If there is insufficient room in the output
0N/A * buffer then it returns {@link CoderResult#OVERFLOW}. If this happens
0N/A * then this method must be invoked again, with an output buffer that has
0N/A * more room, in order to complete the current <a href="#steps">$coding$
0N/A * operation</a>.
0N/A *
0N/A * <p> If this $coder$ has already been flushed then invoking this method
0N/A * has no effect.
0N/A *
0N/A * <p> This method invokes the {@link #implFlush implFlush} method to
0N/A * perform the actual flushing operation. </p>
0N/A *
0N/A * @param out
0N/A * The output $otype$ buffer
0N/A *
0N/A * @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
0N/A * {@link CoderResult#OVERFLOW}
0N/A *
0N/A * @throws IllegalStateException
0N/A * If the previous step of the current $coding$ operation was an
0N/A * invocation neither of the {@link #flush flush} method nor of
0N/A * the three-argument {@link
0N/A * #$code$($Itype$Buffer,$Otype$Buffer,boolean) $code$} method
0N/A * with a value of <tt>true</tt> for the <tt>endOfInput</tt>
0N/A * parameter
0N/A */
0N/A public final CoderResult flush($Otype$Buffer out) {
0N/A if (state == ST_END) {
0N/A CoderResult cr = implFlush(out);
0N/A if (cr.isUnderflow())
0N/A state = ST_FLUSHED;
0N/A return cr;
0N/A }
0N/A
0N/A if (state != ST_FLUSHED)
0N/A throwIllegalStateException(state, ST_FLUSHED);
0N/A
0N/A return CoderResult.UNDERFLOW; // Already flushed
0N/A }
0N/A
0N/A /**
0N/A * Flushes this $coder$.
0N/A *
0N/A * <p> The default implementation of this method does nothing, and always
0N/A * returns {@link CoderResult#UNDERFLOW}. This method should be overridden
0N/A * by $coder$s that may need to write final $otype$s to the output buffer
0N/A * once the entire input sequence has been read. </p>
0N/A *
0N/A * @param out
0N/A * The output $otype$ buffer
0N/A *
0N/A * @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
0N/A * {@link CoderResult#OVERFLOW}
0N/A */
0N/A protected CoderResult implFlush($Otype$Buffer out) {
0N/A return CoderResult.UNDERFLOW;
0N/A }
0N/A
0N/A /**
0N/A * Resets this $coder$, clearing any internal state.
0N/A *
0N/A * <p> This method resets charset-independent state and also invokes the
0N/A * {@link #implReset() implReset} method in order to perform any
0N/A * charset-specific reset actions. </p>
0N/A *
0N/A * @return This $coder$
0N/A *
0N/A */
0N/A public final Charset$Coder$ reset() {
0N/A implReset();
0N/A state = ST_RESET;
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Resets this $coder$, clearing any charset-specific internal state.
0N/A *
0N/A * <p> The default implementation of this method does nothing. This method
0N/A * should be overridden by $coder$s that maintain internal state. </p>
0N/A */
0N/A protected void implReset() { }
0N/A
0N/A /**
0N/A * $Code$s one or more $itype$s into one or more $otype$s.
0N/A *
0N/A * <p> This method encapsulates the basic $coding$ loop, $coding$ as many
0N/A * $itype$s as possible until it either runs out of input, runs out of room
0N/A * in the output buffer, or encounters $a$ $coding$ error. This method is
0N/A * invoked by the {@link #$code$ $code$} method, which handles result
0N/A * interpretation and error recovery.
0N/A *
0N/A * <p> The buffers are read from, and written to, starting at their current
0N/A * positions. At most {@link Buffer#remaining in.remaining()} $itype$s
0N/A * will be read, and at most {@link Buffer#remaining out.remaining()}
0N/A * $otype$s will be written. The buffers' positions will be advanced to
0N/A * reflect the $itype$s read and the $otype$s written, but their marks and
0N/A * limits will not be modified.
0N/A *
0N/A * <p> This method returns a {@link CoderResult} object to describe its
0N/A * reason for termination, in the same manner as the {@link #$code$ $code$}
0N/A * method. Most implementations of this method will handle $coding$ errors
0N/A * by returning an appropriate result object for interpretation by the
0N/A * {@link #$code$ $code$} method. An optimized implementation may instead
0N/A * examine the relevant error action and implement that action itself.
0N/A *
0N/A * <p> An implementation of this method may perform arbitrary lookahead by
0N/A * returning {@link CoderResult#UNDERFLOW} until it receives sufficient
0N/A * input. </p>
0N/A *
0N/A * @param in
0N/A * The input $itype$ buffer
0N/A *
0N/A * @param out
0N/A * The output $otype$ buffer
0N/A *
0N/A * @return A coder-result object describing the reason for termination
0N/A */
0N/A protected abstract CoderResult $code$Loop($Itype$Buffer in,
0N/A $Otype$Buffer out);
0N/A
0N/A /**
0N/A * Convenience method that $code$s the remaining content of a single input
0N/A * $itype$ buffer into a newly-allocated $otype$ buffer.
0N/A *
0N/A * <p> This method implements an entire <a href="#steps">$coding$
0N/A * operation</a>; that is, it resets this $coder$, then it $code$s the
0N/A * $itype$s in the given $itype$ buffer, and finally it flushes this
0N/A * $coder$. This method should therefore not be invoked if $a$ $coding$
0N/A * operation is already in progress. </p>
0N/A *
0N/A * @param in
0N/A * The input $itype$ buffer
0N/A *
0N/A * @return A newly-allocated $otype$ buffer containing the result of the
0N/A * $coding$ operation. The buffer's position will be zero and its
0N/A * limit will follow the last $otype$ written.
0N/A *
0N/A * @throws IllegalStateException
0N/A * If $a$ $coding$ operation is already in progress
0N/A *
0N/A * @throws MalformedInputException
0N/A * If the $itype$ sequence starting at the input buffer's current
0N/A * position is $notLegal$ and the current malformed-input action
0N/A * is {@link CodingErrorAction#REPORT}
0N/A *
0N/A * @throws UnmappableCharacterException
0N/A * If the $itype$ sequence starting at the input buffer's current
0N/A * position cannot be mapped to an equivalent $otype$ sequence and
0N/A * the current unmappable-character action is {@link
0N/A * CodingErrorAction#REPORT}
0N/A */
0N/A public final $Otype$Buffer $code$($Itype$Buffer in)
0N/A throws CharacterCodingException
0N/A {
0N/A int n = (int)(in.remaining() * average$ItypesPerOtype$());
0N/A $Otype$Buffer out = $Otype$Buffer.allocate(n);
0N/A
0N/A if ((n == 0) && (in.remaining() == 0))
0N/A return out;
0N/A reset();
0N/A for (;;) {
0N/A CoderResult cr = in.hasRemaining() ?
0N/A $code$(in, out, true) : CoderResult.UNDERFLOW;
0N/A if (cr.isUnderflow())
0N/A cr = flush(out);
0N/A
0N/A if (cr.isUnderflow())
0N/A break;
0N/A if (cr.isOverflow()) {
0N/A n = 2*n + 1; // Ensure progress; n might be 0!
0N/A $Otype$Buffer o = $Otype$Buffer.allocate(n);
0N/A out.flip();
0N/A o.put(out);
0N/A out = o;
0N/A continue;
0N/A }
0N/A cr.throwException();
0N/A }
0N/A out.flip();
0N/A return out;
0N/A }
0N/A
0N/A#if[decoder]
0N/A
0N/A /**
0N/A * Tells whether or not this decoder implements an auto-detecting charset.
0N/A *
0N/A * <p> The default implementation of this method always returns
0N/A * <tt>false</tt>; it should be overridden by auto-detecting decoders to
0N/A * return <tt>true</tt>. </p>
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this decoder implements an
0N/A * auto-detecting charset
0N/A */
0N/A public boolean isAutoDetecting() {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Tells whether or not this decoder has yet detected a
0N/A * charset&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
0N/A * <p> If this decoder implements an auto-detecting charset then at a
0N/A * single point during a decoding operation this method may start returning
0N/A * <tt>true</tt> to indicate that a specific charset has been detected in
0N/A * the input byte sequence. Once this occurs, the {@link #detectedCharset
0N/A * detectedCharset} method may be invoked to retrieve the detected charset.
0N/A *
0N/A * <p> That this method returns <tt>false</tt> does not imply that no bytes
0N/A * have yet been decoded. Some auto-detecting decoders are capable of
0N/A * decoding some, or even all, of an input byte sequence without fixing on
0N/A * a particular charset.
0N/A *
0N/A * <p> The default implementation of this method always throws an {@link
0N/A * UnsupportedOperationException}; it should be overridden by
0N/A * auto-detecting decoders to return <tt>true</tt> once the input charset
0N/A * has been determined. </p>
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this decoder has detected a
0N/A * specific charset
0N/A *
0N/A * @throws UnsupportedOperationException
0N/A * If this decoder does not implement an auto-detecting charset
0N/A */
0N/A public boolean isCharsetDetected() {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the charset that was detected by this
0N/A * decoder&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
0N/A * <p> If this decoder implements an auto-detecting charset then this
0N/A * method returns the actual charset once it has been detected. After that
0N/A * point, this method returns the same value for the duration of the
0N/A * current decoding operation. If not enough input bytes have yet been
0N/A * read to determine the actual charset then this method throws an {@link
0N/A * IllegalStateException}.
0N/A *
0N/A * <p> The default implementation of this method always throws an {@link
0N/A * UnsupportedOperationException}; it should be overridden by
0N/A * auto-detecting decoders to return the appropriate value. </p>
0N/A *
0N/A * @return The charset detected by this auto-detecting decoder,
0N/A * or <tt>null</tt> if the charset has not yet been determined
0N/A *
0N/A * @throws IllegalStateException
0N/A * If insufficient bytes have been read to determine a charset
0N/A *
0N/A * @throws UnsupportedOperationException
0N/A * If this decoder does not implement an auto-detecting charset
0N/A */
0N/A public Charset detectedCharset() {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A#end[decoder]
0N/A
0N/A#if[encoder]
0N/A
0N/A private boolean canEncode(CharBuffer cb) {
0N/A if (state == ST_FLUSHED)
0N/A reset();
0N/A else if (state != ST_RESET)
0N/A throwIllegalStateException(state, ST_CODING);
0N/A CodingErrorAction ma = malformedInputAction();
0N/A CodingErrorAction ua = unmappableCharacterAction();
0N/A try {
0N/A onMalformedInput(CodingErrorAction.REPORT);
0N/A onUnmappableCharacter(CodingErrorAction.REPORT);
0N/A encode(cb);
0N/A } catch (CharacterCodingException x) {
0N/A return false;
0N/A } finally {
0N/A onMalformedInput(ma);
0N/A onUnmappableCharacter(ua);
0N/A reset();
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Tells whether or not this encoder can encode the given character.
0N/A *
0N/A * <p> This method returns <tt>false</tt> if the given character is a
0N/A * surrogate character; such characters can be interpreted only when they
0N/A * are members of a pair consisting of a high surrogate followed by a low
0N/A * surrogate. The {@link #canEncode(java.lang.CharSequence)
0N/A * canEncode(CharSequence)} method may be used to test whether or not a
0N/A * character sequence can be encoded.
0N/A *
0N/A * <p> This method may modify this encoder's state; it should therefore not
0N/A * be invoked if an <a href="#steps">encoding operation</a> is already in
0N/A * progress.
0N/A *
0N/A * <p> The default implementation of this method is not very efficient; it
0N/A * should generally be overridden to improve performance. </p>
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this encoder can encode
0N/A * the given character
0N/A *
0N/A * @throws IllegalStateException
0N/A * If $a$ $coding$ operation is already in progress
0N/A */
0N/A public boolean canEncode(char c) {
0N/A CharBuffer cb = CharBuffer.allocate(1);
0N/A cb.put(c);
0N/A cb.flip();
0N/A return canEncode(cb);
0N/A }
0N/A
0N/A /**
0N/A * Tells whether or not this encoder can encode the given character
0N/A * sequence.
0N/A *
0N/A * <p> If this method returns <tt>false</tt> for a particular character
0N/A * sequence then more information about why the sequence cannot be encoded
0N/A * may be obtained by performing a full <a href="#steps">encoding
0N/A * operation</a>.
0N/A *
0N/A * <p> This method may modify this encoder's state; it should therefore not
0N/A * be invoked if an encoding operation is already in progress.
0N/A *
0N/A * <p> The default implementation of this method is not very efficient; it
0N/A * should generally be overridden to improve performance. </p>
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this encoder can encode
0N/A * the given character without throwing any exceptions and without
0N/A * performing any replacements
0N/A *
0N/A * @throws IllegalStateException
0N/A * If $a$ $coding$ operation is already in progress
0N/A */
0N/A public boolean canEncode(CharSequence cs) {
0N/A CharBuffer cb;
0N/A if (cs instanceof CharBuffer)
0N/A cb = ((CharBuffer)cs).duplicate();
0N/A else
0N/A cb = CharBuffer.wrap(cs.toString());
0N/A return canEncode(cb);
0N/A }
0N/A
0N/A#end[encoder]
0N/A
0N/A
0N/A private void throwIllegalStateException(int from, int to) {
0N/A throw new IllegalStateException("Current state = " + stateNames[from]
0N/A + ", new state = " + stateNames[to]);
0N/A }
0N/A
0N/A}