Search in sources :

Example 1 with DataOutputStream

use of java.io.DataOutputStream in project jvm-serializers by eishay.

the class ProtoServerHandler method handle.

void handle(final OutputStream os, final InputStream is) throws IOException {
    RpcCallback<Message> done = new RpcCallback<Message>() {

        DataOutputStream dos = new DataOutputStream(os);

        public void run(Message content) {
            try {
                byte[] array = _serializer.serialize((MediaContent) content);
                dos.writeInt(array.length);
                dos.write(array);
                dos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    DataInputStream dis = new DataInputStream(is);
    int index = dis.readInt();
    MethodDescriptor method = getDescriptor().getMethods().get(index);
    byte[] array = new byte[dis.readInt()];
    dis.readFully(array);
    Message request = getRequestPrototype(method).newBuilderForType().mergeFrom(array).build();
    callMethod(method, null, request, done);
}
Also used : Message(com.google.protobuf.Message) DataOutputStream(java.io.DataOutputStream) RpcCallback(com.google.protobuf.RpcCallback) DataInputStream(java.io.DataInputStream) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) IOException(java.io.IOException)

Example 2 with DataOutputStream

use of java.io.DataOutputStream in project buck by facebook.

the class CompileStringsStepTest method createBinaryStream.

private byte[] createBinaryStream(File expectedFile) throws IOException {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream stream = new DataOutputStream(bos)) {
        for (String line : Files.readLines(expectedFile, Charset.defaultCharset())) {
            for (String token : Splitter.on('|').split(line)) {
                char dataType = token.charAt(0);
                String value = token.substring(2);
                switch(dataType) {
                    case 'i':
                        stream.writeInt(Integer.parseInt(value));
                        break;
                    case 's':
                        stream.writeShort(Integer.parseInt(value));
                        break;
                    case 'b':
                        stream.writeByte(Integer.parseInt(value));
                        break;
                    case 't':
                        stream.write(value.getBytes(StandardCharsets.UTF_8));
                        break;
                    default:
                        throw new RuntimeException("Unexpected data type in .fbstr file: " + dataType);
                }
            }
        }
        return bos.toByteArray();
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with DataOutputStream

use of java.io.DataOutputStream in project buck by facebook.

the class HttpArtifactCacheTest method testStore.

@Test
public void testStore() throws Exception {
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    final String data = "data";
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    Path output = Paths.get("output/file");
    filesystem.writeContentsToPath(data, output);
    final AtomicBoolean hasCalled = new AtomicBoolean(false);
    argsBuilder.setProjectFilesystem(filesystem);
    argsBuilder.setStoreClient(withMakeRequest(((path, requestBuilder) -> {
        Request request = requestBuilder.url(SERVER).build();
        hasCalled.set(true);
        Buffer buf = new Buffer();
        request.body().writeTo(buf);
        byte[] actualData = buf.readByteArray();
        byte[] expectedData;
        try (ByteArrayOutputStream out = new ByteArrayOutputStream();
            DataOutputStream dataOut = new DataOutputStream(out)) {
            dataOut.write(HttpArtifactCacheBinaryProtocol.createKeysHeader(ImmutableSet.of(ruleKey)));
            byte[] metadata = HttpArtifactCacheBinaryProtocol.createMetadataHeader(ImmutableSet.of(ruleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)));
            dataOut.writeInt(metadata.length);
            dataOut.write(metadata);
            dataOut.write(data.getBytes(Charsets.UTF_8));
            expectedData = out.toByteArray();
        }
        assertArrayEquals(expectedData, actualData);
        Response response = new Response.Builder().body(createDummyBody()).code(HttpURLConnection.HTTP_ACCEPTED).protocol(Protocol.HTTP_1_1).request(request).build();
        return new OkHttpResponseWrapper(response);
    })));
    HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
    cache.storeImpl(ArtifactInfo.builder().addRuleKeys(ruleKey).build(), output, createFinishedEventBuilder());
    assertTrue(hasCalled.get());
    cache.close();
}
Also used : Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) Buffer(okio.Buffer) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) DataOutputStream(java.io.DataOutputStream) Request(okhttp3.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Response(okhttp3.Response) HttpResponse(com.facebook.buck.slb.HttpResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 4 with DataOutputStream

use of java.io.DataOutputStream in project buck by facebook.

the class HttpArtifactCacheTest method createResponseBody.

private ResponseBody createResponseBody(ImmutableSet<RuleKey> ruleKeys, ImmutableMap<String, String> metadata, ByteSource source, String data) throws IOException {
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(out)) {
        byte[] rawMetadata = HttpArtifactCacheBinaryProtocol.createMetadataHeader(ruleKeys, metadata, source);
        dataOut.writeInt(rawMetadata.length);
        dataOut.write(rawMetadata);
        dataOut.write(data.getBytes(Charsets.UTF_8));
        return ResponseBody.create(OCTET_STREAM, out.toByteArray());
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with DataOutputStream

use of java.io.DataOutputStream in project buck by facebook.

the class ThriftArtifactCacheProtocolTest method serializeData.

// TODO(ruibm): Use Base64 response data generated from the real server implementation.
private BuckCacheResponse serializeData(String errorMessage, OutputStream rawStream, byte[]... payloads) throws IOException, TException {
    BuckCacheResponse cacheResponse = new BuckCacheResponse();
    cacheResponse.setErrorMessage(errorMessage);
    for (byte[] payload : payloads) {
        PayloadInfo info = new PayloadInfo();
        info.setSizeBytes(payload.length);
        cacheResponse.addToPayloads(info);
    }
    try (DataOutputStream stream = new DataOutputStream(rawStream)) {
        byte[] header = ThriftUtil.serialize(PROTOCOL, cacheResponse);
        stream.writeInt(header.length);
        stream.write(header);
        for (byte[] payload : payloads) {
            stream.write(payload);
        }
        stream.flush();
    }
    return cacheResponse;
}
Also used : DataOutputStream(java.io.DataOutputStream) PayloadInfo(com.facebook.buck.artifact_cache.thrift.PayloadInfo) BuckCacheResponse(com.facebook.buck.artifact_cache.thrift.BuckCacheResponse)

Aggregations

DataOutputStream (java.io.DataOutputStream)2954 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1309 IOException (java.io.IOException)1018 Test (org.junit.Test)633 DataInputStream (java.io.DataInputStream)611 FileOutputStream (java.io.FileOutputStream)426 ByteArrayInputStream (java.io.ByteArrayInputStream)409 File (java.io.File)279 BufferedOutputStream (java.io.BufferedOutputStream)227 UnitTest (org.apache.geode.test.junit.categories.UnitTest)172 URL (java.net.URL)149 InputStreamReader (java.io.InputStreamReader)142 BufferedReader (java.io.BufferedReader)138 Path (org.apache.hadoop.fs.Path)137 DataInput (java.io.DataInput)124 ArrayList (java.util.ArrayList)122 HttpURLConnection (java.net.HttpURLConnection)121 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)117 FileInputStream (java.io.FileInputStream)107 InputStream (java.io.InputStream)107