This is a facade method for the encoding operation. This method encodes the remaining character sequence of the given character buffer into a new byte buffer. This method performs a complete encoding operation, resets at first, then encodes, and flushes at last. This method should not be inv
(CharBuffer in)
| 313 | * if other exception happened during the encode operation. |
| 314 | */ |
| 315 | public final ByteBuffer encode(CharBuffer in) |
| 316 | throws CharacterCodingException { |
| 317 | if (in.remaining() == 0) { |
| 318 | return ByteBuffer.allocate(0); |
| 319 | } |
| 320 | reset(); |
| 321 | int length = (int) (in.remaining() * averBytes); |
| 322 | ByteBuffer output = ByteBuffer.allocate(length); |
| 323 | CoderResult result = null; |
| 324 | while (true) { |
| 325 | result = encode(in, output, false); |
| 326 | if (result==CoderResult.UNDERFLOW) { |
| 327 | break; |
| 328 | } else if (result==CoderResult.OVERFLOW) { |
| 329 | output = allocateMore(output); |
| 330 | continue; |
| 331 | } |
| 332 | checkCoderResult(result); |
| 333 | } |
| 334 | result = encode(in, output, true); |
| 335 | checkCoderResult(result); |
| 336 | |
| 337 | while (true) { |
| 338 | result = flush(output); |
| 339 | if (result==CoderResult.UNDERFLOW) { |
| 340 | output.flip(); |
| 341 | break; |
| 342 | } else if (result==CoderResult.OVERFLOW) { |
| 343 | output = allocateMore(output); |
| 344 | continue; |
| 345 | } |
| 346 | checkCoderResult(result); |
| 347 | output.flip(); |
| 348 | if (result.isMalformed()) { |
| 349 | throw new MalformedInputException(result.length()); |
| 350 | } else if (result.isUnmappable()) { |
| 351 | throw new UnmappableCharacterException(result.length()); |
| 352 | } |
| 353 | break; |
| 354 | } |
| 355 | status = READY; |
| 356 | finished = true; |
| 357 | return output; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * checks the result whether it needs to throw CharacterCodingException. |
no test coverage detected