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