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