Refactor SegmentStream to reduce buffering
Use ByteBuffer instead of array of byte for inlined streams
@@ -55,7 +55,7 @@ public class SegmentStream extends InputStream {
private final RecordId recordId;
- private final byte[] inline;
+ private final ByteBuffer inline;
private final ListRecord blocks;
@@ -75,9 +75,7 @@ public class SegmentStream extends InputStream {
SegmentStream(RecordId recordId, ByteBuffer inline, int length) {
this.recordId = checkNotNull(recordId);
- // TODO rewrite this class to leverage the ByteBuffer apis
- this.inline = new byte[length];
- inline.get(this.inline);
+ this.inline = inline.duplicate();
this.blocks = null;
this.length = length;
}
@@ -96,7 +94,7 @@ public class SegmentStream extends InputStream {
public String getString() {
if (inline != null) {
- return new String(inline, Charsets.UTF_8);
+ return Charsets.UTF_8.decode(inline).toString();
} else if (length > Integer.MAX_VALUE) {
throw new IllegalStateException("Too long value: " + length);
} else {
@@ -154,7 +152,8 @@ public class SegmentStream extends InputStream {
}
if (inline != null) {
- System.arraycopy(inline, (int) position, b, off, len);
+ inline.position((int) position);
+ inline.get(b, off, len);
} else {
int blockIndex = (int) (position / BLOCK_SIZE);
int blockOffset = (int) (position % BLOCK_SIZE);
