1 | /* CoderResult.java --
|
---|
2 | Copyright (C) 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package java.nio.charset;
|
---|
39 |
|
---|
40 | import java.lang.ref.WeakReference;
|
---|
41 | import java.nio.BufferOverflowException;
|
---|
42 | import java.nio.BufferUnderflowException;
|
---|
43 | import java.util.HashMap;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * @author Jesse Rosenstock
|
---|
47 | * @since 1.4
|
---|
48 | */
|
---|
49 | public class CoderResult
|
---|
50 | {
|
---|
51 | private static final int TYPE_MALFORMED = 0;
|
---|
52 | private static final int TYPE_OVERFLOW = 1;
|
---|
53 | private static final int TYPE_UNDERFLOW = 2;
|
---|
54 | private static final int TYPE_UNMAPPABLE = 3;
|
---|
55 |
|
---|
56 | public static final CoderResult OVERFLOW
|
---|
57 | = new CoderResult (TYPE_OVERFLOW, 0);
|
---|
58 | public static final CoderResult UNDERFLOW
|
---|
59 | = new CoderResult (TYPE_UNDERFLOW, 0);
|
---|
60 |
|
---|
61 | private static final String[] names
|
---|
62 | = { "MALFORMED", "OVERFLOW", "UNDERFLOW", "UNMAPPABLE" };
|
---|
63 |
|
---|
64 | private static final Cache malformedCache
|
---|
65 | = new Cache ()
|
---|
66 | {
|
---|
67 | protected CoderResult make (int length)
|
---|
68 | {
|
---|
69 | return new CoderResult (TYPE_MALFORMED, length);
|
---|
70 | }
|
---|
71 | };
|
---|
72 |
|
---|
73 | private static final Cache unmappableCache
|
---|
74 | = new Cache ()
|
---|
75 | {
|
---|
76 | protected CoderResult make (int length)
|
---|
77 | {
|
---|
78 | return new CoderResult (TYPE_UNMAPPABLE, length);
|
---|
79 | }
|
---|
80 | };
|
---|
81 |
|
---|
82 | private final int type;
|
---|
83 | private final int length;
|
---|
84 |
|
---|
85 | private CoderResult (int type, int length)
|
---|
86 | {
|
---|
87 | this.type = type;
|
---|
88 | this.length = length;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public boolean isError ()
|
---|
92 | {
|
---|
93 | return length > 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public boolean isMalformed ()
|
---|
97 | {
|
---|
98 | return type == TYPE_MALFORMED;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public boolean isOverflow ()
|
---|
102 | {
|
---|
103 | return type == TYPE_OVERFLOW;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public boolean isUnderflow ()
|
---|
107 | {
|
---|
108 | return type == TYPE_UNDERFLOW;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public boolean isUnmappable ()
|
---|
112 | {
|
---|
113 | return type == TYPE_UNMAPPABLE;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public int length ()
|
---|
117 | {
|
---|
118 | if (length <= 0)
|
---|
119 | throw new UnsupportedOperationException ();
|
---|
120 | else
|
---|
121 | return length;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public static CoderResult malformedForLength (int length)
|
---|
125 | {
|
---|
126 | return malformedCache.get (length);
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void throwException ()
|
---|
130 | throws CharacterCodingException
|
---|
131 | {
|
---|
132 | switch (type)
|
---|
133 | {
|
---|
134 | case TYPE_MALFORMED:
|
---|
135 | throw new MalformedInputException (length);
|
---|
136 | case TYPE_OVERFLOW:
|
---|
137 | throw new BufferOverflowException ();
|
---|
138 | case TYPE_UNDERFLOW:
|
---|
139 | throw new BufferUnderflowException ();
|
---|
140 | case TYPE_UNMAPPABLE:
|
---|
141 | throw new UnmappableCharacterException (length);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public String toString ()
|
---|
146 | {
|
---|
147 | String name = names[type];
|
---|
148 | return (length > 0) ? name + '[' + length + ']' : name;
|
---|
149 | }
|
---|
150 |
|
---|
151 | public static CoderResult unmappableForLength (int length)
|
---|
152 | {
|
---|
153 | return unmappableCache.get (length);
|
---|
154 | }
|
---|
155 |
|
---|
156 | private abstract static class Cache
|
---|
157 | {
|
---|
158 | private final HashMap cache;
|
---|
159 |
|
---|
160 | private Cache ()
|
---|
161 | {
|
---|
162 | // If we didn't synchronize on this, then cache would be initialized
|
---|
163 | // without holding a lock. Undefined behavior would occur if the
|
---|
164 | // first thread to call get(int) was not the same as the one that
|
---|
165 | // called the constructor.
|
---|
166 | synchronized (this)
|
---|
167 | {
|
---|
168 | cache = new HashMap ();
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | private synchronized CoderResult get (int length)
|
---|
173 | {
|
---|
174 | if (length <= 0)
|
---|
175 | throw new IllegalArgumentException ("Non-positive length");
|
---|
176 |
|
---|
177 | Integer len = new Integer (length);
|
---|
178 | CoderResult cr = null;
|
---|
179 | Object o;
|
---|
180 | if ((o = cache.get (len)) != null)
|
---|
181 | cr = (CoderResult) ((WeakReference) o).get ();
|
---|
182 | if (cr == null)
|
---|
183 | {
|
---|
184 | cr = make (length);
|
---|
185 | cache.put (len, new WeakReference (cr));
|
---|
186 | }
|
---|
187 |
|
---|
188 | return cr;
|
---|
189 | }
|
---|
190 |
|
---|
191 | protected abstract CoderResult make (int length);
|
---|
192 | }
|
---|
193 | }
|
---|