1. /*
  2. * @(#)Charset-X-Coder.java 1.37 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. // -- This file was mechanically generated: Do not edit! -- //
  8. package java.nio.charset;
  9. import java.nio.Buffer;
  10. import java.nio.ByteBuffer;
  11. import java.nio.CharBuffer;
  12. import java.nio.BufferOverflowException;
  13. import java.nio.BufferUnderflowException;
  14. import java.lang.ref.WeakReference;
  15. import java.nio.charset.CoderMalfunctionError; // javadoc
  16. /**
  17. * An engine that can transform a sequence of bytes in a specific charset into a sequence of
  18. * sixteen-bit Unicode characters.
  19. *
  20. * <a name="steps">
  21. *
  22. * <p> The input byte sequence is provided in a byte buffer or a series
  23. * of such buffers. The output character sequence is written to a character buffer
  24. * or a series of such buffers. A decoder should always be used by making
  25. * the following sequence of method invocations, hereinafter referred to as a
  26. * <i>decoding operation</i>:
  27. *
  28. * <ol>
  29. *
  30. * <li><p> Reset the decoder via the {@link #reset reset} method, unless it
  31. * has not been used before; </p></li>
  32. *
  33. * <li><p> Invoke the {@link #decode decode} method zero or more times, as
  34. * long as additional input may be available, passing <tt>false</tt> for the
  35. * <tt>endOfInput</tt> argument and filling the input buffer and flushing the
  36. * output buffer between invocations; </p></li>
  37. *
  38. * <li><p> Invoke the {@link #decode decode} method one final time, passing
  39. * <tt>true</tt> for the <tt>endOfInput</tt> argument; and then </p></li>
  40. *
  41. * <li><p> Invoke the {@link #flush flush} method so that the decoder can
  42. * flush any internal state to the output buffer. </p></li>
  43. *
  44. * </ol>
  45. *
  46. * Each invocation of the {@link #decode decode} method will decode as many
  47. * bytes as possible from the input buffer, writing the resulting characters
  48. * to the output buffer. The {@link #decode decode} method returns when more
  49. * input is required, when there is not enough room in the output buffer, or
  50. * when a decoding error has occurred. In each case a {@link CoderResult}
  51. * object is returned to describe the reason for termination. An invoker can
  52. * examine this object and fill the input buffer, flush the output buffer, or
  53. * attempt to recover from a decoding error, as appropriate, and try again.
  54. *
  55. * <a name="ce">
  56. *
  57. * <p> There are two general types of decoding errors. If the input byte
  58. * sequence is not legal for this charset then the input is considered <i>malformed</i>. If
  59. * the input byte sequence is legal but cannot be mapped to a valid
  60. * Unicode character then an <i>unmappable character</i> has been encountered.
  61. *
  62. * <a name="cae">
  63. *
  64. * <p> How a decoding error is handled depends upon the action requested for
  65. * that type of error, which is described by an instance of the {@link
  66. * CodingErrorAction} class. The possible error actions are to {@link
  67. * CodingErrorAction#IGNORE </code>ignore<code>} the erroneous input, {@link
  68. * CodingErrorAction#REPORT </code>report<code>} the error to the invoker via
  69. * the returned {@link CoderResult} object, or {@link CodingErrorAction#REPLACE
  70. * </code>replace<code>} the erroneous input with the current value of the
  71. * replacement string. The replacement
  72. *
  73. * has the initial value <tt>"\uFFFD"</tt>
  74. *
  75. * its value may be changed via the {@link #replaceWith(java.lang.String)
  76. * replaceWith} method.
  77. *
  78. * <p> The default action for malformed-input and unmappable-character errors
  79. * is to {@link CodingErrorAction#REPORT </code>report<code>} them. The
  80. * malformed-input error action may be changed via the {@link
  81. * #onMalformedInput(CodingErrorAction) onMalformedInput} method; the
  82. * unmappable-character action may be changed via the {@link
  83. * #onUnmappableCharacter(CodingErrorAction) onUnmappableCharacter} method.
  84. *
  85. * <p> This class is designed to handle many of the details of the decoding
  86. * process, including the implementation of error actions. A decoder for a
  87. * specific charset, which is a concrete subclass of this class, need only
  88. * implement the abstract {@link #decodeLoop decodeLoop} method, which
  89. * encapsulates the basic decoding loop. A subclass that maintains internal
  90. * state should, additionally, override the {@link #flush flush} and {@link
  91. * #reset reset} methods.
  92. *
  93. * <p> Instances of this class are not safe for use by multiple concurrent
  94. * threads. </p>
  95. *
  96. *
  97. * @version 1.37, 03/01/23
  98. * @author Mark Reinhold
  99. * @author JSR-51 Expert Group
  100. * @since 1.4
  101. *
  102. * @see ByteBuffer
  103. * @see CharBuffer
  104. * @see Charset
  105. * @see CharsetEncoder
  106. */
  107. public abstract class CharsetDecoder {
  108. private final Charset charset;
  109. private final float averageCharsPerByte;
  110. private final float maxCharsPerByte;
  111. private String replacement;
  112. private CodingErrorAction malformedInputAction
  113. = CodingErrorAction.REPORT;
  114. private CodingErrorAction unmappableCharacterAction
  115. = CodingErrorAction.REPORT;
  116. // Internal states
  117. //
  118. private static final int ST_RESET = 0;
  119. private static final int ST_CODING = 1;
  120. private static final int ST_END = 2;
  121. private static final int ST_FLUSHED = 3;
  122. private int state = ST_RESET;
  123. private static String stateNames[]
  124. = { "RESET", "CODING", "CODING_END", "FLUSHED" };
  125. /**
  126. * Initializes a new decoder. The new decoder will have the given
  127. * chars-per-byte and replacement values. </p>
  128. *
  129. * @param averageCharsPerByte
  130. * A positive float value indicating the expected number of
  131. * characters that will be produced for each input byte
  132. *
  133. * @param maxCharsPerByte
  134. * A positive float value indicating the maximum number of
  135. * characters that will be produced for each input byte
  136. *
  137. * @param replacement
  138. * The initial replacement; must not be <tt>null</tt>, must have
  139. * non-zero length, must not be longer than maxCharsPerByte,
  140. * and must be {@link #isLegalReplacement </code>legal<code>}
  141. *
  142. * @throws IllegalArgumentException
  143. * If the preconditions on the parameters do not hold
  144. */
  145. private
  146. CharsetDecoder(Charset cs,
  147. float averageCharsPerByte,
  148. float maxCharsPerByte,
  149. String replacement)
  150. {
  151. this.charset = cs;
  152. if (averageCharsPerByte <= 0.0f)
  153. throw new IllegalArgumentException("Non-positive "
  154. + "averageCharsPerByte");
  155. if (maxCharsPerByte <= 0.0f)
  156. throw new IllegalArgumentException("Non-positive "
  157. + "maxCharsPerByte");
  158. this.replacement = replacement;
  159. this.averageCharsPerByte = averageCharsPerByte;
  160. this.maxCharsPerByte = maxCharsPerByte;
  161. replaceWith(replacement);
  162. }
  163. /**
  164. * Initializes a new decoder. The new decoder will have the given
  165. * chars-per-byte values and its replacement will be the
  166. * string <tt>"\uFFFD"</tt>. </p>
  167. *
  168. * @param averageCharsPerByte
  169. * A positive float value indicating the expected number of
  170. * characters that will be produced for each input byte
  171. *
  172. * @param maxCharsPerByte
  173. * A positive float value indicating the maximum number of
  174. * characters that will be produced for each input byte
  175. *
  176. * @throws IllegalArgumentException
  177. * If the preconditions on the parameters do not hold
  178. */
  179. protected CharsetDecoder(Charset cs,
  180. float averageCharsPerByte,
  181. float maxCharsPerByte)
  182. {
  183. this(cs,
  184. averageCharsPerByte, maxCharsPerByte,
  185. "\uFFFD");
  186. }
  187. /**
  188. * Returns the charset that created this decoder. </p>
  189. *
  190. * @return This decoder's charset
  191. */
  192. public final Charset charset() {
  193. return charset;
  194. }
  195. /**
  196. * Returns this decoder's replacement value. </p>
  197. *
  198. * @return This decoder's current replacement,
  199. * which is never <tt>null</tt> and is never empty
  200. */
  201. public final String replacement() {
  202. return replacement;
  203. }
  204. /**
  205. * Changes this decoder's replacement value.
  206. *
  207. * <p> This method invokes the {@link #implReplaceWith implReplaceWith}
  208. * method, passing the new replacement, after checking that the new
  209. * replacement is acceptable. </p>
  210. *
  211. * @param newReplacement
  212. *
  213. * The new replacement; must not be <tt>null</tt>
  214. * and must have non-zero length
  215. *
  216. * @return This decoder
  217. *
  218. * @throws IllegalArgumentException
  219. * If the preconditions on the parameter do not hold
  220. */
  221. public final CharsetDecoder replaceWith(String newReplacement) {
  222. if (newReplacement == null)
  223. throw new IllegalArgumentException("Null replacement");
  224. int len = newReplacement.length();
  225. if (len == 0)
  226. throw new IllegalArgumentException("Empty replacement");
  227. if (len > maxCharsPerByte)
  228. throw new IllegalArgumentException("Replacement too long");
  229. this.replacement = newReplacement;
  230. implReplaceWith(newReplacement);
  231. return this;
  232. }
  233. /**
  234. * Reports a change to this decoder's replacement value.
  235. *
  236. * <p> The default implementation of this method does nothing. This method
  237. * should be overridden by decoders that require notification of changes to
  238. * the replacement. </p>
  239. *
  240. * @param newReplacement
  241. */
  242. protected void implReplaceWith(String newReplacement) {
  243. }
  244. /**
  245. * Returns this decoder's current action for malformed-input errors. </p>
  246. *
  247. * @return The current malformed-input action, which is never <tt>null</tt>
  248. */
  249. public CodingErrorAction malformedInputAction() {
  250. return malformedInputAction;
  251. }
  252. /**
  253. * Changes this decoder's action for malformed-input errors. </p>
  254. *
  255. * <p> This method invokes the {@link #implOnMalformedInput
  256. * implOnMalformedInput} method, passing the new action. </p>
  257. *
  258. * @param newAction The new action; must not be <tt>null</tt>
  259. *
  260. * @return This decoder
  261. *
  262. * @throws IllegalArgumentException
  263. * If the precondition on the parameter does not hold
  264. */
  265. public final CharsetDecoder onMalformedInput(CodingErrorAction newAction) {
  266. if (newAction == null)
  267. throw new IllegalArgumentException("Null action");
  268. malformedInputAction = newAction;
  269. implOnMalformedInput(newAction);
  270. return this;
  271. }
  272. /**
  273. * Reports a change to this decoder's malformed-input action.
  274. *
  275. * <p> The default implementation of this method does nothing. This method
  276. * should be overridden by decoders that require notification of changes to
  277. * the malformed-input action. </p>
  278. */
  279. protected void implOnMalformedInput(CodingErrorAction newAction) { }
  280. /**
  281. * Returns this decoder's current action for unmappable-character errors.
  282. * </p>
  283. *
  284. * @return The current unmappable-character action, which is never
  285. * <tt>null</tt>
  286. */
  287. public CodingErrorAction unmappableCharacterAction() {
  288. return unmappableCharacterAction;
  289. }
  290. /**
  291. * Changes this decoder's action for unmappable-character errors.
  292. *
  293. * <p> This method invokes the {@link #implOnUnmappableCharacter
  294. * implOnUnmappableCharacter} method, passing the new action. </p>
  295. *
  296. * @param newAction The new action; must not be <tt>null</tt>
  297. *
  298. * @return This decoder
  299. *
  300. * @throws IllegalArgumentException
  301. * If the precondition on the parameter does not hold
  302. */
  303. public final CharsetDecoder onUnmappableCharacter(CodingErrorAction
  304. newAction)
  305. {
  306. if (newAction == null)
  307. throw new IllegalArgumentException("Null action");
  308. unmappableCharacterAction = newAction;
  309. implOnUnmappableCharacter(newAction);
  310. return this;
  311. }
  312. /**
  313. * Reports a change to this decoder's unmappable-character action.
  314. *
  315. * <p> The default implementation of this method does nothing. This method
  316. * should be overridden by decoders that require notification of changes to
  317. * the unmappable-character action. </p>
  318. */
  319. protected void implOnUnmappableCharacter(CodingErrorAction newAction) { }
  320. /**
  321. * Returns the average number of characters that will be produced for each
  322. * byte of input. This heuristic value may be used to estimate the size
  323. * of the output buffer required for a given input sequence. </p>
  324. *
  325. * @return The average number of characters produced
  326. * per byte of input
  327. */
  328. public final float averageCharsPerByte() {
  329. return averageCharsPerByte;
  330. }
  331. /**
  332. * Returns the maximum number of characters that will be produced for each
  333. * byte of input. This value may be used to compute the worst-case size
  334. * of the output buffer required for a given input sequence. </p>
  335. *
  336. * @return The maximum number of characters that will be produced per
  337. * byte of input
  338. */
  339. public final float maxCharsPerByte() {
  340. return maxCharsPerByte;
  341. }
  342. /**
  343. * Decodes as many bytes as possible from the given input buffer,
  344. * writing the results to the given output buffer.
  345. *
  346. * <p> The buffers are read from, and written to, starting at their current
  347. * positions. At most {@link Buffer#remaining in.remaining()} bytes
  348. * will be read and at most {@link Buffer#remaining out.remaining()}
  349. * characters will be written. The buffers' positions will be advanced to
  350. * reflect the bytes read and the characters written, but their marks and
  351. * limits will not be modified.
  352. *
  353. * <p> In addition to reading bytes from the input buffer and writing
  354. * characters to the output buffer, this method returns a {@link CoderResult}
  355. * object to describe its reason for termination:
  356. *
  357. * <ul>
  358. *
  359. * <li><p> {@link CoderResult#UNDERFLOW} indicates that as much of the
  360. * input buffer as possible has been decoded. If there are no bytes
  361. * remaining and the invoker has no further input then the decoding
  362. * operation is complete. Otherwise there is insufficient input for the
  363. * operation to proceed, so this method should be invoked again with
  364. * further input. </p></li>
  365. *
  366. * <li><p> {@link CoderResult#OVERFLOW} indicates that the output buffer
  367. * is full. This method should be invoked again with a non-full output
  368. * buffer. </p></li>
  369. *
  370. * <li><p> A {@link CoderResult#malformedForLength
  371. * </code>malformed-input<code>} result indicates that a malformed-input
  372. * error has been detected. The malformed bytes begin at the input
  373. * buffer's (possibly incremented) position; the number of malformed
  374. * bytes may be determined by invoking the result object's {@link
  375. * CoderResult#length length} method. This case applies only if the
  376. * {@link #onMalformedInput </code>malformed action<code>} of this decoder
  377. * is {@link CodingErrorAction#REPORT}; otherwise the malformed input
  378. * will be ignored or replaced, as requested. </p></li>
  379. *
  380. * <li><p> An {@link CoderResult#unmappableForLength
  381. * </code>unmappable-character<code>} result indicates that an
  382. * unmappable-character error has been detected. The bytes that
  383. * decode the unmappable character begin at the input buffer's (possibly
  384. * incremented) position; the number of such bytes may be determined
  385. * by invoking the result object's {@link CoderResult#length length}
  386. * method. This case applies only if the {@link #onUnmappableCharacter
  387. * </code>unmappable action<code>} of this decoder is {@link
  388. * CodingErrorAction#REPORT}; otherwise the unmappable character will be
  389. * ignored or replaced, as requested. </p></li>
  390. *
  391. * </ul>
  392. *
  393. * In any case, if this method is to be reinvoked in the same decoding
  394. * operation then care should be taken to preserve any bytes remaining
  395. * in the input buffer so that they are available to the next invocation.
  396. *
  397. * <p> The <tt>endOfInput</tt> parameter advises this method as to whether
  398. * the invoker can provide further input beyond that contained in the given
  399. * input buffer. If there is a possibility of providing additional input
  400. * then the invoker should pass <tt>false</tt> for this parameter; if there
  401. * is no possibility of providing further input then the invoker should
  402. * pass <tt>true</tt>. It is not erroneous, and in fact it is quite
  403. * common, to pass <tt>false</tt> in one invocation and later discover that
  404. * no further input was actually available. It is critical, however, that
  405. * the final invocation of this method in a sequence of invocations always
  406. * pass <tt>true</tt> so that any remaining undecoded input will be treated
  407. * as being malformed.
  408. *
  409. * <p> This method works by invoking the {@link #decodeLoop decodeLoop}
  410. * method, interpreting its results, handling error conditions, and
  411. * reinvoking it as necessary. </p>
  412. *
  413. *
  414. * @param in
  415. * The input byte buffer
  416. *
  417. * @param out
  418. * The output character buffer
  419. *
  420. * @param endOfInput
  421. * <tt>true</tt> if, and only if, the invoker can provide no
  422. * additional input bytes beyond those in the given buffer
  423. *
  424. * @return A coder-result object describing the reason for termination
  425. *
  426. * @throws IllegalStateException
  427. * If a decoding operation is already in progress and the previous
  428. * step was an invocation neither of the {@link #reset reset}
  429. * method, nor of this method with a value of <tt>false</tt> for
  430. * the <tt>endOfInput</tt> parameter, nor of this method with a
  431. * value of <tt>true</tt> for the <tt>endOfInput</tt> parameter
  432. * but a return value indicating an incomplete decoding operation
  433. *
  434. * @throws CoderMalfunctionError
  435. * If an invocation of the decodeLoop method threw
  436. * an unexpected exception
  437. */
  438. public final CoderResult decode(ByteBuffer in, CharBuffer out,
  439. boolean endOfInput)
  440. {
  441. int newState = endOfInput ? ST_END : ST_CODING;
  442. if ((state != ST_RESET) && (state != ST_CODING)
  443. && !(endOfInput && (state == ST_END)))
  444. throwIllegalStateException(state, newState);
  445. state = newState;
  446. for (;;) {
  447. CoderResult cr;
  448. try {
  449. cr = decodeLoop(in, out);
  450. } catch (BufferUnderflowException x) {
  451. throw new CoderMalfunctionError(x);
  452. } catch (BufferOverflowException x) {
  453. throw new CoderMalfunctionError(x);
  454. }
  455. if (cr.isOverflow())
  456. return cr;
  457. if (cr.isUnderflow()) {
  458. if (endOfInput && in.hasRemaining()) {
  459. cr = CoderResult.malformedForLength(in.remaining());
  460. // Fall through to malformed-input case
  461. } else {
  462. return cr;
  463. }
  464. }
  465. CodingErrorAction action = null;
  466. if (cr.isMalformed())
  467. action = malformedInputAction;
  468. else if (cr.isUnmappable())
  469. action = unmappableCharacterAction;
  470. else
  471. assert false : cr.toString();
  472. if (action == CodingErrorAction.REPORT)
  473. return cr;
  474. if (action == CodingErrorAction.REPLACE) {
  475. if (out.remaining() < replacement.length())
  476. return CoderResult.OVERFLOW;
  477. out.put(replacement);
  478. }
  479. if ((action == CodingErrorAction.IGNORE)
  480. || (action == CodingErrorAction.REPLACE)) {
  481. // Skip erroneous input either way
  482. in.position(in.position() + cr.length());
  483. continue;
  484. }
  485. assert false;
  486. }
  487. }
  488. /**
  489. * Flushes this decoder.
  490. *
  491. * <p> Some decoders maintain internal state and may need to write some
  492. * final characters to the output buffer once the overall input sequence has
  493. * been read.
  494. *
  495. * <p> Any additional output is written to the output buffer beginning at
  496. * its current position. At most {@link Buffer#remaining out.remaining()}
  497. * characters will be written. The buffer's position will be advanced
  498. * appropriately, but its mark and limit will not be modified.
  499. *
  500. * <p> If this method completes successfully then it returns {@link
  501. * CoderResult#UNDERFLOW}. If there is insufficient room in the output
  502. * buffer then it returns {@link CoderResult#OVERFLOW}. If this happens
  503. * then this method must be invoked again, with an output buffer that has
  504. * more room, in order to complete the current <a href="#steps">decoding
  505. * operation</a>.
  506. *
  507. * <p> This method invokes the {@link #implFlush implFlush} method to
  508. * perform the actual flushing operation. </p>
  509. *
  510. * @param out
  511. * The output character buffer
  512. *
  513. * @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
  514. * {@link CoderResult#OVERFLOW}
  515. *
  516. * @throws IllegalStateException
  517. * If the previous step of the current decoding operation was an
  518. * invocation neither of the {@link #reset reset} method nor of
  519. * the three-argument {@link
  520. * #decode(ByteBuffer,CharBuffer,boolean) decode} method
  521. * with a value of <tt>true</tt> for the <tt>endOfInput</tt>
  522. * parameter
  523. */
  524. public final CoderResult flush(CharBuffer out) {
  525. if (state != ST_END)
  526. throwIllegalStateException(state, ST_FLUSHED);
  527. state = ST_FLUSHED;
  528. return implFlush(out);
  529. }
  530. /**
  531. * Flushes this decoder.
  532. *
  533. * <p> The default implementation of this method does nothing, and always
  534. * returns {@link CoderResult#UNDERFLOW}. This method should be overridden
  535. * by decoders that may need to write final characters to the output buffer
  536. * once the entire input sequence has been read. </p>
  537. *
  538. * @param out
  539. * The output character buffer
  540. *
  541. * @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
  542. * {@link CoderResult#OVERFLOW}
  543. */
  544. protected CoderResult implFlush(CharBuffer out) {
  545. return CoderResult.UNDERFLOW;
  546. }
  547. /**
  548. * Resets this decoder, clearing any internal state.
  549. *
  550. * <p> This method resets charset-independent state and also invokes the
  551. * {@link #implReset() implReset} method in order to perform any
  552. * charset-specific reset actions. </p>
  553. *
  554. * @return This decoder
  555. *
  556. */
  557. public final CharsetDecoder reset() {
  558. implReset();
  559. state = ST_RESET;
  560. return this;
  561. }
  562. /**
  563. * Resets this decoder, clearing any charset-specific internal state.
  564. *
  565. * <p> The default implementation of this method does nothing. This method
  566. * should be overridden by decoders that maintain internal state. </p>
  567. */
  568. protected void implReset() { }
  569. /**
  570. * Decodes one or more bytes into one or more characters.
  571. *
  572. * <p> This method encapsulates the basic decoding loop, decoding as many
  573. * bytes as possible until it either runs out of input, runs out of room
  574. * in the output buffer, or encounters a decoding error. This method is
  575. * invoked by the {@link #decode decode} method, which handles result
  576. * interpretation and error recovery.
  577. *
  578. * <p> The buffers are read from, and written to, starting at their current
  579. * positions. At most {@link Buffer#remaining in.remaining()} bytes
  580. * will be read, and at most {@link Buffer#remaining out.remaining()}
  581. * characters will be written. The buffers' positions will be advanced to
  582. * reflect the bytes read and the characters written, but their marks and
  583. * limits will not be modified.
  584. *
  585. * <p> This method returns a {@link CoderResult} object to describe its
  586. * reason for termination, in the same manner as the {@link #decode decode}
  587. * method. Most implementations of this method will handle decoding errors
  588. * by returning an appropriate result object for interpretation by the
  589. * {@link #decode decode} method. An optimized implementation may instead
  590. * examine the relevant error action and implement that action itself.
  591. *
  592. * <p> An implementation of this method may perform arbitrary lookahead by
  593. * returning {@link CoderResult#UNDERFLOW} until it receives sufficient
  594. * input. </p>
  595. *
  596. * @param in
  597. * The input byte buffer
  598. *
  599. * @param out
  600. * The output character buffer
  601. *
  602. * @return A coder-result object describing the reason for termination
  603. */
  604. protected abstract CoderResult decodeLoop(ByteBuffer in,
  605. CharBuffer out);
  606. /**
  607. * Convenience method that decodes the remaining content of a single input
  608. * byte buffer into a newly-allocated character buffer.
  609. *
  610. * <p> This method implements an entire <a href="#steps">decoding
  611. * operation</a> that is, it resets this decoder, then it decodes the
  612. * bytes in the given byte buffer, and finally it flushes this
  613. * decoder. This method should therefore not be invoked if a decoding
  614. * operation is already in progress. </p>
  615. *
  616. * @param in
  617. * The input byte buffer
  618. *
  619. * @return A newly-allocated character buffer containing the result of the
  620. * decoding operation. The buffer's position will be zero and its
  621. * limit will follow the last character written.
  622. *
  623. * @throws IllegalStateException
  624. * If a decoding operation is already in progress
  625. *
  626. * @throws MalformedInputException
  627. * If the byte sequence starting at the input buffer's current
  628. * position is not legal for this charset and the current malformed-input action
  629. * is {@link CodingErrorAction#REPORT}
  630. *
  631. * @throws UnmappableCharacterException
  632. * If the byte sequence starting at the input buffer's current
  633. * position cannot be mapped to an equivalent character sequence and
  634. * the current unmappable-character action is {@link
  635. * CodingErrorAction#REPORT}
  636. */
  637. public final CharBuffer decode(ByteBuffer in)
  638. throws CharacterCodingException
  639. {
  640. int n = (int)(in.remaining() * averageCharsPerByte());
  641. CharBuffer out = CharBuffer.allocate(n);
  642. if (n == 0)
  643. return out;
  644. reset();
  645. for (;;) {
  646. CoderResult cr;
  647. if (in.hasRemaining())
  648. cr = decode(in, out, true);
  649. else
  650. cr = flush(out);
  651. if (cr.isUnderflow())
  652. break;
  653. if (cr.isOverflow()) {
  654. n *= 2;
  655. CharBuffer o = CharBuffer.allocate(n);
  656. out.flip();
  657. o.put(out);
  658. out = o;
  659. continue;
  660. }
  661. cr.throwException();
  662. }
  663. out.flip();
  664. return out;
  665. }
  666. /**
  667. * Tells whether or not this decoder implements an auto-detecting charset.
  668. *
  669. * <p> The default implementation of this method always returns
  670. * <tt>false</tt> it should be overridden by auto-detecting decoders to
  671. * return <tt>true</tt>. </p>
  672. *
  673. * @return <tt>true</tt> if, and only if, this decoder implements an
  674. * auto-detecting charset
  675. */
  676. public boolean isAutoDetecting() {
  677. return false;
  678. }
  679. /**
  680. * Tells whether or not this decoder has yet detected a
  681. * charset  <i>(optional operation)</i>.
  682. *
  683. * <p> If this decoder implements an auto-detecting charset then at a
  684. * single point during a decoding operation this method may start returning
  685. * <tt>true</tt> to indicate that a specific charset has been detected in
  686. * the input byte sequence. Once this occurs, the {@link #detectedCharset
  687. * detectedCharset} method may be invoked to retrieve the detected charset.
  688. *
  689. * <p> That this method returns <tt>false</tt> does not imply that no bytes
  690. * have yet been decoded. Some auto-detecting decoders are capable of
  691. * decoding some, or even all, of an input byte sequence without fixing on
  692. * a particular charset.
  693. *
  694. * <p> The default implementation of this method always throws an {@link
  695. * UnsupportedOperationException}; it should be overridden by
  696. * auto-detecting decoders to return <tt>true</tt> once the input charset
  697. * has been determined. </p>
  698. *
  699. * @return <tt>true</tt> if, and only if, this decoder has detected a
  700. * specific charset
  701. *
  702. * @throws UnsupportedOperationException
  703. * If this decoder does not implement an auto-detecting charset
  704. */
  705. public boolean isCharsetDetected() {
  706. throw new UnsupportedOperationException();
  707. }
  708. /**
  709. * Retrieves the charset that was detected by this
  710. * decoder  <i>(optional operation)</i>.
  711. *
  712. * <p> If this decoder implements an auto-detecting charset then this
  713. * method returns the actual charset once it has been detected. After that
  714. * point, this method returns the same value for the duration of the
  715. * current decoding operation. If not enough input bytes have yet been
  716. * read to determine the actual charset then this method throws an {@link
  717. * IllegalStateException}.
  718. *
  719. * <p> The default implementation of this method always throws an {@link
  720. * UnsupportedOperationException}; it should be overridden by
  721. * auto-detecting decoders to return the appropriate value. </p>
  722. *
  723. * @return The charset detected by this auto-detecting decoder,
  724. * or <tt>null</tt> if the charset has not yet been determined
  725. *
  726. * @throws IllegalStateException
  727. * If insufficient bytes have been read to determine a charset
  728. *
  729. * @throws UnsupportedOperationException
  730. * If this decoder does not implement an auto-detecting charset
  731. */
  732. public Charset detectedCharset() {
  733. throw new UnsupportedOperationException();
  734. }
  735. private void throwIllegalStateException(int from, int to) {
  736. throw new IllegalStateException("Current state = " + stateNames[from]
  737. + ", new state = " + stateNames[to]);
  738. }
  739. }