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)));
}
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));
}
}
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());
}
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());
}
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);
}
Aggregations