/** * Reads the contents of an input stream and returns a byte array. * * @param inputStream the input to read from. * @return A byte array whose length is equal to the number of bytes contained. */ static byte[] _readFully(InputStream inputStream, UnsynchronizedBuffer buffer) throws IOException { final byte[] bytes = PER_THREAD_BUFFER.get(); for (;;) { int count = inputStream.read(bytes, 0, bytes.length); if (count < 0) { break; } buffer.write(bytes, 0, count); } return buffer.toArray(); }
/** * Reads the contents of an input stream and returns a byte array. * * @param inputStream the input to read from. * @return A byte array whose length is equal to the number of bytes contained. */ static byte[] _readFully(InputStream inputStream, UnsynchronizedBuffer buffer) throws IOException { final byte[] bytes = PER_THREAD_BUFFER.get(); for (;;) { int count = inputStream.read(bytes, 0, bytes.length); if (count < 0) { break; } buffer.write(bytes, 0, count); } return buffer.toArray(); }
@Override public byte[] serializeRequest(Request request) throws IOException { // Avoid BAOS for its synchronized write methods, we don't need that concurrency control UnsynchronizedBuffer out = threadLocalBuffer.get(); try { Message requestMsg = request.serialize(); // Serialization of the request may be large if (LOG.isTraceEnabled()) { LOG.trace("Serializing request '{}'", TextFormat.shortDebugString(requestMsg)); } serializeMessage(out, requestMsg); return out.toArray(); } finally { out.reset(); } }
@Override public byte[] serializeResponse(Response response) throws IOException { // Avoid BAOS for its synchronized write methods, we don't need that concurrency control UnsynchronizedBuffer out = threadLocalBuffer.get(); try { Message responseMsg = response.serialize(); // Serialization of the response may be large if (LOG.isTraceEnabled()) { LOG.trace("Serializing response '{}'", TextFormat.shortDebugString(responseMsg)); } serializeMessage(out, responseMsg); return out.toArray(); } finally { out.reset(); } }
@Override public byte[] serializeRequest(Request request) throws IOException { // Avoid BAOS for its synchronized write methods, we don't need that concurrency control UnsynchronizedBuffer out = threadLocalBuffer.get(); try { Message requestMsg = request.serialize(); // Serialization of the request may be large if (LOG.isTraceEnabled()) { LOG.trace("Serializing request '{}'", TextFormat.shortDebugString(requestMsg)); } serializeMessage(out, requestMsg); return out.toArray(); } finally { out.reset(); } }
@Override public byte[] serializeResponse(Response response) throws IOException { // Avoid BAOS for its synchronized write methods, we don't need that concurrency control UnsynchronizedBuffer out = threadLocalBuffer.get(); try { Message responseMsg = response.serialize(); // Serialization of the response may be large if (LOG.isTraceEnabled()) { LOG.trace("Serializing response '{}'", TextFormat.shortDebugString(responseMsg)); } serializeMessage(out, responseMsg); return out.toArray(); } finally { out.reset(); } }
void serializeMessage(OutputStream out, Message msg) throws IOException { // Serialize the protobuf message UnsynchronizedBuffer buffer = threadLocalBuffer.get(); ByteString serializedMsg; try { msg.writeTo(buffer); // Make a bytestring from it serializedMsg = UnsafeByteOperations.unsafeWrap(buffer.toArray()); } finally { buffer.reset(); } // Wrap the serialized message in a WireMessage WireMessage wireMsg = WireMessage.newBuilder().setNameBytes(getClassNameBytes(msg.getClass())) .setWrappedMessage(serializedMsg).build(); // Write the WireMessage to the provided OutputStream wireMsg.writeTo(out); }
void serializeMessage(OutputStream out, Message msg) throws IOException { // Serialize the protobuf message UnsynchronizedBuffer buffer = threadLocalBuffer.get(); ByteString serializedMsg; try { msg.writeTo(buffer); // Make a bytestring from it serializedMsg = UnsafeByteOperations.unsafeWrap(buffer.toArray()); } finally { buffer.reset(); } // Wrap the serialized message in a WireMessage WireMessage wireMsg = WireMessage.newBuilder().setNameBytes(getClassNameBytes(msg.getClass())) .setWrappedMessage(serializedMsg).build(); // Write the WireMessage to the provided OutputStream wireMsg.writeTo(out); }