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();
}
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);
}
}
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);
}
}
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;
}
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("💩")));
}
Aggregations