Search in sources :

Example 6 with WritableByteChannel

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;
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 7 with WritableByteChannel

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();
}
Also used : IntBuffer(java.nio.IntBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 8 with WritableByteChannel

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);
}
Also used : IntBuffer(java.nio.IntBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 9 with WritableByteChannel

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();
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 10 with WritableByteChannel

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) Test(org.junit.Test)

Aggregations

WritableByteChannel (java.nio.channels.WritableByteChannel)111 ByteBuffer (java.nio.ByteBuffer)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ReadableByteChannel (java.nio.channels.ReadableByteChannel)27 FileOutputStream (java.io.FileOutputStream)23 Test (org.testng.annotations.Test)19 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Test (org.junit.Test)15 IOException (java.io.IOException)14 File (java.io.File)13 OutputStream (java.io.OutputStream)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)10 Vector (java.util.Vector)10 FileInputStream (java.io.FileInputStream)6 Writer (java.io.Writer)6 DbusEventIterator (com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)4 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)4 DbusEventsStatisticsCollector (com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 Map (java.util.Map)4