Search in sources :

Example 26 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream 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 27 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream 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 28 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream 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 29 with ByteArrayOutputStream

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

the class ThriftArtifactCacheProtocolTest method testSendingRequestWithPayload.

@Test
public void testSendingRequestWithPayload() throws IOException {
    int payloadSizeBytes = 42;
    ThriftArtifactCacheProtocol.Request request = ThriftArtifactCacheProtocol.createRequest(PROTOCOL, createDefaultRequest(payloadSizeBytes), ByteSource.wrap(new byte[payloadSizeBytes]));
    long expectedBytes = request.getRequestLengthBytes();
    Assert.assertTrue(expectedBytes > 0);
    try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        request.writeAndClose(stream);
        stream.flush();
        byte[] buffer = stream.toByteArray();
        Assert.assertEquals(expectedBytes, buffer.length);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 30 with ByteArrayOutputStream

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

the class ThriftArtifactCacheProtocolTest method testSendingRequestWithExtratPayloadStream.

@Test(expected = IllegalArgumentException.class)
public void testSendingRequestWithExtratPayloadStream() throws IOException {
    int payloadSizeBytes = 42;
    ThriftArtifactCacheProtocol.Request request = ThriftArtifactCacheProtocol.createRequest(PROTOCOL, createDefaultRequest(payloadSizeBytes), ByteSource.wrap(new byte[payloadSizeBytes]), ByteSource.wrap(new byte[payloadSizeBytes]));
    long expectedBytes = request.getRequestLengthBytes();
    Assert.assertTrue(expectedBytes > 0);
    try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        request.writeAndClose(stream);
        stream.flush();
        byte[] buffer = stream.toByteArray();
        Assert.assertEquals(expectedBytes, buffer.length);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

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