Search in sources :

Example 31 with ByteArrayOutputStream

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

the class HttpArtifactCacheBinaryProtocolTest method testStoreRequest.

@Test
public void testStoreRequest() throws IOException {
    final RuleKey ruleKey = new RuleKey("00000000010000000000008000000000");
    final RuleKey ruleKey2 = new RuleKey("90000000000000000000008000000005");
    final String data = "data";
    ImmutableMap<String, String> metadata = ImmutableMap.of("metaKey", "metaValue");
    HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest(ArtifactInfo.builder().addRuleKeys(ruleKey, ruleKey2).setMetadata(metadata).build(), new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
        }
    });
    ByteArrayOutputStream storeRequestOutputStream = new ByteArrayOutputStream();
    storeRequest.write(storeRequestOutputStream);
    ByteArrayOutputStream storeRequestPayloadStream = new ByteArrayOutputStream();
    StoreResponseReadResult readStoreRequest = HttpArtifactCacheBinaryProtocol.readStoreRequest(new DataInputStream(new ByteArrayInputStream(storeRequestOutputStream.toByteArray())), storeRequestPayloadStream);
    assertThat(readStoreRequest.getRuleKeys(), Matchers.containsInAnyOrder(ruleKey, ruleKey2));
    assertThat(readStoreRequest.getMetadata(), Matchers.equalTo(metadata));
    assertThat(storeRequestPayloadStream.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8)));
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteSource(com.google.common.io.ByteSource) Test(org.junit.Test)

Example 32 with ByteArrayOutputStream

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

the class HttpArtifactCacheBinaryProtocolTest method testReadFetchResponse.

@Test
public void testReadFetchResponse() throws IOException {
    final String base64EncodedData = "AAAALgAAAAEAIDAwMDAwMDAwMDEwMDAwMDAwMDAwMDA4MDAwMDAwMDAwAAAAANcwdr5kYXRh";
    final RuleKey ruleKey = new RuleKey("00000000010000000000008000000000");
    final String data = "data";
    byte[] expectedData;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(out)) {
        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();
    }
    assertThat(expectedData, Matchers.equalTo(BaseEncoding.base64().decode(base64EncodedData)));
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(expectedData))) {
        FetchResponseReadResult result = HttpArtifactCacheBinaryProtocol.readFetchResponse(inputStream, outputStream);
        assertThat(result.getRuleKeys(), Matchers.contains(ruleKey));
        assertThat(outputStream.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8)));
        assertThat(result.getActualHashCode(), Matchers.equalTo(HashCode.fromString("d73076be")));
        assertThat(result.getExpectedHashCode(), Matchers.equalTo(HashCode.fromString("d73076be")));
        assertThat(result.getMetadata(), Matchers.anEmptyMap());
        assertThat(result.getResponseSizeBytes(), Matchers.equalTo(4L));
    }
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 33 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.

the class HttpClientGZIPTest method testGZIPContentSentTwiceInOneWrite.

@Test
public void testGZIPContentSentTwiceInOneWrite() throws Exception {
    final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader("Content-Encoding", "gzip");
            ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
            GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
            gzipOutput.write(data);
            gzipOutput.finish();
            byte[] gzipBytes = gzipData.toByteArray();
            byte[] content = Arrays.copyOf(gzipBytes, 2 * gzipBytes.length);
            System.arraycopy(gzipBytes, 0, content, gzipBytes.length, gzipBytes.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(content);
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    byte[] expected = Arrays.copyOf(data, 2 * data.length);
    System.arraycopy(data, 0, expected, data.length, data.length);
    Assert.assertArrayEquals(expected, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPOutputStream(java.util.zip.GZIPOutputStream) Test(org.junit.Test)

Example 34 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.

the class HttpClientGZIPTest method testGZIPContentFragmented.

private void testGZIPContentFragmented(final int fragment) throws Exception {
    final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader("Content-Encoding", "gzip");
            ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
            GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
            gzipOutput.write(data);
            gzipOutput.finish();
            byte[] gzipBytes = gzipData.toByteArray();
            byte[] chunk1 = Arrays.copyOfRange(gzipBytes, 0, gzipBytes.length - fragment);
            byte[] chunk2 = Arrays.copyOfRange(gzipBytes, gzipBytes.length - fragment, gzipBytes.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            output.flush();
            sleep(500);
            output.write(chunk2);
            output.flush();
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 35 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testStreamNoBlocks.

@Test
public void testStreamNoBlocks() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.close();
    byte[] bytes = baos.toByteArray();
    GZIPInputStream input = new GZIPInputStream(new ByteArrayInputStream(bytes), 1);
    int read = input.read();
    assertEquals(-1, read);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) 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