/** * Creates a new input stream that will encode the characters from {@code reader} into bytes using * the given character set encoder. * * @param reader input source * @param encoder character set encoder used for encoding chars to bytes * @param bufferSize size of internal input and output buffers * @throws IllegalArgumentException if bufferSize is non-positive */ ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) { this.reader = checkNotNull(reader); this.encoder = checkNotNull(encoder); checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize); encoder.reset(); charBuffer = CharBuffer.allocate(bufferSize); charBuffer.flip(); byteBuffer = ByteBuffer.allocate(bufferSize); }
@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(); }
final ByteBuffer bb2 = ByteBuffer.allocate(16); final CharBuffer cb2 = CharBuffer.allocate(TEST_STRING_2.length()); final int len = bytes.length; for (int i = 0; i < len; i++) { bb2.flip(); try { charsetDecoder2.decode(bb2, cb2, i == (len - 1)); } catch ( final IllegalArgumentException e){ throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. " +
@Override public String getSinkContents() throws IOException { File file = getFile(); Reader reader = new InputStreamReader(new FileInputStream(file), Charsets.UTF_8); StringBuilder builder = new StringBuilder(); CharBuffer buffer = CharBuffer.allocate(100); while (reader.read(buffer) != -1) { buffer.flip(); builder.append(buffer); buffer.clear(); } return builder.toString(); } }
/** * Tests whether the given argument is legal as this encoder's replacement byte * array. The given byte array is legal if and only if it can be decoded into * characters. */ public boolean isLegalReplacement(byte[] replacement) { if (decoder == null) { decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.REPORT); } ByteBuffer in = ByteBuffer.wrap(replacement); CharBuffer out = CharBuffer.allocate((int) (replacement.length * decoder.maxCharsPerByte())); CoderResult result = decoder.decode(in, out, true); return !result.isError(); }
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; }
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param encoder the charset encoder * @param bufferSize the size of the input buffer in number of characters * @since 2.1 */ public ReaderInputStream(final Reader reader, final CharsetEncoder encoder, final int bufferSize) { this.reader = reader; this.encoder = encoder; this.encoderIn = CharBuffer.allocate(bufferSize); this.encoderIn.flip(); this.encoderOut = ByteBuffer.allocate(128); this.encoderOut.flip(); }
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param charset the charset encoding * @param bufferSize the size of the input buffer in number of characters */ private ReaderInputStream(final Reader reader, final Charset charset, final int bufferSize) { this.reader = reader; encoder = charset.newEncoder(); encoderIn = CharBuffer.allocate(bufferSize); encoderIn.flip(); }
private static String decode(ByteBuffer bb, CharsetDecoder decoder) { CharBuffer cb = CharBuffer.allocate(128); CoderResult result = decoder.decode((ByteBuffer) bb.flip(), cb, true /* endOfInput */); if (result.isError()) { throw new IllegalArgumentException("Malformed UTF-8!"); } return ((CharBuffer) cb.flip()).toString(); }
CharsetDecoder decoder = Charset.forName(encoding).newDecoder(); CharBuffer cb = CharBuffer.allocate(100); decoder.decode(ByteBuffer.wrap(buffer1), cb, false); decoder.decode(ByteBuffer.wrap(buffer2), cb, false); ... decoder.decode(ByteBuffer.wrap(bufferN), cb, true); cb.position(0); return cb.toString();
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(); } }