use of java.nio.channels.WritableByteChannel in project druid by druid-io.
the class CompressedVSizeIndexedBenchmark method serialize.
private static ByteBuffer serialize(WritableSupplier<IndexedMultivalue<IndexedInts>> writableSupplier) throws IOException {
final ByteBuffer buffer = ByteBuffer.allocateDirect((int) writableSupplier.getSerializedSize());
WritableByteChannel channel = new WritableByteChannel() {
@Override
public int write(ByteBuffer src) throws IOException {
int size = src.remaining();
buffer.put(src);
return size;
}
@Override
public boolean isOpen() {
return true;
}
@Override
public void close() throws IOException {
}
};
writableSupplier.writeToChannel(channel);
buffer.rewind();
return buffer;
}
use of java.nio.channels.WritableByteChannel 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.channels.WritableByteChannel in project deeplearning4j by deeplearning4j.
the class IntegerArrayIO method writeArray.
public static void writeArray(OutputStream output, int[] array) throws IOException {
DataOutputStream dataOutput = new DataOutputStream(output);
int length = array.length;
dataOutput.writeInt(length);
ByteBuffer tmpBuffer = ByteBuffer.allocate(length * INT_BYTES);
IntBuffer intBuffer = tmpBuffer.asIntBuffer();
tmpBuffer.rewind();
intBuffer.put(array);
WritableByteChannel channel = Channels.newChannel(dataOutput);
channel.write(tmpBuffer);
}
use of java.nio.channels.WritableByteChannel in project deeplearning4j by deeplearning4j.
the class ConnectionCostsCompiler method compile.
@Override
public void compile() throws IOException {
DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));
dataOutput.writeInt(cardinality);
dataOutput.writeInt(bufferSize * SHORT_BYTES);
ByteBuffer byteBuffer = ByteBuffer.allocate(costs.array().length * SHORT_BYTES);
for (short cost : this.costs.array()) {
byteBuffer.putShort(cost);
}
WritableByteChannel channel = Channels.newChannel(dataOutput);
byteBuffer.flip();
channel.write(byteBuffer);
dataOutput.close();
}
use of java.nio.channels.WritableByteChannel in project druid by druid-io.
the class SerializerUtilsTest method testChannelWritelong.
@Test
public void testChannelWritelong() throws IOException {
final int index = 0;
WritableByteChannel channelOutput = Channels.newChannel(outStream);
serializerUtils.writeLong(channelOutput, longs[index]);
ByteArrayInputStream inputstream = new ByteArrayInputStream(outStream.toByteArray());
channelOutput.close();
inputstream.close();
long expected = serializerUtils.readLong(inputstream);
long actuals = longs[index];
Assert.assertEquals(expected, actuals);
}
Aggregations