org.elasticsearch.common.io.stream.OutputStreamStreamOutput Java Examples

The following examples show how to use org.elasticsearch.common.io.stream.OutputStreamStreamOutput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlobStoreRepository.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Writes snapshot index file
 * <p>
 * This file can be used by read-only repositories that are unable to list files in the repository
 *
 * @param snapshots list of snapshot ids
 * @throws IOException I/O errors
 */
protected void writeSnapshotList(List<SnapshotId> snapshots) throws IOException {
    final BytesReference bRef;
    try(BytesStreamOutput bStream = new BytesStreamOutput()) {
        try(StreamOutput stream = new OutputStreamStreamOutput(bStream)) {
            XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, stream);
            builder.startObject();
            builder.startArray("snapshots");
            for (SnapshotId snapshot : snapshots) {
                builder.value(snapshot.getSnapshot());
            }
            builder.endArray();
            builder.endObject();
            builder.close();
        }
        bRef = bStream.bytes();
    }
    if (snapshotsBlobContainer.blobExists(SNAPSHOTS_FILE)) {
        snapshotsBlobContainer.deleteBlob(SNAPSHOTS_FILE);
    }
    snapshotsBlobContainer.writeBlob(SNAPSHOTS_FILE, bRef);
}
 
Example #2
Source File: DeflateCompressor.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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;
    OutputStream compressedOut = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
    compressedOut = new BufferedOutputStream(compressedOut, BUFFER_SIZE);
    return new OutputStreamStreamOutput(compressedOut) {
        private boolean closed = false;

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed == false) {
                    // important to release native memory
                    deflater.end();
                    closed = true;
                }
            }
        }
    };
}
 
Example #3
Source File: DeflateCompressor.java    From crate with Apache License 2.0 6 votes vote down vote up
@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();
                }
            }
        }
    };
}
 
Example #4
Source File: AnomalyResultResponse.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public static AnomalyResultResponse fromActionResponse(final ActionResponse actionResponse) {
    if (actionResponse instanceof AnomalyResultResponse) {
        return (AnomalyResultResponse) actionResponse;
    }

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
        actionResponse.writeTo(osso);
        try (InputStreamStreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
            return new AnomalyResultResponse(input);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("failed to parse ActionResponse into AnomalyResultResponse", e);
    }
}
 
Example #5
Source File: StopDetectorResponse.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public static StopDetectorResponse fromActionResponse(final ActionResponse actionResponse) {
    if (actionResponse instanceof StopDetectorResponse) {
        return (StopDetectorResponse) actionResponse;
    }

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
        actionResponse.writeTo(osso);
        try (InputStreamStreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
            return new StopDetectorResponse(input);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("failed to parse ActionResponse into StopDetectorResponse", e);
    }
}
 
Example #6
Source File: AnomalyResultRequest.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public static AnomalyResultRequest fromActionRequest(final ActionRequest actionRequest) {
    if (actionRequest instanceof AnomalyResultRequest) {
        return (AnomalyResultRequest) actionRequest;
    }

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
        actionRequest.writeTo(osso);
        try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
            return new AnomalyResultRequest(input);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("failed to parse ActionRequest into AnomalyResultRequest", e);
    }
}
 
Example #7
Source File: StopDetectorRequest.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public static StopDetectorRequest fromActionRequest(final ActionRequest actionRequest) {
    if (actionRequest instanceof StopDetectorRequest) {
        return (StopDetectorRequest) actionRequest;
    }

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
        actionRequest.writeTo(osso);
        try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
            return new StopDetectorRequest(input);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("failed to parse ActionRequest into StopDetectorRequest", e);
    }
}
 
Example #8
Source File: NodeStatsContextTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Test
public void testStreamContext() throws Exception {
    NodeStatsContext ctx1 = new NodeStatsContext(true);
    ctx1.id("93c7ff92-52fa-11e6-aad8-3c15c2d3ad18");
    ctx1.name("crate1");
    ctx1.hostname("crate1.example.com");
    ctx1.timestamp(100L);
    ctx1.version(Version.CURRENT);
    ctx1.build(Build.CURRENT);
    ctx1.httpPort(4200);
    ctx1.transportPort(4300);
    ctx1.restUrl("10.0.0.1:4200");
    ctx1.jvmStats(JvmStats.jvmStats());
    ctx1.osInfo(DummyOsInfo.INSTANCE);
    ProcessProbe processProbe = ProcessProbe.getInstance();
    ctx1.processStats(processProbe.processStats());
    OsProbe osProbe = OsProbe.getInstance();
    ctx1.osStats(osProbe.osStats());
    ctx1.extendedOsStats(extendedNodeInfo.osStats());
    ctx1.threadPools(threadPool.stats());
    ctx1.clusterStateVersion(10L);

    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    StreamOutput out = new OutputStreamStreamOutput(outBuffer);
    ctx1.writeTo(out);

    ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
    InputStreamStreamInput in = new InputStreamStreamInput(inBuffer);
    NodeStatsContext ctx2 = new NodeStatsContext(in, true);

    assertThat(ctx1.id(), is(ctx2.id()));
    assertThat(ctx1.name(), is(ctx2.name()));
    assertThat(ctx1.hostname(), is(ctx2.hostname()));
    assertThat(ctx1.timestamp(), is(100L));
    assertThat(ctx1.version(), is(ctx2.version()));
    assertThat(ctx1.build().hash(), is(ctx2.build().hash()));
    assertThat(ctx1.restUrl(), is(ctx2.restUrl()));
    assertThat(ctx1.httpPort(), is(ctx2.httpPort()));
    assertThat(ctx1.transportPort(), is(ctx2.transportPort()));
    assertThat(ctx1.pgPort(), is(ctx2.pgPort()));
    assertThat(ctx1.jvmStats().getTimestamp(), is(ctx2.jvmStats().getTimestamp()));
    assertThat(ctx1.osInfo().getArch(), is(ctx2.osInfo().getArch()));
    assertThat(ctx1.processStats().getTimestamp(), is(ctx2.processStats().getTimestamp()));
    assertThat(ctx1.osStats().getTimestamp(), is(ctx2.osStats().getTimestamp()));
    assertThat(ctx1.extendedOsStats().uptime(), is(ctx2.extendedOsStats().uptime()));
    assertThat(ctx1.threadPools().iterator().next().getActive(), is(ctx2.threadPools().iterator().next().getActive()));
    assertThat(ctx1.clusterStateVersion(), is(ctx2.clusterStateVersion()));
}