use of org.elasticsearch.common.io.stream.StreamOutput 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.StreamOutput in project elasticsearch by elastic.
the class DeflateCompressedXContentTests method testDifferentCompressedRepresentation.
public void testDifferentCompressedRepresentation() throws Exception {
byte[] b = "---\nf:abcdefghijabcdefghij".getBytes("UTF-8");
BytesStreamOutput bout = new BytesStreamOutput();
StreamOutput out = compressor.streamOutput(bout);
out.writeBytes(b);
out.flush();
out.writeBytes(b);
out.close();
final BytesReference b1 = bout.bytes();
bout = new BytesStreamOutput();
out = compressor.streamOutput(bout);
out.writeBytes(b);
out.writeBytes(b);
out.close();
final BytesReference b2 = bout.bytes();
// because of the intermediate flush, the two compressed representations
// are different. It can also happen for other reasons like if hash tables
// of different size are being used
assertFalse(b1.equals(b2));
// we used the compressed representation directly and did not recompress
assertArrayEquals(BytesReference.toBytes(b1), new CompressedXContent(b1).compressed());
assertArrayEquals(BytesReference.toBytes(b2), new CompressedXContent(b2).compressed());
// but compressedstring instances are still equal
assertEquals(new CompressedXContent(b1), new CompressedXContent(b2));
}
use of org.elasticsearch.common.io.stream.StreamOutput in project elasticsearch by elastic.
the class ScriptExceptionTests method testRoundTrip.
/** ensure we can round trip in serialization */
public void testRoundTrip() throws IOException {
ScriptException e = new ScriptException("messageData", new Exception("causeData"), Arrays.asList("stack1", "stack2"), "sourceData", "langData");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
StreamOutput output = new DataOutputStreamOutput(new DataOutputStream(bytes));
e.writeTo(output);
output.close();
StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(bytes.toByteArray()));
ScriptException e2 = new ScriptException(input);
input.close();
assertEquals(e.getMessage(), e2.getMessage());
assertEquals(e.getScriptStack(), e2.getScriptStack());
assertEquals(e.getScript(), e2.getScript());
assertEquals(e.getLang(), e2.getLang());
}
use of org.elasticsearch.common.io.stream.StreamOutput in project elasticsearch by elastic.
the class BlobStoreRepository method writeIncompatibleSnapshots.
/**
* Writes the incompatible snapshot ids list to the `incompatible-snapshots` blob in the repository.
*
* Package private for testing.
*/
void writeIncompatibleSnapshots(RepositoryData repositoryData) throws IOException {
// can not write to a read only repository
assert isReadOnly() == false;
final BytesReference bytes;
try (BytesStreamOutput bStream = new BytesStreamOutput()) {
try (StreamOutput stream = new OutputStreamStreamOutput(bStream)) {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, stream);
repositoryData.incompatibleSnapshotsToXContent(builder, ToXContent.EMPTY_PARAMS);
builder.close();
}
bytes = bStream.bytes();
}
// write the incompatible snapshots blob
writeAtomic(INCOMPATIBLE_SNAPSHOTS_BLOB, bytes);
}
use of org.elasticsearch.common.io.stream.StreamOutput in project elasticsearch by elastic.
the class TcpTransport method sendResponse.
private void sendResponse(Version nodeVersion, Channel channel, final TransportResponse response, final long requestId, final String action, TransportResponseOptions options, byte status) throws IOException {
if (compress) {
options = TransportResponseOptions.builder(options).withCompress(true).build();
}
// TODO share some code with sendRequest
status = TransportStatus.setResponse(status);
ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
// we wrap this in a release once since if the onRequestSent callback throws an exception
// we might release things twice and this should be prevented
final Releasable toRelease = Releasables.releaseOnce(() -> Releasables.close(bStream.bytes()));
boolean addedReleaseListener = false;
StreamOutput stream = bStream;
try {
if (options.compress()) {
status = TransportStatus.setCompress(status);
stream = CompressorFactory.COMPRESSOR.streamOutput(stream);
}
threadPool.getThreadContext().writeTo(stream);
stream.setVersion(nodeVersion);
BytesReference reference = buildMessage(requestId, status, nodeVersion, response, stream, bStream);
final TransportResponseOptions finalOptions = options;
Runnable onRequestSent = () -> {
// this might be called in a different thread
try {
toRelease.close();
} finally {
transportServiceAdapter.onResponseSent(requestId, action, response, finalOptions);
}
};
addedReleaseListener = internalSendMessage(channel, reference, onRequestSent);
} finally {
try {
IOUtils.close(stream);
} finally {
if (!addedReleaseListener) {
toRelease.close();
}
}
}
}
Aggregations