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); } }
/** * Decode the contents of the input ByteBuffer into a CharBuffer. * * @param endOfInput indicates end of input * @throws IOException if an I/O error occurs */ private void processInput(final boolean endOfInput) throws IOException { // Prepare decoderIn for reading decoderIn.flip(); CoderResult coderResult; while (true) { coderResult = decoder.decode(decoderIn, decoderOut, endOfInput); if (coderResult.isOverflow()) { flushOutput(); } else if (coderResult.isUnderflow()) { break; } else { // The decoder is configured to replace malformed input and unmappable characters, // so we should not get here. throw new IOException("Unexpected coder result"); } } // Discard the bytes that have been read decoderIn.compact(); }
/** * Fills the internal char buffer from the reader. * * @throws IOException * If an I/O error occurs */ private void fillBuffer() throws IOException { if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) { encoderIn.compact(); final int position = encoderIn.position(); // We don't use Reader#read(CharBuffer) here because it is more efficient // to write directly to the underlying char array (the default implementation // copies data to a temporary char array). final int c = reader.read(encoderIn.array(), position, encoderIn.remaining()); if (c == EOF) { endOfInput = true; } else { encoderIn.position(position+c); } encoderIn.flip(); } encoderOut.compact(); lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput); encoderOut.flip(); }
result = encoder.flush(byteBuffer); } else { result = encoder.encode(charBuffer, byteBuffer, endOfInput); if (result.isOverflow()) { } else if (result.isUnderflow()) { result.throwException(); return 0; // Not called.
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); } } }
while (true) { CoderResult cr = (inBuffer.hasRemaining() ? charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW); if (cr.isUnderflow()) { cr = charsetEncoder.flush(outBuffer); if (cr.isUnderflow()) { break; if (cr.isOverflow()) { writePosition(outBuffer.position()); int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar());
while (byteBuffer.hasRemaining()) { while (charBuffer.hasRemaining()) { final CoderResult result = encoder.encode(charBuffer, byteBuffer, false); if (result.isOverflow()) { return true; if (result.isUnderflow()) { filled = true; break; charBuffer.flip();
private Map<String, Object> buildStringKeyMap(ByteBuffer input) { int payloadSize = input.remaining(); if (chars == null || chars.remaining() < payloadSize) { chars = CharBuffer.allocate(payloadSize); } final CoderResult coderResult = charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .decode(input, chars, true); Map<String, Object> theMap; if (coderResult.isUnderflow()) { chars.flip(); try { theMap = parseString(chars.toString()); } finally { chars.clear(); } } else { throw new ParseException("Failed with CoderResult[%s]", coderResult); } return theMap; }
result = decoder.decode(bytes, out, false); if (result.isUnderflow()) { result = decoder.decode(bytes, out, true); decoder.flush(out); decoder.reset();
@Override public CoderResult encode(CharBuffer input, CharBuffer output, boolean endOfInput) { for (;;) { CoderResult cr1 = _first.encode(input, _buffer, endOfInput); _buffer.flip(); CoderResult cr2 = _last.encode(_buffer, output, endOfInput && cr1.isUnderflow()); _buffer.compact(); if (cr2.isOverflow()) { // no more room in output, even if we still have buffered input. return cr2; } if (cr1.isUnderflow()) { // we've consumed everything from input return cr2; } } }
protected void decodePartial(ByteBuffer nioBuffer, boolean endOfInput) { // deal with pending splitCharBuffer if (splitCharBuffer.position() > 0 && nioBuffer.hasRemaining()) { handlePendingSplitCharBuffer(nioBuffer, endOfInput); } // decode remaining buffer if (nioBuffer.hasRemaining()) { CoderResult res = decoder.decode(nioBuffer, charBuffer, endOfInput); if (res.isUnderflow()) { if (nioBuffer.remaining() > 0) { splitCharBuffer.put(nioBuffer); } } } }
private void encodeBytes(CharsetEncoder encoder, ByteBuffer output) throws IOException { CoderResult result = encoder.encode(readBuffer, output, false); if (rewindReadBuffer() && result.isUnderflow()) { encoder.encode(readBuffer, output, false); rewindReadBuffer(); } }
private void checkResult(CoderResult cr) throws IOException { if (cr.isOverflow()) { wasOverflow = true; assert bb.position() > 0; } else if (!cr.isUnderflow()) { try { cr.throwException(); } catch (CharacterCodingException e) { throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10083, encoder.charset().displayName()), e); } } else { wasOverflow = false; } }
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 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(); } } }
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();
try { while (byteBuffer.hasRemaining()) { final CoderResult result = decoder.decode(byteBuffer, charBuffer, eof); if (result.isOverflow()) { writer.write(chars, 0, charBuffer.position()); charBuffer.clear(); continue; if (result.isUnderflow()) { final int p = charBuffer.position(); if (p > 0) {
/** * Fills the internal char buffer from the reader. * * @throws java.io.IOException If an I/O error occurs */ private void fillBuffer() throws IOException { if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) { encoderIn.compact(); int position = encoderIn.position(); // We don't use Reader#read(CharBuffer) here because it is more efficient // to write directly to the underlying char array (the default implementation // copies data to a temporary char array). int c = reader.read(encoderIn.array(), position, encoderIn.remaining()); if (c == -1) { endOfInput = true; } else { encoderIn.position(position + c); } encoderIn.flip(); } encoderOut.compact(); lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput); encoderOut.flip(); }
while (true) { CoderResult cr = (inBuffer.hasRemaining() ? charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW); if (cr.isUnderflow()) { cr = charsetEncoder.flush(outBuffer); if (cr.isUnderflow()) { break; if (cr.isOverflow()) { writePosition(outBuffer.position()); int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar());