jdk/src/share/classes/java/nio/charset/Charset-X-Coder.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 895 67f1dc69ad10
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#warn This file is preprocessed before being compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package java.nio.charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.Buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.BufferOverflowException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.BufferUnderflowException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.charset.CoderMalfunctionError;                  // javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * An engine that can transform a sequence of $itypesPhrase$ into a sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * $otypesPhrase$.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <a name="steps">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> The input $itype$ sequence is provided in a $itype$ buffer or a series
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * of such buffers.  The output $otype$ sequence is written to a $otype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * or a series of such buffers.  $A$ $coder$ should always be used by making
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the following sequence of method invocations, hereinafter referred to as $a$
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <i>$coding$ operation</i>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   <li><p> Reset the $coder$ via the {@link #reset reset} method, unless it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   has not been used before; </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   <li><p> Invoke the {@link #$code$ $code$} method zero or more times, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   long as additional input may be available, passing <tt>false</tt> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   <tt>endOfInput</tt> argument and filling the input buffer and flushing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   output buffer between invocations; </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   <li><p> Invoke the {@link #$code$ $code$} method one final time, passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   <tt>true</tt> for the <tt>endOfInput</tt> argument; and then </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   <li><p> Invoke the {@link #flush flush} method so that the $coder$ can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   flush any internal state to the output buffer. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Each invocation of the {@link #$code$ $code$} method will $code$ as many
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * $itype$s as possible from the input buffer, writing the resulting $otype$s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * to the output buffer.  The {@link #$code$ $code$} method returns when more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * input is required, when there is not enough room in the output buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * when $a$ $coding$ error has occurred.  In each case a {@link CoderResult}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * object is returned to describe the reason for termination.  An invoker can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * examine this object and fill the input buffer, flush the output buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * attempt to recover from $a$ $coding$ error, as appropriate, and try again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <a name="ce">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p> There are two general types of $coding$ errors.  If the input $itype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * sequence is $notLegal$ then the input is considered <i>malformed</i>.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * the input $itype$ sequence is legal but cannot be mapped to a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * $outSequence$ then an <i>unmappable character</i> has been encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <a name="cae">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p> How $a$ $coding$ error is handled depends upon the action requested for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * that type of error, which is described by an instance of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * CodingErrorAction} class.  The possible error actions are to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * CodingErrorAction#IGNORE </code>ignore<code>} the erroneous input, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * CodingErrorAction#REPORT </code>report<code>} the error to the invoker via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * the returned {@link CoderResult} object, or {@link CodingErrorAction#REPLACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * </code>replace<code>} the erroneous input with the current value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * replacement $replTypeName$.  The replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
#if[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * is initially set to the $coder$'s default replacement, which often
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * (but not always) has the initial value&nbsp;$defaultReplName$;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
#end[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
#if[decoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * has the initial value $defaultReplName$;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
#end[decoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * its value may be changed via the {@link #replaceWith($replFQType$)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * replaceWith} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <p> The default action for malformed-input and unmappable-character errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * is to {@link CodingErrorAction#REPORT </code>report<code>} them.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * malformed-input error action may be changed via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * #onMalformedInput(CodingErrorAction) onMalformedInput} method; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * unmappable-character action may be changed via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * #onUnmappableCharacter(CodingErrorAction) onUnmappableCharacter} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <p> This class is designed to handle many of the details of the $coding$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * process, including the implementation of error actions.  $A$ $coder$ for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * specific charset, which is a concrete subclass of this class, need only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * implement the abstract {@link #$code$Loop $code$Loop} method, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * encapsulates the basic $coding$ loop.  A subclass that maintains internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * state should, additionally, override the {@link #implFlush implFlush} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * {@link #implReset implReset} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <p> Instances of this class are not safe for use by multiple concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * threads.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @see ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @see CharBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @see Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @see Charset$OtherCoder$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
public abstract class Charset$Coder$ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private final Charset charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private final float average$ItypesPerOtype$;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private final float max$ItypesPerOtype$;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private $replType$ replacement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private CodingErrorAction malformedInputAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        = CodingErrorAction.REPORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private CodingErrorAction unmappableCharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        = CodingErrorAction.REPORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // Internal states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static final int ST_RESET   = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static final int ST_CODING  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final int ST_END     = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static final int ST_FLUSHED = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private int state = ST_RESET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static String stateNames[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        = { "RESET", "CODING", "CODING_END", "FLUSHED" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Initializes a new $coder$.  The new $coder$ will have the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * $otypes-per-itype$ and replacement values. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param  average$ItypesPerOtype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *         A positive float value indicating the expected number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *         $otype$s that will be produced for each input $itype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param  max$ItypesPerOtype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *         A positive float value indicating the maximum number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *         $otype$s that will be produced for each input $itype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *         The initial replacement; must not be <tt>null</tt>, must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *         non-zero length, must not be longer than max$ItypesPerOtype$,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *         and must be {@link #isLegalReplacement </code>legal<code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *          If the preconditions on the parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    {#if[encoder]?protected:private}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    Charset$Coder$(Charset cs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                   float average$ItypesPerOtype$,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                   float max$ItypesPerOtype$,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                   $replType$ replacement)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        this.charset = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (average$ItypesPerOtype$ <= 0.0f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new IllegalArgumentException("Non-positive "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                               + "average$ItypesPerOtype$");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (max$ItypesPerOtype$ <= 0.0f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new IllegalArgumentException("Non-positive "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                               + "max$ItypesPerOtype$");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (!Charset.atBugLevel("1.4")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (average$ItypesPerOtype$ > max$ItypesPerOtype$)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                throw new IllegalArgumentException("average$ItypesPerOtype$"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                                   + " exceeds "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                                   + "max$ItypesPerOtype$");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        this.replacement = replacement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        this.average$ItypesPerOtype$ = average$ItypesPerOtype$;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        this.max$ItypesPerOtype$ = max$ItypesPerOtype$;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        replaceWith(replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Initializes a new $coder$.  The new $coder$ will have the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * $otypes-per-itype$ values and its replacement will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * $replTypeName$ $defaultReplName$. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param  average$ItypesPerOtype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *         A positive float value indicating the expected number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *         $otype$s that will be produced for each input $itype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param  max$ItypesPerOtype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *         A positive float value indicating the maximum number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *         $otype$s that will be produced for each input $itype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *          If the preconditions on the parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    protected Charset$Coder$(Charset cs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                             float average$ItypesPerOtype$,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                             float max$ItypesPerOtype$)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        this(cs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
             average$ItypesPerOtype$, max$ItypesPerOtype$,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
             $defaultRepl$);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Returns the charset that created this $coder$.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return  This $coder$'s charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public final Charset charset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Returns this $coder$'s replacement value. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return  This $coder$'s current replacement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *          which is never <tt>null</tt> and is never empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public final $replType$ replacement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return replacement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Changes this $coder$'s replacement value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p> This method invokes the {@link #implReplaceWith implReplaceWith}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * method, passing the new replacement, after checking that the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * replacement is acceptable.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param  newReplacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
#if[decoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *         The new replacement; must not be <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *         and must have non-zero length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
#end[decoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
#if[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *         The new replacement; must not be <tt>null</tt>, must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *         non-zero length, must not be longer than the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *         the {@link #max$ItypesPerOtype$() max$ItypesPerOtype$} method, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *         must be {@link #isLegalReplacement </code>legal<code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
#end[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return  This $coder$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          If the preconditions on the parameter do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public final Charset$Coder$ replaceWith($replType$ newReplacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (newReplacement == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new IllegalArgumentException("Null replacement");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int len = newReplacement.$replLength$;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw new IllegalArgumentException("Empty replacement");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (len > max$ItypesPerOtype$)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new IllegalArgumentException("Replacement too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
#if[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (!isLegalReplacement(newReplacement))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw new IllegalArgumentException("Illegal replacement");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
#end[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        this.replacement = newReplacement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        implReplaceWith(newReplacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Reports a change to this $coder$'s replacement value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p> The default implementation of this method does nothing.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * should be overridden by $coder$s that require notification of changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * the replacement.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param  newReplacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    protected void implReplaceWith($replType$ newReplacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
#if[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private WeakReference cachedDecoder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Tells whether or not the given byte array is a legal replacement value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * for this encoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <p> A replacement is legal if, and only if, it is a legal sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * bytes in this encoder's charset; that is, it must be possible to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * the replacement into one or more sixteen-bit Unicode characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p> The default implementation of this method is not very efficient; it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * should generally be overridden to improve performance.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param  repl  The byte array to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @return  <tt>true</tt> if, and only if, the given byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *          is a legal replacement value for this encoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public boolean isLegalReplacement(byte[] repl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        WeakReference wr = cachedDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        CharsetDecoder dec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if ((wr == null) || ((dec = (CharsetDecoder)wr.get()) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            dec = charset().newDecoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            dec.onMalformedInput(CodingErrorAction.REPORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            dec.onUnmappableCharacter(CodingErrorAction.REPORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            cachedDecoder = new WeakReference(dec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            dec.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        ByteBuffer bb = ByteBuffer.wrap(repl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        CharBuffer cb = CharBuffer.allocate((int)(bb.remaining()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                                  * dec.maxCharsPerByte()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        CoderResult cr = dec.decode(bb, cb, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return !cr.isError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
#end[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Returns this $coder$'s current action for malformed-input errors.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @return The current malformed-input action, which is never <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public CodingErrorAction malformedInputAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return malformedInputAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Changes this $coder$'s action for malformed-input errors.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <p> This method invokes the {@link #implOnMalformedInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * implOnMalformedInput} method, passing the new action.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param  newAction  The new action; must not be <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @return  This $coder$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *         If the precondition on the parameter does not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public final Charset$Coder$ onMalformedInput(CodingErrorAction newAction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (newAction == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new IllegalArgumentException("Null action");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        malformedInputAction = newAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        implOnMalformedInput(newAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Reports a change to this $coder$'s malformed-input action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p> The default implementation of this method does nothing.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * should be overridden by $coder$s that require notification of changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * the malformed-input action.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    protected void implOnMalformedInput(CodingErrorAction newAction) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Returns this $coder$'s current action for unmappable-character errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @return The current unmappable-character action, which is never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *         <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public CodingErrorAction unmappableCharacterAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return unmappableCharacterAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Changes this $coder$'s action for unmappable-character errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <p> This method invokes the {@link #implOnUnmappableCharacter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * implOnUnmappableCharacter} method, passing the new action.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param  newAction  The new action; must not be <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @return  This $coder$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *         If the precondition on the parameter does not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public final Charset$Coder$ onUnmappableCharacter(CodingErrorAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                                      newAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (newAction == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            throw new IllegalArgumentException("Null action");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        unmappableCharacterAction = newAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        implOnUnmappableCharacter(newAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Reports a change to this $coder$'s unmappable-character action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p> The default implementation of this method does nothing.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * should be overridden by $coder$s that require notification of changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * the unmappable-character action.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    protected void implOnUnmappableCharacter(CodingErrorAction newAction) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Returns the average number of $otype$s that will be produced for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * $itype$ of input.  This heuristic value may be used to estimate the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * of the output buffer required for a given input sequence. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @return  The average number of $otype$s produced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *          per $itype$ of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public final float average$ItypesPerOtype$() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return average$ItypesPerOtype$;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Returns the maximum number of $otype$s that will be produced for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * $itype$ of input.  This value may be used to compute the worst-case size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * of the output buffer required for a given input sequence. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return  The maximum number of $otype$s that will be produced per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *          $itype$ of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public final float max$ItypesPerOtype$() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return max$ItypesPerOtype$;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * $Code$s as many $itype$s as possible from the given input buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * writing the results to the given output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * <p> The buffers are read from, and written to, starting at their current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * positions.  At most {@link Buffer#remaining in.remaining()} $itype$s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * will be read and at most {@link Buffer#remaining out.remaining()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * $otype$s will be written.  The buffers' positions will be advanced to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * reflect the $itype$s read and the $otype$s written, but their marks and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * limits will not be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * <p> In addition to reading $itype$s from the input buffer and writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * $otype$s to the output buffer, this method returns a {@link CoderResult}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * object to describe its reason for termination:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *   <li><p> {@link CoderResult#UNDERFLOW} indicates that as much of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *   input buffer as possible has been $code$d.  If there is no further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *   input then the invoker can proceed to the next step of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *   <a href="#steps">$coding$ operation</a>.  Otherwise this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *   should be invoked again with further input.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *   <li><p> {@link CoderResult#OVERFLOW} indicates that there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *   insufficient space in the output buffer to $code$ any more $itype$s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *   This method should be invoked again with an output buffer that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *   more {@linkplain Buffer#remaining remaining} $otype$s. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *   typically done by draining any $code$d $otype$s from the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *   buffer.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *   <li><p> A {@link CoderResult#malformedForLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *   </code>malformed-input<code>} result indicates that a malformed-input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *   error has been detected.  The malformed $itype$s begin at the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *   buffer's (possibly incremented) position; the number of malformed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *   $itype$s may be determined by invoking the result object's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *   CoderResult#length() length} method.  This case applies only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *   {@link #onMalformedInput </code>malformed action<code>} of this $coder$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *   is {@link CodingErrorAction#REPORT}; otherwise the malformed input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *   will be ignored or replaced, as requested.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *   <li><p> An {@link CoderResult#unmappableForLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *   </code>unmappable-character<code>} result indicates that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *   unmappable-character error has been detected.  The $itype$s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *   $code$ the unmappable character begin at the input buffer's (possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *   incremented) position; the number of such $itype$s may be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *   by invoking the result object's {@link CoderResult#length() length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *   method.  This case applies only if the {@link #onUnmappableCharacter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *   </code>unmappable action<code>} of this $coder$ is {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *   CodingErrorAction#REPORT}; otherwise the unmappable character will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *   ignored or replaced, as requested.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * In any case, if this method is to be reinvoked in the same $coding$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * operation then care should be taken to preserve any $itype$s remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * in the input buffer so that they are available to the next invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <p> The <tt>endOfInput</tt> parameter advises this method as to whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * the invoker can provide further input beyond that contained in the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * input buffer.  If there is a possibility of providing additional input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * then the invoker should pass <tt>false</tt> for this parameter; if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * is no possibility of providing further input then the invoker should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * pass <tt>true</tt>.  It is not erroneous, and in fact it is quite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * common, to pass <tt>false</tt> in one invocation and later discover that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * no further input was actually available.  It is critical, however, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * the final invocation of this method in a sequence of invocations always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * pass <tt>true</tt> so that any remaining un$code$d input will be treated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * as being malformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <p> This method works by invoking the {@link #$code$Loop $code$Loop}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * method, interpreting its results, handling error conditions, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * reinvoking it as necessary.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @param  in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *         The input $itype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param  out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *         The output $otype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @param  endOfInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *         <tt>true</tt> if, and only if, the invoker can provide no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *         additional input $itype$s beyond those in the given buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @return  A coder-result object describing the reason for termination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *          If $a$ $coding$ operation is already in progress and the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *          step was an invocation neither of the {@link #reset reset}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *          method, nor of this method with a value of <tt>false</tt> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *          the <tt>endOfInput</tt> parameter, nor of this method with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *          value of <tt>true</tt> for the <tt>endOfInput</tt> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *          but a return value indicating an incomplete $coding$ operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @throws  CoderMalfunctionError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *          If an invocation of the $code$Loop method threw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *          an unexpected exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public final CoderResult $code$($Itype$Buffer in, $Otype$Buffer out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                    boolean endOfInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        int newState = endOfInput ? ST_END : ST_CODING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if ((state != ST_RESET) && (state != ST_CODING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            && !(endOfInput && (state == ST_END)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            throwIllegalStateException(state, newState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            CoderResult cr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                cr = $code$Loop(in, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            } catch (BufferUnderflowException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                throw new CoderMalfunctionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            } catch (BufferOverflowException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                throw new CoderMalfunctionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (cr.isOverflow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                return cr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (cr.isUnderflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                if (endOfInput && in.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    cr = CoderResult.malformedForLength(in.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    // Fall through to malformed-input case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    return cr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            CodingErrorAction action = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (cr.isMalformed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                action = malformedInputAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            else if (cr.isUnmappable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                action = unmappableCharacterAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                assert false : cr.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            if (action == CodingErrorAction.REPORT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                return cr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            if (action == CodingErrorAction.REPLACE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                if (out.remaining() < replacement.$replLength$)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                out.put(replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if ((action == CodingErrorAction.IGNORE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                || (action == CodingErrorAction.REPLACE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                // Skip erroneous input either way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                in.position(in.position() + cr.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Flushes this $coder$.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <p> Some $coder$s maintain internal state and may need to write some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * final $otype$s to the output buffer once the overall input sequence has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * been read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <p> Any additional output is written to the output buffer beginning at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * its current position.  At most {@link Buffer#remaining out.remaining()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * $otype$s will be written.  The buffer's position will be advanced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * appropriately, but its mark and limit will not be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <p> If this method completes successfully then it returns {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * CoderResult#UNDERFLOW}.  If there is insufficient room in the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * buffer then it returns {@link CoderResult#OVERFLOW}.  If this happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * then this method must be invoked again, with an output buffer that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * more room, in order to complete the current <a href="#steps">$coding$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * operation</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <p> If this $coder$ has already been flushed then invoking this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <p> This method invokes the {@link #implFlush implFlush} method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * perform the actual flushing operation.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param  out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *         The output $otype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return  A coder-result object, either {@link CoderResult#UNDERFLOW} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *          {@link CoderResult#OVERFLOW}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *          If the previous step of the current $coding$ operation was an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *          invocation neither of the {@link #flush flush} method nor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *          the three-argument {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *          #$code$($Itype$Buffer,$Otype$Buffer,boolean) $code$} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *          with a value of <tt>true</tt> for the <tt>endOfInput</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *          parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public final CoderResult flush($Otype$Buffer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if (state == ST_END) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            CoderResult cr = implFlush(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            if (cr.isUnderflow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                state = ST_FLUSHED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            return cr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (state != ST_FLUSHED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            throwIllegalStateException(state, ST_FLUSHED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        return CoderResult.UNDERFLOW; // Already flushed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Flushes this $coder$.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <p> The default implementation of this method does nothing, and always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * returns {@link CoderResult#UNDERFLOW}.  This method should be overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * by $coder$s that may need to write final $otype$s to the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * once the entire input sequence has been read. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @param  out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *         The output $otype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @return  A coder-result object, either {@link CoderResult#UNDERFLOW} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *          {@link CoderResult#OVERFLOW}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    protected CoderResult implFlush($Otype$Buffer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Resets this $coder$, clearing any internal state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <p> This method resets charset-independent state and also invokes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * {@link #implReset() implReset} method in order to perform any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * charset-specific reset actions.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @return  This $coder$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public final Charset$Coder$ reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        implReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        state = ST_RESET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Resets this $coder$, clearing any charset-specific internal state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <p> The default implementation of this method does nothing.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * should be overridden by $coder$s that maintain internal state.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    protected void implReset() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * $Code$s one or more $itype$s into one or more $otype$s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * <p> This method encapsulates the basic $coding$ loop, $coding$ as many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * $itype$s as possible until it either runs out of input, runs out of room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * in the output buffer, or encounters $a$ $coding$ error.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * invoked by the {@link #$code$ $code$} method, which handles result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * interpretation and error recovery.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <p> The buffers are read from, and written to, starting at their current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * positions.  At most {@link Buffer#remaining in.remaining()} $itype$s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * will be read, and at most {@link Buffer#remaining out.remaining()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * $otype$s will be written.  The buffers' positions will be advanced to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * reflect the $itype$s read and the $otype$s written, but their marks and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * limits will not be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <p> This method returns a {@link CoderResult} object to describe its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * reason for termination, in the same manner as the {@link #$code$ $code$}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * method.  Most implementations of this method will handle $coding$ errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * by returning an appropriate result object for interpretation by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * {@link #$code$ $code$} method.  An optimized implementation may instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * examine the relevant error action and implement that action itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * <p> An implementation of this method may perform arbitrary lookahead by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * returning {@link CoderResult#UNDERFLOW} until it receives sufficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * input.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @param  in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *         The input $itype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @param  out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *         The output $otype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @return  A coder-result object describing the reason for termination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    protected abstract CoderResult $code$Loop($Itype$Buffer in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                                              $Otype$Buffer out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Convenience method that $code$s the remaining content of a single input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * $itype$ buffer into a newly-allocated $otype$ buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * <p> This method implements an entire <a href="#steps">$coding$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * operation</a>; that is, it resets this $coder$, then it $code$s the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * $itype$s in the given $itype$ buffer, and finally it flushes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * $coder$.  This method should therefore not be invoked if $a$ $coding$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * operation is already in progress.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @param  in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *         The input $itype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @return A newly-allocated $otype$ buffer containing the result of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *         $coding$ operation.  The buffer's position will be zero and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *         limit will follow the last $otype$ written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *          If $a$ $coding$ operation is already in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @throws  MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *          If the $itype$ sequence starting at the input buffer's current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *          position is $notLegal$ and the current malformed-input action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *          is {@link CodingErrorAction#REPORT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @throws  UnmappableCharacterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *          If the $itype$ sequence starting at the input buffer's current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *          position cannot be mapped to an equivalent $otype$ sequence and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *          the current unmappable-character action is {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *          CodingErrorAction#REPORT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    public final $Otype$Buffer $code$($Itype$Buffer in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        throws CharacterCodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        int n = (int)(in.remaining() * average$ItypesPerOtype$());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        $Otype$Buffer out = $Otype$Buffer.allocate(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if ((n == 0) && (in.remaining() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            CoderResult cr = in.hasRemaining() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                $code$(in, out, true) : CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            if (cr.isUnderflow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                cr = flush(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            if (cr.isUnderflow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            if (cr.isOverflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                n = 2*n + 1;    // Ensure progress; n might be 0!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                $Otype$Buffer o = $Otype$Buffer.allocate(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                out.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                o.put(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                out = o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            cr.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        out.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
#if[decoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Tells whether or not this decoder implements an auto-detecting charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * <p> The default implementation of this method always returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * <tt>false</tt>; it should be overridden by auto-detecting decoders to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * return <tt>true</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @return  <tt>true</tt> if, and only if, this decoder implements an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *          auto-detecting charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    public boolean isAutoDetecting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * Tells whether or not this decoder has yet detected a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * charset&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * <p> If this decoder implements an auto-detecting charset then at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * single point during a decoding operation this method may start returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * <tt>true</tt> to indicate that a specific charset has been detected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * the input byte sequence.  Once this occurs, the {@link #detectedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * detectedCharset} method may be invoked to retrieve the detected charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * <p> That this method returns <tt>false</tt> does not imply that no bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * have yet been decoded.  Some auto-detecting decoders are capable of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * decoding some, or even all, of an input byte sequence without fixing on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * a particular charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * <p> The default implementation of this method always throws an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * UnsupportedOperationException}; it should be overridden by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * auto-detecting decoders to return <tt>true</tt> once the input charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * has been determined.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @return  <tt>true</tt> if, and only if, this decoder has detected a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *          specific charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *          If this decoder does not implement an auto-detecting charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    public boolean isCharsetDetected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Retrieves the charset that was detected by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * decoder&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <p> If this decoder implements an auto-detecting charset then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * method returns the actual charset once it has been detected.  After that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * point, this method returns the same value for the duration of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * current decoding operation.  If not enough input bytes have yet been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * read to determine the actual charset then this method throws an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * <p> The default implementation of this method always throws an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * UnsupportedOperationException}; it should be overridden by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * auto-detecting decoders to return the appropriate value.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @return  The charset detected by this auto-detecting decoder,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *          or <tt>null</tt> if the charset has not yet been determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *          If insufficient bytes have been read to determine a charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *          If this decoder does not implement an auto-detecting charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public Charset detectedCharset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
#end[decoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
#if[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    private boolean canEncode(CharBuffer cb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if (state == ST_FLUSHED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        else if (state != ST_RESET)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            throwIllegalStateException(state, ST_CODING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        CodingErrorAction ma = malformedInputAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        CodingErrorAction ua = unmappableCharacterAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            onMalformedInput(CodingErrorAction.REPORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            onUnmappableCharacter(CodingErrorAction.REPORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            encode(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        } catch (CharacterCodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            onMalformedInput(ma);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            onUnmappableCharacter(ua);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Tells whether or not this encoder can encode the given character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * <p> This method returns <tt>false</tt> if the given character is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * surrogate character; such characters can be interpreted only when they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * are members of a pair consisting of a high surrogate followed by a low
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * surrogate.  The {@link #canEncode(java.lang.CharSequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * canEncode(CharSequence)} method may be used to test whether or not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * character sequence can be encoded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * <p> This method may modify this encoder's state; it should therefore not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * be invoked if an <a href="#steps">encoding operation</a> is already in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * <p> The default implementation of this method is not very efficient; it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * should generally be overridden to improve performance.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @return  <tt>true</tt> if, and only if, this encoder can encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *          the given character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *          If $a$ $coding$ operation is already in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    public boolean canEncode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        CharBuffer cb = CharBuffer.allocate(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        cb.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        cb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        return canEncode(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Tells whether or not this encoder can encode the given character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * <p> If this method returns <tt>false</tt> for a particular character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * sequence then more information about why the sequence cannot be encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * may be obtained by performing a full <a href="#steps">encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * operation</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * <p> This method may modify this encoder's state; it should therefore not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * be invoked if an encoding operation is already in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * <p> The default implementation of this method is not very efficient; it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * should generally be overridden to improve performance.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @return  <tt>true</tt> if, and only if, this encoder can encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *          the given character without throwing any exceptions and without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *          performing any replacements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *          If $a$ $coding$ operation is already in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    public boolean canEncode(CharSequence cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        CharBuffer cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        if (cs instanceof CharBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            cb = ((CharBuffer)cs).duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            cb = CharBuffer.wrap(cs.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        return canEncode(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
#end[encoder]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    private void throwIllegalStateException(int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        throw new IllegalStateException("Current state = " + stateNames[from]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                                        + ", new state = " + stateNames[to]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
}