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