Skip to content

Commit

Permalink
java: Enhance TMemoryInputTransport to allow operation on specific ra…
Browse files Browse the repository at this point in the history
…nge of a buffer rather than the whole thing. This will allow us to avoid making array copies in some instances.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@930574 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Bryan Duxbury committed Apr 3, 2010
1 parent d05a6c0 commit 9dc5757
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/java/src/org/apache/thrift/TDeserializer.java
Expand Up @@ -52,7 +52,7 @@ public TDeserializer() {
* @param protocolFactory Factory to create a protocol
*/
public TDeserializer(TProtocolFactory protocolFactory) {
trans_ = new TMemoryInputTransport(null);
trans_ = new TMemoryInputTransport();
protocol_ = protocolFactory.getProtocol(trans_);
}

Expand Down
Expand Up @@ -85,18 +85,22 @@ public int read(byte[] buf, int off, int len) throws TTransportException {
return readBuffer_.read(buf, off, len);
}

@Override
public byte[] getBuffer() {
return readBuffer_.getBuffer();
}

@Override
public int getBufferPosition() {
return readBuffer_.getBufferPosition();
}

@Override
public int getBytesRemainingInBuffer() {
return readBuffer_.getBytesRemainingInBuffer();
}

@Override
public void consumeBuffer(int len) {
readBuffer_.consumeBuffer(len);
}
Expand Down
@@ -1,17 +1,30 @@
package org.apache.thrift.transport;

public class TMemoryInputTransport extends TTransport {
public final class TMemoryInputTransport extends TTransport {

private byte[] buf_;
private int pos_;
private int endPos_;

public TMemoryInputTransport() {
}

public TMemoryInputTransport(byte[] buf) {
reset(buf);
}

public TMemoryInputTransport(byte[] buf, int offset, int length) {
reset(buf, offset, length);
}

public void reset(byte[] buf) {
reset(buf, 0, buf.length);
}

public void reset(byte[] buf, int offset, int length) {
buf_ = buf;
pos_ = 0;
pos_ = offset;
endPos_ = offset + length;
}

@Override
Expand Down Expand Up @@ -51,7 +64,7 @@ public int getBufferPosition() {
}

public int getBytesRemainingInBuffer() {
return buf_.length - pos_;
return endPos_ - pos_;
}

public void consumeBuffer(int len) {
Expand Down
Expand Up @@ -42,4 +42,26 @@ public void testReused() throws Exception {
assertEquals(new_buf, trans.getBuffer());
assertEquals(3, trans.getBytesRemainingInBuffer());
}

public void testWithOffsetAndLength() throws Exception {
byte[] input_buf = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TMemoryInputTransport trans = new TMemoryInputTransport(input_buf, 1, 3);
assertEquals(1, trans.getBufferPosition());
assertEquals(3, trans.getBytesRemainingInBuffer());
byte[] readBuffer = new byte[3];
trans.readAll(readBuffer, 0, 3);
assertTrue(Arrays.equals(new byte[]{2, 3, 4}, readBuffer));

try {
assertEquals(0, trans.readAll(readBuffer, 0, 3));
fail("should have thrown an exception");
} catch (Exception e) {
// yay
}

trans.reset(input_buf, 3, 4);
readBuffer = new byte[4];
trans.readAll(readBuffer, 0, 4);
assertTrue(Arrays.equals(new byte[]{4, 5, 6, 7}, readBuffer));
}
}

0 comments on commit 9dc5757

Please sign in to comment.