Search in sources :

Example 1 with CharBuffer

use of java.nio.CharBuffer in project jvm-serializers by eishay.

the class JsonFormat method toStringBuilder.

// TODO(chrisn): See if working around java.io.Reader#read(CharBuffer)
// overhead is worthwhile
private static StringBuilder toStringBuilder(Readable input) throws IOException {
    StringBuilder text = new StringBuilder();
    CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
    while (true) {
        int n = input.read(buffer);
        if (n == -1) {
            break;
        }
        buffer.flip();
        text.append(buffer, 0, n);
    }
    return text;
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 2 with CharBuffer

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

the class NulTerminatedCharsetDecoderTest method nulTerminatedBufferDecodesContentsToBuffer.

@Test
public void nulTerminatedBufferDecodesContentsToBuffer() {
    NulTerminatedCharsetDecoder decoder = new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder());
    // U+1F4A9 in UTF-8
    ByteBuffer in = decodeHex("F09F92A900");
    CharBuffer out = CharBuffer.allocate(2);
    assertThat(in.position(), is(equalTo(0)));
    assertThat(in.limit(), is(equalTo(5)));
    assertThat(out.position(), is(equalTo(0)));
    assertThat(out.limit(), is(equalTo(2)));
    NulTerminatedCharsetDecoder.Result result = decoder.decode(in, out, true);
    assertThat(result, is(equalTo(new NulTerminatedCharsetDecoder.Result(true, CoderResult.UNDERFLOW))));
    assertThat(in.position(), is(equalTo(5)));
    assertThat(in.limit(), is(equalTo(5)));
    assertThat(out.position(), is(equalTo(2)));
    assertThat(out.limit(), is(equalTo(2)));
    out.flip();
    // U+1F4A9 in Java
    assertThat(out.toString(), is(equalTo("💩")));
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 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 4 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 5 with CharBuffer

use of java.nio.CharBuffer in project deeplearning4j by deeplearning4j.

the class DoubleArrayTrie method write.

public void write(OutputStream output) throws IOException {
    baseBuffer.rewind();
    checkBuffer.rewind();
    tailBuffer.rewind();
    int baseCheckSize = Math.min(maxBaseCheckIndex + 64, baseBuffer.capacity());
    int tailSize = Math.min(tailIndex - TAIL_OFFSET + 64, tailBuffer.capacity());
    DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));
    dataOutput.writeBoolean(compact);
    dataOutput.writeInt(baseCheckSize);
    dataOutput.writeInt(tailSize);
    WritableByteChannel channel = Channels.newChannel(dataOutput);
    ByteBuffer tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    IntBuffer tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(baseBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);
    tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(checkBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);
    tmpBuffer = ByteBuffer.allocate(tailSize * 2);
    CharBuffer tmpCharBuffer = tmpBuffer.asCharBuffer();
    tmpCharBuffer.put(tailBuffer.array(), 0, tailSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);
    dataOutput.flush();
}
Also used : IntBuffer(java.nio.IntBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharBuffer (java.nio.CharBuffer)707 ByteBuffer (java.nio.ByteBuffer)279 CoderResult (java.nio.charset.CoderResult)137 CharsetDecoder (java.nio.charset.CharsetDecoder)93 IOException (java.io.IOException)74 Charset (java.nio.charset.Charset)66 CharacterCodingException (java.nio.charset.CharacterCodingException)48 Test (org.junit.Test)47 CharsetEncoder (java.nio.charset.CharsetEncoder)42 FileInputStream (java.io.FileInputStream)20 Reader (java.io.Reader)18 BufferOverflowException (java.nio.BufferOverflowException)17 FileChannel (java.nio.channels.FileChannel)16 InputStream (java.io.InputStream)15 IntBuffer (java.nio.IntBuffer)13 MappedByteBuffer (java.nio.MappedByteBuffer)12 DoubleBuffer (java.nio.DoubleBuffer)11 FloatBuffer (java.nio.FloatBuffer)11 LongBuffer (java.nio.LongBuffer)11 ShortBuffer (java.nio.ShortBuffer)11