@Override public UnsynchronizedBuffer initialValue() { return new UnsynchronizedBuffer(); } };
private void reserve(int l) { if (offset + l > data.length) { int newSize = UnsynchronizedBuffer.nextArraySize(offset + l); byte[] newData = new byte[newSize]; System.arraycopy(data, 0, newData, 0, offset); data = newData; } }
@Override public void write(int b) throws IOException { reserve(1); data[offset] = (byte) b; offset++; }
/** * 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(); } }
} finally { buffer.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(); } }
requestBytes = AvaticaUtils.readFullyToBytes(inputStream, buffer); } finally { buffer.reset();
/** * 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 protected UnsynchronizedBuffer initialValue() { return new UnsynchronizedBuffer(); } };
private void reserve(int l) { if (offset + l > data.length) { int newSize = UnsynchronizedBuffer.nextArraySize(offset + l); byte[] newData = new byte[newSize]; System.arraycopy(data, 0, newData, 0, offset); data = newData; } }
} finally { buffer.reset();
@Override public void write(int b) throws IOException { reserve(1); data[offset] = (byte) b; offset++; }
@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 UnsynchronizedBuffer initialValue() { return new UnsynchronizedBuffer(); } };
@Test public void testArrayResizing() { int size = 64; int expected = 128; for (int i = 0; i < 10; i++) { // We keep being one byte short to contain this message int next = UnsynchronizedBuffer.nextArraySize(size + 1); assertEquals(expected, next); size = next; expected *= 2; } } }
requestBytes = AvaticaUtils.readFullyToBytes(inputStream, buffer); } finally { buffer.reset();
/** * Adds bytes to this writer's buffer. * * @param bytes byte array * @param off offset into array to start copying bytes * @param length number of bytes to add * @throws IndexOutOfBoundsException if off or length are invalid */ public void write(byte[] bytes, int off, int length) { reserve(length); System.arraycopy(bytes, off, data, offset, length); offset += length; }
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); }