Search in sources :

Example 11 with OutputStreamStreamOutput

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

the class ScriptTests method testScriptSerialization.

public void testScriptSerialization() throws IOException {
    Script expectedScript = createScript();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        expectedScript.writeTo(new OutputStreamStreamOutput(out));
        try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
            Script actualScript = new Script(new InputStreamStreamInput(in));
            assertThat(actualScript, equalTo(expectedScript));
        }
    }
}
Also used : OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 12 with OutputStreamStreamOutput

use of org.elasticsearch.common.io.stream.OutputStreamStreamOutput in project crate by crate.

the class DeflateCompressor method streamOutput.

@Override
public StreamOutput streamOutput(StreamOutput out) throws IOException {
    out.writeBytes(HEADER);
    final boolean nowrap = true;
    final Deflater deflater = new Deflater(LEVEL, nowrap);
    final boolean syncFlush = true;
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
    OutputStream compressedOut = new BufferedOutputStream(deflaterOutputStream, BUFFER_SIZE);
    return new OutputStreamStreamOutput(compressedOut) {

        final AtomicBoolean closed = new AtomicBoolean(false);

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed.compareAndSet(false, true)) {
                    // important to release native memory
                    deflater.end();
                }
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Deflater(java.util.zip.Deflater) OutputStream(java.io.OutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with OutputStreamStreamOutput

use of org.elasticsearch.common.io.stream.OutputStreamStreamOutput in project crate by crate.

the class TranslogHeader method write.

/**
 * Writes this header with the latest format into the file channel
 */
void write(final FileChannel channel) throws IOException {
    // This output is intentionally not closed because closing it will close the FileChannel.
    @SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed", "resource" }) final BufferedChecksumStreamOutput out = new BufferedChecksumStreamOutput(new OutputStreamStreamOutput(java.nio.channels.Channels.newOutputStream(channel)));
    CodecUtil.writeHeader(new OutputStreamDataOutput(out), TRANSLOG_CODEC, CURRENT_VERSION);
    // Write uuid
    final BytesRef uuid = new BytesRef(translogUUID);
    out.writeInt(uuid.length);
    out.writeBytes(uuid.bytes, uuid.offset, uuid.length);
    // Write primary term
    out.writeLong(primaryTerm);
    // Checksum header
    out.writeInt((int) out.getChecksum());
    out.flush();
    channel.force(true);
    assert channel.position() == headerSizeInBytes : "Header is not fully written; header size [" + headerSizeInBytes + "], channel position [" + channel.position() + "]";
}
Also used : OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) OutputStreamDataOutput(org.apache.lucene.store.OutputStreamDataOutput) BytesRef(org.apache.lucene.util.BytesRef)

Example 14 with OutputStreamStreamOutput

use of org.elasticsearch.common.io.stream.OutputStreamStreamOutput 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 OutputStreamStreamOutput

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

the class StoreTests method testMetadataSnapshotStreaming.

public void testMetadataSnapshotStreaming() throws Exception {
    Store.MetadataSnapshot outMetadataSnapshot = createMetaDataSnapshot();
    org.elasticsearch.Version targetNodeVersion = randomVersion(random());
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
    out.setVersion(targetNodeVersion);
    outMetadataSnapshot.writeTo(out);
    ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
    InputStreamStreamInput in = new InputStreamStreamInput(inBuffer);
    in.setVersion(targetNodeVersion);
    Store.MetadataSnapshot inMetadataSnapshot = new Store.MetadataSnapshot(in);
    Map<String, StoreFileMetaData> origEntries = new HashMap<>();
    origEntries.putAll(outMetadataSnapshot.asMap());
    for (Map.Entry<String, StoreFileMetaData> entry : inMetadataSnapshot.asMap().entrySet()) {
        assertThat(entry.getValue().name(), equalTo(origEntries.remove(entry.getKey()).name()));
    }
    assertThat(origEntries.size(), equalTo(0));
    assertThat(inMetadataSnapshot.getCommitUserData(), equalTo(outMetadataSnapshot.getCommitUserData()));
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) HashMap(java.util.HashMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Aggregations

OutputStreamStreamOutput (org.elasticsearch.common.io.stream.OutputStreamStreamOutput)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 InputStreamStreamInput (org.elasticsearch.common.io.stream.InputStreamStreamInput)16 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)9 Test (org.junit.Test)6 CrateUnitTest (io.crate.test.integration.CrateUnitTest)5 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)3 ShardId (org.elasticsearch.index.shard.ShardId)3 ThreadPools (io.crate.monitor.ThreadPools)2 BufferedOutputStream (java.io.BufferedOutputStream)2 OutputStream (java.io.OutputStream)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Deflater (java.util.zip.Deflater)2 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)2 Version (org.elasticsearch.Version)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2