Search in sources :

Example 11 with CharBuffer

use of java.nio.CharBuffer in project buck by facebook.

the class NulTerminatedCharsetDecoderTest method nulTerminatedEmptyBufferDecodesToEmptyBuffer.

@Test
public void nulTerminatedEmptyBufferDecodesToEmptyBuffer() {
    NulTerminatedCharsetDecoder decoder = new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder());
    ByteBuffer in = decodeHex("00");
    CharBuffer out = CharBuffer.allocate(0);
    assertThat(in.position(), is(equalTo(0)));
    assertThat(in.limit(), is(equalTo(1)));
    assertThat(out.position(), is(equalTo(0)));
    assertThat(out.limit(), is(equalTo(0)));
    NulTerminatedCharsetDecoder.Result result = decoder.decode(in, out, true);
    assertThat(in.position(), is(equalTo(1)));
    assertThat(in.limit(), is(equalTo(1)));
    assertThat(out.position(), is(equalTo(0)));
    assertThat(out.limit(), is(equalTo(0)));
    assertThat(result, is(equalTo(new NulTerminatedCharsetDecoder.Result(true, CoderResult.UNDERFLOW))));
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 12 with CharBuffer

use of java.nio.CharBuffer in project buck by facebook.

the class NulTerminatedCharsetDecoderTest method invalidUTF8BufferReturnsMalformedResult.

@Test
public void invalidUTF8BufferReturnsMalformedResult() {
    NulTerminatedCharsetDecoder decoder = new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder());
    ByteBuffer in = decodeHex("C0FFEE00");
    CharBuffer out = CharBuffer.allocate(4);
    assertThat(in.position(), is(equalTo(0)));
    assertThat(in.limit(), is(equalTo(4)));
    assertThat(out.position(), is(equalTo(0)));
    assertThat(out.limit(), is(equalTo(4)));
    NulTerminatedCharsetDecoder.Result result = decoder.decode(in, out, true);
    assertThat(result, is(equalTo(new NulTerminatedCharsetDecoder.Result(false, CoderResult.malformedForLength(1)))));
    assertThat(in.position(), is(equalTo(0)));
    assertThat(in.limit(), is(equalTo(4)));
    assertThat(out.position(), is(equalTo(0)));
    assertThat(out.limit(), is(equalTo(4)));
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 13 with CharBuffer

use of java.nio.CharBuffer in project android_frameworks_base by ParanoidAndroid.

the class FastXmlSerializer method flush.

public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Example 14 with CharBuffer

use of java.nio.CharBuffer in project feign by OpenFeign.

the class Util method toString.

/**
   * Adapted from {@code com.google.common.io.CharStreams.toString()}.
   */
public static String toString(Reader reader) throws IOException {
    if (reader == null) {
        return null;
    }
    try {
        StringBuilder to = new StringBuilder();
        CharBuffer buf = CharBuffer.allocate(BUF_SIZE);
        while (reader.read(buf) != -1) {
            buf.flip();
            to.append(buf);
            buf.clear();
        }
        return to.toString();
    } finally {
        ensureClosed(reader);
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 15 with CharBuffer

use of java.nio.CharBuffer in project fastjson by alibaba.

the class JSON method parse.

public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) {
    charsetDecoder.reset();
    int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte());
    char[] chars = allocateChars(scaleLength);
    ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len);
    CharBuffer charBuf = CharBuffer.wrap(chars);
    IOUtils.decode(charsetDecoder, byteBuf, charBuf);
    int position = charBuf.position();
    DefaultJSONParser parser = new DefaultJSONParser(chars, position, ParserConfig.getGlobalInstance(), features);
    Object value = parser.parse();
    parser.handleResovleTask(value);
    parser.close();
    return value;
}
Also used : CharBuffer(java.nio.CharBuffer) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharBuffer (java.nio.CharBuffer)387 ByteBuffer (java.nio.ByteBuffer)143 CoderResult (java.nio.charset.CoderResult)81 CharsetDecoder (java.nio.charset.CharsetDecoder)45 IOException (java.io.IOException)41 Charset (java.nio.charset.Charset)28 Test (org.junit.Test)23 CharacterCodingException (java.nio.charset.CharacterCodingException)12 CharsetEncoder (java.nio.charset.CharsetEncoder)12 FileInputStream (java.io.FileInputStream)11 IntBuffer (java.nio.IntBuffer)10 Reader (java.io.Reader)9 BufferOverflowException (java.nio.BufferOverflowException)9 DoubleBuffer (java.nio.DoubleBuffer)9 FloatBuffer (java.nio.FloatBuffer)9 LongBuffer (java.nio.LongBuffer)9 ShortBuffer (java.nio.ShortBuffer)9 BufferUnderflowException (java.nio.BufferUnderflowException)7 ValueWrapper (org.apache.geode.internal.memcached.ValueWrapper)7 ReadableByteChannel (java.nio.channels.ReadableByteChannel)6