Search in sources :

Example 11 with StreamOutput

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());
}
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 12 with StreamOutput

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));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 13 with StreamOutput

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) DataOutputStreamOutput(org.elasticsearch.common.io.stream.DataOutputStreamOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) DataOutputStreamOutput(org.elasticsearch.common.io.stream.DataOutputStreamOutput) IOException(java.io.IOException) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 14 with StreamOutput

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 15 with StreamOutput

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();
            }
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) AbstractLifecycleRunnable(org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) Releasable(org.elasticsearch.common.lease.Releasable) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)18 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)8 OutputStreamStreamOutput (org.elasticsearch.common.io.stream.OutputStreamStreamOutput)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 CrateUnitTest (io.crate.test.integration.CrateUnitTest)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStreamStreamInput (org.elasticsearch.common.io.stream.InputStreamStreamInput)6 Test (org.junit.Test)6 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 IOException (java.io.IOException)3 StreamInput (org.elasticsearch.common.io.stream.StreamInput)3 ThreadPools (io.crate.monitor.ThreadPools)2 CompositeBytesReference (org.elasticsearch.common.bytes.CompositeBytesReference)2 ReleasableBytesStreamOutput (org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)2 Releasable (org.elasticsearch.common.lease.Releasable)2 AbstractLifecycleRunnable (org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable)2 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)2 DataOutputStream (java.io.DataOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1