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