/** * Fills the byte output buffer from the input char buffer. * * @throws CharacterCodingException * an error encoding data */ private void fillBuffer() throws CharacterCodingException { this.bbuf.compact(); final CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); if (result.isError()) { result.throwException(); } this.bbuf.flip(); }
private void checkEncodeResult(CoderResult result) throws CharacterCodingException { if (result.isError()) { result.throwException(); } }
private static void decode(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst) { try { CoderResult cr = decoder.decode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = decoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } }
public int encode(char[] chars, int offset, int len, byte[] bytes) { ce.reset(); ByteBuffer bb = ByteBuffer.wrap(bytes); CharBuffer cb = CharBuffer.wrap(chars, offset, len); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); return bb.position(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new RuntimeException("Encode error: " + x.getMessage(), x); } } }
private void drainEncoder() throws IOException { // Strictly speaking, I think it's part of the CharsetEncoder contract that you call // encode with endOfInput true before flushing. Our ICU-based implementations don't // actually need this, and you'd hope that any reasonable implementation wouldn't either. // CharsetEncoder.encode doesn't actually pass the boolean through to encodeLoop anyway! CharBuffer chars = CharBuffer.allocate(0); while (true) { CoderResult result = encoder.encode(chars, bytes, true); if (result.isError()) { result.throwException(); } else if (result.isOverflow()) { flushBytes(false); continue; } break; } // Some encoders (such as ISO-2022-JP) have stuff to write out after all the // characters (such as shifting back into a default state). In our implementation, // this is actually the first time ICU is told that we've run out of input. CoderResult result = encoder.flush(bytes); while (!result.isUnderflow()) { if (result.isOverflow()) { flushBytes(false); result = encoder.flush(bytes); } else { result.throwException(); } } }
CoderResult cr; if (hasRemaining()) { cr = decoder.decode(buf(), out, true); } else { cr = decoder.flush(out); if (cr.isUnderflow()) { break; if (cr.isError()) { cr.throwException();
/** * Decodes the contents of {@link #buf} as much as possible to {@link #out}. * If necessary {@link #out} is further sent to {@link #writer}. * * <p> * When this method returns, the {@link #buf} is back to the 'accumulation' * mode. * * @param last * if true, tell the decoder that all the input bytes are ready. */ private void decode(boolean last) throws IOException { buf.flip(); while(true) { CoderResult r = decoder.decode(buf, out, last); if(r==CoderResult.OVERFLOW) { flushOutput(); continue; } if(r==CoderResult.UNDERFLOW) { buf.compact(); return; } // otherwise treat it as an error r.throwException(); } }
endOfInput = (bytesRead == -1); utf8BytesIn.flip(); CoderResult result = decoder.decode( utf8BytesIn, utf16CodeUnitsOut, endOfInput); if (result.isError() && decodingErrorAction.equals(CodingErrorAction.REPORT)) { result.throwException(); if (flushResult.isError() && decodingErrorAction.equals(CodingErrorAction.REPORT)) { flushResult.throwException();
private void write(final ByteBuffer buffer, final FileChannel fileChannel) { try { buffer.clear(); encoder.reset(); final CoderResult coderResult = encoder.encode(CharBuffer.wrap(builder), buffer, false); if (CoderResult.UNDERFLOW != coderResult) { coderResult.throwException(); } buffer.flip(); do { fileChannel.write(buffer); } while (buffer.remaining() > 0); } catch (final Exception ex) { LangUtil.rethrowUnchecked(ex); } } }
private void checkResult(CoderResult cr) throws IOException { if (!cr.isUnderflow() && cr.isError()) { if (cr.isMalformed() || cr.isUnmappable()) { try { cr.throwException(); } catch (CharacterCodingException e) { throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10082, cd.charset().displayName(), bytesProcessed + bb.position() + 1), e); } } cr.throwException(); } }
private static void decodeString(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst) { try { CoderResult cr = decoder.decode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = decoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } }
private void convert(CharBuffer chars) throws IOException { while (true) { CoderResult result = encoder.encode(chars, bytes, false); if (result.isOverflow()) { // Make room and try again. flushBytes(false); continue; } else if (result.isError()) { result.throwException(); } break; } }
final ByteBuffer dstBuf = dst.internalNioBuffer(dst.readerIndex(), length); final int pos = dstBuf.position(); CoderResult cr = encoder.encode(src, dstBuf, true); if (!cr.isUnderflow()) { cr.throwException(); if (!cr.isUnderflow()) { cr.throwException();
result = encoder.flush(byteBuffer); } else { result = encoder.encode(charBuffer, byteBuffer, endOfInput); } else if (result.isUnderflow()) { readMoreChars(); } else if (result.isError()) { result.throwException(); return 0; // Not called.
CoderResult cr; if (hasRemaining()) { cr = decoder.decode(buf(), out, true); } else { cr = decoder.flush(out); if (cr.isUnderflow()) { break; if (cr.isError()) { cr.throwException();
private void checkResult(final CoderResult result) throws IOException { if (result.isError()) { result.throwException(); } }
CoderResult res = decoder.decode(buf, charBuf, isEndOfInput); if (res.isMalformed() || res.isUnmappable()) { res.throwException(); res = decoder.decode(buf, charBuf, isEndOfInput); if (res.isMalformed() || res.isUnmappable()) { res.throwException();
private void checkResult(CoderResult cr) throws IOException { if (!cr.isUnderflow() && cr.isError()) { if (cr.isMalformed() || cr.isUnmappable()) { try { cr.throwException(); } catch (CharacterCodingException e) { throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10082, cd.charset().displayName(), bytesProcessed + bb.position() + 1), e); } } cr.throwException(); } }
@SuppressWarnings("ObjectToString") public static String decodeString(ByteBuffer src, Charset charset) { CharsetDecoder decoder = getDecoder(charset); CharBuffer dst = CharBuffer.allocate((int) ((double) src.remaining() * decoder.maxCharsPerByte())); try { CoderResult cr = decoder.decode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = decoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } return dst.flip().toString(); }
private void fillBuffer() throws CharacterCodingException { this.bbuf.compact(); CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); if (result.isError()) { result.throwException(); } this.bbuf.flip(); }