Search in sources :

Example 1 with ByteBufferStreamInput

use of org.elasticsearch.common.io.stream.ByteBufferStreamInput in project elasticsearch by elastic.

the class DeflateCompressTests method doTest.

private void doTest(byte[] bytes) throws IOException {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    StreamInput rawIn = new ByteBufferStreamInput(bb);
    Compressor c = compressor;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStreamStreamOutput rawOs = new OutputStreamStreamOutput(bos);
    StreamOutput os = c.streamOutput(rawOs);
    Random r = random();
    int bufferSize = r.nextBoolean() ? 65535 : TestUtil.nextInt(random(), 1, 70000);
    int prepadding = r.nextInt(70000);
    int postpadding = r.nextInt(70000);
    byte[] buffer = new byte[prepadding + bufferSize + postpadding];
    // fill block completely with junk
    r.nextBytes(buffer);
    int len;
    while ((len = rawIn.read(buffer, prepadding, bufferSize)) != -1) {
        os.write(buffer, prepadding, len);
    }
    os.close();
    rawIn.close();
    // now we have compressed byte array
    byte[] compressed = bos.toByteArray();
    ByteBuffer bb2 = ByteBuffer.wrap(compressed);
    StreamInput compressedIn = new ByteBufferStreamInput(bb2);
    StreamInput in = c.streamInput(compressedIn);
    // randomize constants again
    bufferSize = r.nextBoolean() ? 65535 : TestUtil.nextInt(random(), 1, 70000);
    prepadding = r.nextInt(70000);
    postpadding = r.nextInt(70000);
    buffer = new byte[prepadding + bufferSize + postpadding];
    // fill block completely with junk
    r.nextBytes(buffer);
    ByteArrayOutputStream uncompressedOut = new ByteArrayOutputStream();
    while ((len = in.read(buffer, prepadding, bufferSize)) != -1) {
        uncompressedOut.write(buffer, prepadding, len);
    }
    uncompressedOut.close();
    assertArrayEquals(bytes, uncompressedOut.toByteArray());
}
Also used : ByteBufferStreamInput(org.elasticsearch.common.io.stream.ByteBufferStreamInput) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Random(java.util.Random) ByteBufferStreamInput(org.elasticsearch.common.io.stream.ByteBufferStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ByteBufferStreamInput

use of org.elasticsearch.common.io.stream.ByteBufferStreamInput in project elasticsearch by elastic.

the class BaseTranslogReader method checksummedStream.

/**
     * reads an operation at the given position and returns it. The buffer length is equal to the number
     * of bytes reads.
     */
protected final BufferedChecksumStreamInput checksummedStream(ByteBuffer reusableBuffer, long position, int opSize, BufferedChecksumStreamInput reuse) throws IOException {
    final ByteBuffer buffer;
    if (reusableBuffer.capacity() >= opSize) {
        buffer = reusableBuffer;
    } else {
        buffer = ByteBuffer.allocate(opSize);
    }
    buffer.clear();
    buffer.limit(opSize);
    readBytes(buffer, position);
    buffer.flip();
    return new BufferedChecksumStreamInput(new ByteBufferStreamInput(buffer), reuse);
}
Also used : ByteBufferStreamInput(org.elasticsearch.common.io.stream.ByteBufferStreamInput) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ByteBufferStreamInput

use of org.elasticsearch.common.io.stream.ByteBufferStreamInput in project elasticsearch by elastic.

the class UnassignedInfoTests method testAllocationStatusSerialization.

public void testAllocationStatusSerialization() throws IOException {
    for (AllocationStatus allocationStatus : AllocationStatus.values()) {
        BytesStreamOutput out = new BytesStreamOutput();
        allocationStatus.writeTo(out);
        ByteBufferStreamInput in = new ByteBufferStreamInput(ByteBuffer.wrap(out.bytes().toBytesRef().bytes));
        AllocationStatus readStatus = AllocationStatus.readFrom(in);
        assertThat(readStatus, equalTo(allocationStatus));
    }
}
Also used : ByteBufferStreamInput(org.elasticsearch.common.io.stream.ByteBufferStreamInput) AllocationStatus(org.elasticsearch.cluster.routing.UnassignedInfo.AllocationStatus) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 4 with ByteBufferStreamInput

use of org.elasticsearch.common.io.stream.ByteBufferStreamInput in project elasticsearch by elastic.

the class ActiveShardCountTests method doWriteRead.

private void doWriteRead(ActiveShardCount activeShardCount) throws IOException {
    final BytesStreamOutput out = new BytesStreamOutput();
    activeShardCount.writeTo(out);
    final ByteBufferStreamInput in = new ByteBufferStreamInput(ByteBuffer.wrap(out.bytes().toBytesRef().bytes));
    ActiveShardCount readActiveShardCount = ActiveShardCount.readFrom(in);
    if (activeShardCount == ActiveShardCount.DEFAULT || activeShardCount == ActiveShardCount.ALL || activeShardCount == ActiveShardCount.NONE) {
        assertSame(activeShardCount, readActiveShardCount);
    } else {
        assertEquals(activeShardCount, readActiveShardCount);
    }
}
Also used : ByteBufferStreamInput(org.elasticsearch.common.io.stream.ByteBufferStreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

ByteBufferStreamInput (org.elasticsearch.common.io.stream.ByteBufferStreamInput)4 ByteBuffer (java.nio.ByteBuffer)2 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Random (java.util.Random)1 AllocationStatus (org.elasticsearch.cluster.routing.UnassignedInfo.AllocationStatus)1 OutputStreamStreamOutput (org.elasticsearch.common.io.stream.OutputStreamStreamOutput)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)1