Search in sources :

Example 16 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project druid by druid-io.

the class InlineSchemaAvroBytesDecoderTest method testParse.

@Test
public void testParse() throws Exception {
    GenericRecord someAvroDatum = AvroStreamInputRowParserTest.buildSomeAvroDatum();
    Schema schema = SomeAvroDatum.getClassSchema();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
    writer.write(someAvroDatum, EncoderFactory.get().directBinaryEncoder(out, null));
    GenericRecord actual = new InlineSchemaAvroBytesDecoder(schema).parse(ByteBuffer.wrap(out.toByteArray()));
    Assert.assertEquals(someAvroDatum.get("id"), actual.get("id"));
}
Also used : Schema(org.apache.avro.Schema) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test) AvroStreamInputRowParserTest(io.druid.data.input.AvroStreamInputRowParserTest)

Example 17 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project druid by druid-io.

the class SchemaRegistryBasedAvroBytesDecoderTest method getAvroDatum.

byte[] getAvroDatum(Schema schema, GenericRecord someAvroDatum) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
    writer.write(someAvroDatum, EncoderFactory.get().directBinaryEncoder(out, null));
    return out.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 18 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project elasticsearch by elastic.

the class ChecksumBlobStoreFormat method writeBlob.

/**
     * Writes blob in atomic manner without resolving the blobName using using {@link #blobName} method.
     * <p>
     * The blob will be compressed and checksum will be written if required.
     *
     * @param obj           object to be serialized
     * @param blobContainer blob container
     * @param blobName          blob name
     */
protected void writeBlob(T obj, BlobContainer blobContainer, String blobName) throws IOException {
    BytesReference bytes = write(obj);
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        final String resourceDesc = "ChecksumBlobStoreFormat.writeBlob(blob=\"" + blobName + "\")";
        try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, blobName, byteArrayOutputStream, BUFFER_SIZE)) {
            CodecUtil.writeHeader(indexOutput, codec, VERSION);
            try (OutputStream indexOutputOutputStream = new IndexOutputOutputStream(indexOutput) {

                @Override
                public void close() throws IOException {
                // this is important since some of the XContentBuilders write bytes on close.
                // in order to write the footer we need to prevent closing the actual index input.
                }
            }) {
                bytes.writeTo(indexOutputOutputStream);
            }
            CodecUtil.writeFooter(indexOutput);
        }
        BytesArray bytesArray = new BytesArray(byteArrayOutputStream.toByteArray());
        try (InputStream stream = bytesArray.streamInput()) {
            blobContainer.writeBlob(blobName, stream, bytesArray.length());
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) OutputStreamIndexOutput(org.apache.lucene.store.OutputStreamIndexOutput) IndexOutputOutputStream(org.elasticsearch.common.lucene.store.IndexOutputOutputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IndexOutputOutputStream(org.elasticsearch.common.lucene.store.IndexOutputOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 19 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project elasticsearch by elastic.

the class RestController method handleFavicon.

void handleFavicon(RestRequest request, RestChannel channel) {
    if (request.method() == RestRequest.Method.GET) {
        try {
            try (InputStream stream = getClass().getResourceAsStream("/config/favicon.ico")) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                Streams.copy(stream, out);
                BytesRestResponse restResponse = new BytesRestResponse(RestStatus.OK, "image/x-icon", out.toByteArray());
                channel.sendResponse(restResponse);
            }
        } catch (IOException e) {
            channel.sendResponse(new BytesRestResponse(INTERNAL_SERVER_ERROR, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
        }
    } else {
        channel.sendResponse(new BytesRestResponse(FORBIDDEN, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 20 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project elasticsearch by elastic.

the class CreateIndexRequestBuilderTests method testSetSource.

/**
     * test setting the source with available setters
     */
public void testSetSource() throws IOException {
    CreateIndexRequestBuilder builder = new CreateIndexRequestBuilder(this.testClient, CreateIndexAction.INSTANCE);
    builder.setSource("{\"" + KEY + "\" : \"" + VALUE + "\"}", XContentType.JSON);
    assertEquals(VALUE, builder.request().settings().get(KEY));
    XContentBuilder xContent = XContentFactory.jsonBuilder().startObject().field(KEY, VALUE).endObject();
    xContent.close();
    builder.setSource(xContent);
    assertEquals(VALUE, builder.request().settings().get(KEY));
    ByteArrayOutputStream docOut = new ByteArrayOutputStream();
    XContentBuilder doc = XContentFactory.jsonBuilder(docOut).startObject().field(KEY, VALUE).endObject();
    doc.close();
    builder.setSource(docOut.toByteArray(), XContentType.JSON);
    assertEquals(VALUE, builder.request().settings().get(KEY));
    Map<String, String> settingsMap = new HashMap<>();
    settingsMap.put(KEY, VALUE);
    builder.setSettings(settingsMap);
    assertEquals(VALUE, builder.request().settings().get(KEY));
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)17705 ByteArrayInputStream (java.io.ByteArrayInputStream)4669 Test (org.junit.Test)4609 IOException (java.io.IOException)4326 InputStream (java.io.InputStream)1957 ObjectOutputStream (java.io.ObjectOutputStream)1679 PrintStream (java.io.PrintStream)1544 DataOutputStream (java.io.DataOutputStream)1303 ArrayList (java.util.ArrayList)875 ObjectInputStream (java.io.ObjectInputStream)818 OutputStream (java.io.OutputStream)807 File (java.io.File)770 HashMap (java.util.HashMap)558 Test (org.junit.jupiter.api.Test)526 FileInputStream (java.io.FileInputStream)460 Document (org.w3c.dom.Document)382 DataInputStream (java.io.DataInputStream)378 OutputStreamWriter (java.io.OutputStreamWriter)365 URL (java.net.URL)358 GZIPOutputStream (java.util.zip.GZIPOutputStream)338