FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / nio / charset / CharsetDecoder.java
blob185de149fd43d9a925df2d89df7b40d8d7b4e804
1 /* CharsetDecoder.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package java.nio.charset;
40 import java.nio.ByteBuffer;
41 import java.nio.CharBuffer;
43 /**
44 * @author Jesse Rosenstock
45 * @since 1.4
47 public abstract class CharsetDecoder
49 private static final int STATE_RESET = 0;
50 private static final int STATE_CODING = 1;
51 private static final int STATE_END = 2;
52 private static final int STATE_FLUSHED = 3;
54 private static final String DEFAULT_REPLACEMENT = "\uFFFD";
56 private final Charset charset;
57 private final float averageCharsPerByte;
58 private final float maxCharsPerByte;
59 private String replacement;
61 private int state = STATE_RESET;
63 private CodingErrorAction malformedInputAction
64 = CodingErrorAction.REPORT;
65 private CodingErrorAction unmappableCharacterAction
66 = CodingErrorAction.REPORT;
68 private CharsetDecoder (Charset cs, float averageCharsPerByte,
69 float maxCharsPerByte, String replacement)
71 if (averageCharsPerByte <= 0.0f)
72 throw new IllegalArgumentException ("Non-positive averageCharsPerByte");
73 if (maxCharsPerByte <= 0.0f)
74 throw new IllegalArgumentException ("Non-positive maxCharsPerByte");
76 this.charset = cs;
77 this.averageCharsPerByte
78 = averageCharsPerByte;
79 this.maxCharsPerByte
80 = maxCharsPerByte;
81 this.replacement = replacement;
82 implReplaceWith (replacement);
85 protected CharsetDecoder (Charset cs, float averageCharsPerByte,
86 float maxCharsPerByte)
88 this (cs, averageCharsPerByte, maxCharsPerByte, DEFAULT_REPLACEMENT);
91 public final float averageCharsPerByte ()
93 return averageCharsPerByte;
96 public final Charset charset ()
98 return charset;
101 public final CharBuffer decode (ByteBuffer in)
102 throws CharacterCodingException
104 // XXX: Sun's Javadoc seems to contradict itself saying an
105 // IllegalStateException is thrown "if a decoding operation is already
106 // in progress" and also that "it resets this Decoder".
107 // Should we check to see that the state is reset, or should we
108 // call reset()?
109 if (state != STATE_RESET)
110 throw new IllegalStateException ();
112 // REVIEW: Using max instead of average may allocate a very large
113 // buffer. Maybe we should do something more efficient?
114 int remaining = in.remaining ();
115 int n = (int) (remaining * maxCharsPerByte ());
116 CharBuffer out = CharBuffer.allocate (n);
118 if (remaining == 0)
120 state = STATE_FLUSHED;
121 return out;
124 CoderResult cr = decode (in, out, true);
125 if (cr.isError ())
126 cr.throwException ();
128 cr = flush (out);
129 if (cr.isError ())
130 cr.throwException ();
132 out.flip ();
133 return out;
136 public final CoderResult decode (ByteBuffer in, CharBuffer out,
137 boolean endOfInput)
139 int newState = endOfInput ? STATE_END : STATE_CODING;
140 // XXX: Need to check for "previous step was an invocation [not] of
141 // this method with a value of true for the endOfInput parameter but
142 // a return value indicating an incomplete decoding operation"
143 // XXX: We will not check the previous return value, just
144 // that the previous call passed true for endOfInput
145 if (state != STATE_RESET && state != STATE_CODING
146 && !(endOfInput && state == STATE_END))
147 throw new IllegalStateException ();
148 state = newState;
150 for (;;)
152 CoderResult cr;
155 cr = decodeLoop (in, out);
157 catch (RuntimeException e)
159 throw new CoderMalfunctionError (e);
162 if (cr.isOverflow ())
163 return cr;
165 if (cr.isUnderflow ())
167 if (endOfInput && in.hasRemaining ())
168 cr = CoderResult.malformedForLength (in.remaining ());
169 else
170 return cr;
173 CodingErrorAction action = cr.isMalformed ()
174 ? malformedInputAction
175 : unmappableCharacterAction;
177 if (action == CodingErrorAction.REPORT)
178 return cr;
180 if (action == CodingErrorAction.REPLACE)
182 if (out.remaining () < replacement.length ())
183 return CoderResult.OVERFLOW;
184 out.put (replacement);
187 in.position (in.position () + cr.length ());
191 protected abstract CoderResult decodeLoop (ByteBuffer in, CharBuffer out);
193 public Charset detectedCharset ()
195 throw new UnsupportedOperationException ();
198 public final CoderResult flush (CharBuffer out)
200 // It seems weird that you can flush after reset, but Sun's javadoc
201 // says an IllegalStateException is thrown "If the previous step of the
202 // current decoding operation was an invocation neither of the reset
203 // method nor ... of the three-argument decode method with a value of
204 // true for the endOfInput parameter."
205 // Further note that flush() only requires that there not be
206 // an IllegalStateException if the previous step was a call to
207 // decode with true as the last argument. It does not require
208 // that the call succeeded. decode() does require that it succeeded.
209 // XXX: test this to see if reality matches javadoc
210 if (state != STATE_RESET && state != STATE_END)
211 throw new IllegalStateException ();
213 state = STATE_FLUSHED;
214 return implFlush (out);
217 protected CoderResult implFlush (CharBuffer out)
219 return CoderResult.UNDERFLOW;
222 public final CharsetDecoder onMalformedInput (CodingErrorAction newAction)
224 if (newAction == null)
225 throw new IllegalArgumentException ("Null action");
227 malformedInputAction = newAction;
228 implOnMalformedInput (newAction);
229 return this;
232 protected void implOnMalformedInput (CodingErrorAction newAction)
234 // default implementation does nothing
237 protected void implOnUnmappableCharacter (CodingErrorAction newAction)
239 // default implementation does nothing
242 protected void implReplaceWith (String newReplacement)
244 // default implementation does nothing
247 protected void implReset ()
249 // default implementation does nothing
252 public boolean isAutoDetecting ()
254 return false;
257 public boolean isCharsetDetected ()
259 throw new UnsupportedOperationException ();
262 public CodingErrorAction malformedInputAction ()
264 return malformedInputAction;
267 public final float maxCharsPerByte ()
269 return maxCharsPerByte;
272 public final CharsetDecoder onUnmappableCharacter
273 (CodingErrorAction newAction)
275 if (newAction == null)
276 throw new IllegalArgumentException ("Null action");
278 unmappableCharacterAction = newAction;
279 implOnUnmappableCharacter (newAction);
280 return this;
283 public final String replacement ()
285 return replacement;
288 public final CharsetDecoder replaceWith (String newReplacement)
290 if (newReplacement == null)
291 throw new IllegalArgumentException ("Null replacement");
292 if (newReplacement.length () == 0)
293 throw new IllegalArgumentException ("Empty replacement");
294 // XXX: what about maxCharsPerByte?
296 this.replacement = newReplacement;
297 implReplaceWith (newReplacement);
298 return this;
301 public final CharsetDecoder reset ()
303 state = STATE_RESET;
304 implReset ();
305 return this;
308 public CodingErrorAction unmappableCharacterAction ()
310 return unmappableCharacterAction;