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); } }
@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(); }
result = decoder.decode(bytes, out, false); if (result.isUnderflow()) { result = decoder.decode(bytes, out, true); decoder.flush(out); decoder.reset();
/** * Must only be called when the input encoded bytes buffer is empty. */ private void flush() throws MalformedInputException, UnmappableCharacterException { CoderResult coderResult; decodedCharBuf.clear(); coderResult = decoder.flush(decodedCharBuf); decodedCharBuf.flip(); outputBuf.append(decodedCharBuf); throwIfError(coderResult); if (coderResult != CoderResult.UNDERFLOW) { throw new IllegalStateException("decoder flush resulted in " + coderResult); } }
private String decode() { if (null == decoder) { decoder = UTF8.newDecoder(); } snapshot(); try { if (null == chars || chars.remaining() < buffer.remaining()) { chars = CharBuffer.allocate(buffer.remaining()); } else { chars.rewind(); } decoder.reset(); CoderResult cr = decoder.decode(buffer, chars, true); if (cr.isUnderflow()) { decoder.flush(chars); } chars.flip(); return chars.toString(); } finally { reset(); } }
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 int decodeInternal(byte[] src, int srcOffset, int srcLength, char[] dst, int dstOffset) { ByteBuffer bb = ByteBuffer.wrap(src, srcOffset, srcLength); CharBuffer cb = CharBuffer.wrap(dst, dstOffset, dst.length - dstOffset); utf8_decoder.reset(); utf8_decoder.decode(bb, cb, true); utf8_decoder.flush(cb); int length = cb.position() - dstOffset; return length; }
result = decoder.decode(bytes, out, false); if (result.isUnderflow()) { result = decoder.decode(bytes, out, true); decoder.flush(out); decoder.reset();
@Override public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException { final CharsetDecoder charsetDecoder = getCharsetDecoder(); checkResult(charsetDecoder.decode(EMPTY_BIN, charbuf, true)); doDecode(false); checkResult(charsetDecoder.flush(charbuf)); doDecode(true); completed(); }
public CharBuffer decodeUnsafely(byte[] byteS, int start, int len) { // Prepare buffers if (byteBuffer.capacity() < len) { byteBuffer = ByteBuffer.allocate(len * 2); } byteBuffer.clear(); byteBuffer.put(byteS, start, len); byteBuffer.flip(); int maxChars = (int) (byteBuffer.capacity() * decoder.maxCharsPerByte()); if (charBuffer.capacity() < maxChars) { charBuffer = CharBuffer.allocate(maxChars); } charBuffer.clear(); // Decode UTF-8 decoder.reset(); decoder.decode(byteBuffer, charBuffer, true); decoder.flush(charBuffer); charBuffer.flip(); return charBuffer; } }
public static void decode(CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) { try { CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = charsetDecoder.flush(charByte); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new JSONException("utf8 decode error, " + x.getMessage(), x); } }
private int decodeInternal(byte[] src, int srcOffset, int srcLength, char[] dst, int dstOffset) { ByteBuffer bb = ByteBuffer.wrap(src, srcOffset, srcLength); CharBuffer cb = CharBuffer.wrap(dst, dstOffset, dst.length - dstOffset); utf8_decoder.reset(); utf8_decoder.decode(bb, cb, true); utf8_decoder.flush(cb); int length = cb.position() - dstOffset; return length; }
@Override public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException { final CharsetDecoder charsetDecoder = getCharsetDecoder(); checkResult(charsetDecoder.decode(EMPTY_BIN, charbuf, true)); doDecode(false); checkResult(charsetDecoder.flush(charbuf)); doDecode(true); completed(); }
public CharBuffer decodeUnsafely(byte[] byteS, int start, int len) { // Prepare buffers if (byteBuffer.capacity() < len) { byteBuffer = ByteBuffer.allocate(len * 2); } byteBuffer.clear(); byteBuffer.put(byteS, start, len); byteBuffer.flip(); int maxChars = (int) (byteBuffer.capacity() * decoder.maxCharsPerByte()); if (charBuffer.capacity() < maxChars) { charBuffer = CharBuffer.allocate(maxChars); } charBuffer.clear(); // Decode UTF-8 decoder.reset(); decoder.decode(byteBuffer, charBuffer, true); decoder.flush(charBuffer); charBuffer.flip(); return charBuffer; } }
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); } }
public String decode(byte[] buffer, int length) { utf8_decoder.reset(); ByteBuffer bb = ByteBuffer.wrap(buffer, 0, length); if(charArray.length < length) charArray = new char[length * 2]; CharBuffer cb = CharBuffer.wrap(charArray); utf8_decoder.decode(bb, cb, true); utf8_decoder.flush(cb); return new String(charArray, 0, cb.position()); }
static String decodeString(ByteBuffer src, Charset charset) { final CharsetDecoder decoder = CharsetUtil.getDecoder(charset); final 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(); }