jdk/src/java.base/share/classes/java/nio/charset/CoderResult.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 32143 394ab6a6658d
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18574
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio.charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A description of the result state of a coder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p> A charset coder, that is, either a decoder or an encoder, consumes bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * (or characters) from an input buffer, translates them, and writes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * resulting characters (or bytes) to an output buffer.  A coding process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * terminates for one of four categories of reasons, which are described by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * instances of this class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   <li><p> <i>Underflow</i> is reported when there is no more input to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   processed, or there is insufficient input and additional input is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   required.  This condition is represented by the unique result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   {@link #UNDERFLOW}, whose {@link #isUnderflow() isUnderflow} method
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
    49
 *   returns {@code true}.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   <li><p> <i>Overflow</i> is reported when there is insufficient room
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   remaining in the output buffer.  This condition is represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   unique result object {@link #OVERFLOW}, whose {@link #isOverflow()
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
    54
 *   isOverflow} method returns {@code true}.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   <li><p> A <i>malformed-input error</i> is reported when a sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   input units is not well-formed.  Such errors are described by instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   this class whose {@link #isMalformed() isMalformed} method returns
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
    59
 *   {@code true} and whose {@link #length() length} method returns the length
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   of the malformed sequence.  There is one unique instance of this class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   all malformed-input errors of a given length.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   <li><p> An <i>unmappable-character error</i> is reported when a sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   of input units denotes a character that cannot be represented in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   output charset.  Such errors are described by instances of this class
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
    66
 *   whose {@link #isUnmappable() isUnmappable} method returns {@code true} and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   whose {@link #length() length} method returns the length of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   sequence denoting the unmappable character.  There is one unique instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   of this class for all unmappable-character errors of a given length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
    74
 * <p> For convenience, the {@link #isError() isError} method returns {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * for result objects that describe malformed-input and unmappable-character
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
    76
 * errors but {@code false} for those that describe underflow or overflow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * conditions.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public class CoderResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final int CR_UNDERFLOW  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final int CR_OVERFLOW   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final int CR_ERROR_MIN  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final int CR_MALFORMED  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final int CR_UNMAPPABLE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final String[] names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        = { "UNDERFLOW", "OVERFLOW", "MALFORMED", "UNMAPPABLE" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private final int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private final int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private CoderResult(int type, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Returns a string describing this coder result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return  A descriptive string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        String nm = names[type];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return isError() ? nm + "[" + length + "]" : nm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   115
     * Tells whether or not this object describes an underflow condition.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
   117
     * @return  {@code true} if, and only if, this object denotes underflow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public boolean isUnderflow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return (type == CR_UNDERFLOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   124
     * Tells whether or not this object describes an overflow condition.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
   126
     * @return  {@code true} if, and only if, this object denotes overflow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public boolean isOverflow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return (type == CR_OVERFLOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   133
     * Tells whether or not this object describes an error condition.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
   135
     * @return  {@code true} if, and only if, this object denotes either a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *          malformed-input error or an unmappable-character error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public boolean isError() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return (type >= CR_ERROR_MIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Tells whether or not this object describes a malformed-input error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
   145
     * @return  {@code true} if, and only if, this object denotes a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *          malformed-input error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public boolean isMalformed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return (type == CR_MALFORMED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Tells whether or not this object describes an unmappable-character
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   154
     * error.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
   156
     * @return  {@code true} if, and only if, this object denotes an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *          unmappable-character error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public boolean isUnmappable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return (type == CR_UNMAPPABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns the length of the erroneous input described by this
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   165
     * object&nbsp;&nbsp;<i>(optional operation)</i>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return  The length of the erroneous input, a positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *          If this object does not describe an error condition, that is,
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 29986
diff changeset
   171
     *          if the {@link #isError() isError} does not return {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (!isError())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Result object indicating underflow, meaning that either the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * has been completely consumed or, if the input buffer is not yet empty,
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   182
     * that additional input is required.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public static final CoderResult UNDERFLOW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        = new CoderResult(CR_UNDERFLOW, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Result object indicating overflow, meaning that there is insufficient
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   189
     * room in the output buffer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public static final CoderResult OVERFLOW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        = new CoderResult(CR_OVERFLOW, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32143
diff changeset
   194
    private abstract static class Cache {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   196
        private Map<Integer,WeakReference<CoderResult>> cache = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        protected abstract CoderResult create(int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        private synchronized CoderResult get(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (len <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                throw new IllegalArgumentException("Non-positive length");
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   203
            Integer k = len;
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   204
            WeakReference<CoderResult> w;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            CoderResult e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (cache == null) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   207
                cache = new HashMap<>();
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   208
            } else if ((w = cache.get(k)) != null) {
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   209
                e = w.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                e = create(len);
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   213
                cache.put(k, new WeakReference<>(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static Cache malformedCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        = new Cache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                public CoderResult create(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    return new CoderResult(CR_MALFORMED, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Static factory method that returns the unique object describing a
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   228
     * malformed-input error of the given length.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   230
     * @param   length
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   231
     *          The given length
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   232
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return  The requested coder-result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static CoderResult malformedForLength(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return malformedCache.get(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static Cache unmappableCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        = new Cache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                public CoderResult create(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    return new CoderResult(CR_UNMAPPABLE, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Static factory method that returns the unique result object describing
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   247
     * an unmappable-character error of the given length.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   249
     * @param   length
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   250
     *          The given length
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   251
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return  The requested coder-result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public static CoderResult unmappableForLength(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return unmappableCache.get(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Throws an exception appropriate to the result described by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @throws  BufferUnderflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *          If this object is {@link #UNDERFLOW}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *          If this object is {@link #OVERFLOW}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @throws  MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          If this object represents a malformed-input error; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *          exception's length value will be that of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws  UnmappableCharacterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *          If this object represents an unmappable-character error; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          exceptions length value will be that of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public void throwException()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        throws CharacterCodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        case CR_UNDERFLOW:   throw new BufferUnderflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        case CR_OVERFLOW:    throw new BufferOverflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        case CR_MALFORMED:   throw new MalformedInputException(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        case CR_UNMAPPABLE:  throw new UnmappableCharacterException(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}