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