Search in sources :

Example 61 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 62 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)

Example 63 with CharBuffer

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

the class DoubleArrayTrie method addToTail.

/**
     * Add characters(nodes) to tail array
     *
     * @param node
     */
private void addToTail(Trie.Node node) {
    while (true) {
        if (tailBuffer.capacity() < tailIndex - TAIL_OFFSET + 1) {
            CharBuffer newTailBuffer = CharBuffer.allocate(tailBuffer.capacity() + (int) (tailBuffer.capacity() * BUFFER_GROWTH_PERCENTAGE));
            tailBuffer.rewind();
            newTailBuffer.put(tailBuffer);
            tailBuffer = newTailBuffer;
        }
        // set character of current node
        tailBuffer.put(tailIndex++ - TAIL_OFFSET, node.getKey());
        if (node.getChildren().isEmpty()) {
            // if it reached the end of input, break.
            break;
        }
        // Move to next node
        node = node.getChildren().get(0);
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 64 with CharBuffer

use of java.nio.CharBuffer in project jetty.project by eclipse.

the class UnixSocketClient method main.

public static void main(String[] args) throws Exception {
    java.io.File path = new java.io.File("/tmp/jetty.sock");
    String data = "GET / HTTP/1.1\r\nHost: unixsock\r\n\r\n";
    UnixSocketAddress address = new UnixSocketAddress(path);
    UnixSocketChannel channel = UnixSocketChannel.open(address);
    System.out.println("connected to " + channel.getRemoteSocketAddress());
    PrintWriter w = new PrintWriter(Channels.newOutputStream(channel));
    InputStreamReader r = new InputStreamReader(Channels.newInputStream(channel));
    while (true) {
        w.print(data);
        w.flush();
        CharBuffer result = CharBuffer.allocate(4096);
        r.read(result);
        result.flip();
        System.out.println("read from server: " + result.toString());
        Thread.sleep(1000);
    }
}
Also used : UnixSocketChannel(jnr.unixsocket.UnixSocketChannel) InputStreamReader(java.io.InputStreamReader) CharBuffer(java.nio.CharBuffer) UnixSocketAddress(jnr.unixsocket.UnixSocketAddress) PrintWriter(java.io.PrintWriter)

Example 65 with CharBuffer

use of java.nio.CharBuffer in project tomcat by apache.

the class InputBuffer method makeSpace.

private void makeSpace(int count) {
    int desiredSize = cb.limit() + count;
    if (desiredSize > readLimit) {
        desiredSize = readLimit;
    }
    if (desiredSize <= cb.capacity()) {
        return;
    }
    int newSize = 2 * cb.capacity();
    if (desiredSize >= newSize) {
        newSize = 2 * cb.capacity() + count;
    }
    if (newSize > readLimit) {
        newSize = readLimit;
    }
    CharBuffer tmp = CharBuffer.allocate(newSize);
    int oldPosition = cb.position();
    cb.position(0);
    tmp.put(cb);
    tmp.flip();
    tmp.position(oldPosition);
    cb = tmp;
    tmp = null;
}
Also used : CharBuffer(java.nio.CharBuffer)

Aggregations

CharBuffer (java.nio.CharBuffer)401 ByteBuffer (java.nio.ByteBuffer)152 CoderResult (java.nio.charset.CoderResult)83 CharsetDecoder (java.nio.charset.CharsetDecoder)47 IOException (java.io.IOException)45 Charset (java.nio.charset.Charset)33 Test (org.junit.Test)23 CharacterCodingException (java.nio.charset.CharacterCodingException)15 CharsetEncoder (java.nio.charset.CharsetEncoder)15 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 InputStream (java.io.InputStream)6