Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/charset/CoderResult.java
38918 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.charset;2627import java.lang.ref.WeakReference;28import java.nio.*;29import java.util.Map;30import java.util.HashMap;313233/**34* A description of the result state of a coder.35*36* <p> A charset coder, that is, either a decoder or an encoder, consumes bytes37* (or characters) from an input buffer, translates them, and writes the38* resulting characters (or bytes) to an output buffer. A coding process39* terminates for one of four categories of reasons, which are described by40* instances of this class:41*42* <ul>43*44* <li><p> <i>Underflow</i> is reported when there is no more input to be45* processed, or there is insufficient input and additional input is46* required. This condition is represented by the unique result object47* {@link #UNDERFLOW}, whose {@link #isUnderflow() isUnderflow} method48* returns <tt>true</tt>. </p></li>49*50* <li><p> <i>Overflow</i> is reported when there is insufficient room51* remaining in the output buffer. This condition is represented by the52* unique result object {@link #OVERFLOW}, whose {@link #isOverflow()53* isOverflow} method returns <tt>true</tt>. </p></li>54*55* <li><p> A <i>malformed-input error</i> is reported when a sequence of56* input units is not well-formed. Such errors are described by instances of57* this class whose {@link #isMalformed() isMalformed} method returns58* <tt>true</tt> and whose {@link #length() length} method returns the length59* of the malformed sequence. There is one unique instance of this class for60* all malformed-input errors of a given length. </p></li>61*62* <li><p> An <i>unmappable-character error</i> is reported when a sequence63* of input units denotes a character that cannot be represented in the64* output charset. Such errors are described by instances of this class65* whose {@link #isUnmappable() isUnmappable} method returns <tt>true</tt> and66* whose {@link #length() length} method returns the length of the input67* sequence denoting the unmappable character. There is one unique instance68* of this class for all unmappable-character errors of a given length.69* </p></li>70*71* </ul>72*73* <p> For convenience, the {@link #isError() isError} method returns <tt>true</tt>74* for result objects that describe malformed-input and unmappable-character75* errors but <tt>false</tt> for those that describe underflow or overflow76* conditions. </p>77*78*79* @author Mark Reinhold80* @author JSR-51 Expert Group81* @since 1.482*/8384public class CoderResult {8586private static final int CR_UNDERFLOW = 0;87private static final int CR_OVERFLOW = 1;88private static final int CR_ERROR_MIN = 2;89private static final int CR_MALFORMED = 2;90private static final int CR_UNMAPPABLE = 3;9192private static final String[] names93= { "UNDERFLOW", "OVERFLOW", "MALFORMED", "UNMAPPABLE" };9495private final int type;96private final int length;9798private CoderResult(int type, int length) {99this.type = type;100this.length = length;101}102103/**104* Returns a string describing this coder result.105*106* @return A descriptive string107*/108public String toString() {109String nm = names[type];110return isError() ? nm + "[" + length + "]" : nm;111}112113/**114* Tells whether or not this object describes an underflow condition.115*116* @return <tt>true</tt> if, and only if, this object denotes underflow117*/118public boolean isUnderflow() {119return (type == CR_UNDERFLOW);120}121122/**123* Tells whether or not this object describes an overflow condition.124*125* @return <tt>true</tt> if, and only if, this object denotes overflow126*/127public boolean isOverflow() {128return (type == CR_OVERFLOW);129}130131/**132* Tells whether or not this object describes an error condition.133*134* @return <tt>true</tt> if, and only if, this object denotes either a135* malformed-input error or an unmappable-character error136*/137public boolean isError() {138return (type >= CR_ERROR_MIN);139}140141/**142* Tells whether or not this object describes a malformed-input error.143*144* @return <tt>true</tt> if, and only if, this object denotes a145* malformed-input error146*/147public boolean isMalformed() {148return (type == CR_MALFORMED);149}150151/**152* Tells whether or not this object describes an unmappable-character153* error.154*155* @return <tt>true</tt> if, and only if, this object denotes an156* unmappable-character error157*/158public boolean isUnmappable() {159return (type == CR_UNMAPPABLE);160}161162/**163* Returns the length of the erroneous input described by this164* object <i>(optional operation)</i>.165*166* @return The length of the erroneous input, a positive integer167*168* @throws UnsupportedOperationException169* If this object does not describe an error condition, that is,170* if the {@link #isError() isError} does not return <tt>true</tt>171*/172public int length() {173if (!isError())174throw new UnsupportedOperationException();175return length;176}177178/**179* Result object indicating underflow, meaning that either the input buffer180* has been completely consumed or, if the input buffer is not yet empty,181* that additional input is required.182*/183public static final CoderResult UNDERFLOW184= new CoderResult(CR_UNDERFLOW, 0);185186/**187* Result object indicating overflow, meaning that there is insufficient188* room in the output buffer.189*/190public static final CoderResult OVERFLOW191= new CoderResult(CR_OVERFLOW, 0);192193private static abstract class Cache {194195private Map<Integer,WeakReference<CoderResult>> cache = null;196197protected abstract CoderResult create(int len);198199private synchronized CoderResult get(int len) {200if (len <= 0)201throw new IllegalArgumentException("Non-positive length");202Integer k = new Integer(len);203WeakReference<CoderResult> w;204CoderResult e = null;205if (cache == null) {206cache = new HashMap<Integer,WeakReference<CoderResult>>();207} else if ((w = cache.get(k)) != null) {208e = w.get();209}210if (e == null) {211e = create(len);212cache.put(k, new WeakReference<CoderResult>(e));213}214return e;215}216217}218219private static Cache malformedCache220= new Cache() {221public CoderResult create(int len) {222return new CoderResult(CR_MALFORMED, len);223}};224225/**226* Static factory method that returns the unique object describing a227* malformed-input error of the given length.228*229* @param length230* The given length231*232* @return The requested coder-result object233*/234public static CoderResult malformedForLength(int length) {235return malformedCache.get(length);236}237238private static Cache unmappableCache239= new Cache() {240public CoderResult create(int len) {241return new CoderResult(CR_UNMAPPABLE, len);242}};243244/**245* Static factory method that returns the unique result object describing246* an unmappable-character error of the given length.247*248* @param length249* The given length250*251* @return The requested coder-result object252*/253public static CoderResult unmappableForLength(int length) {254return unmappableCache.get(length);255}256257/**258* Throws an exception appropriate to the result described by this object.259*260* @throws BufferUnderflowException261* If this object is {@link #UNDERFLOW}262*263* @throws BufferOverflowException264* If this object is {@link #OVERFLOW}265*266* @throws MalformedInputException267* If this object represents a malformed-input error; the268* exception's length value will be that of this object269*270* @throws UnmappableCharacterException271* If this object represents an unmappable-character error; the272* exceptions length value will be that of this object273*/274public void throwException()275throws CharacterCodingException276{277switch (type) {278case CR_UNDERFLOW: throw new BufferUnderflowException();279case CR_OVERFLOW: throw new BufferOverflowException();280case CR_MALFORMED: throw new MalformedInputException(length);281case CR_UNMAPPABLE: throw new UnmappableCharacterException(length);282default:283assert false;284}285}286287}288289290