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