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