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