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