SegmentBuilder.currentSize() and capacity()

This commit is contained in:
David Renshaw 2014-05-14 20:51:26 -04:00
parent d20aecb327
commit 0d99987eff
3 changed files with 33 additions and 5 deletions

View file

@ -27,6 +27,7 @@ public class InputStreamMessageReader {
static ByteBuffer makeByteBuffer(byte[] bytes) {
ByteBuffer result = ByteBuffer.wrap(bytes);
result.order(ByteOrder.LITTLE_ENDIAN);
result.mark();
return result;
}
@ -66,12 +67,14 @@ public class InputStreamMessageReader {
segmentSlices[0] = ByteBuffer.wrap(allSegments, 0, segment0Size * 8);
segmentSlices[0].order(ByteOrder.LITTLE_ENDIAN);
segmentSlices[0].mark();
int offset = segment0Size;
for (int ii = 1; ii < segmentCount; ++ii) {
segmentSlices[ii] = ByteBuffer.wrap(allSegments, offset * 8, moreSizes.get(ii - 1) * 8);
segmentSlices[ii].order(ByteOrder.LITTLE_ENDIAN);
segmentSlices[ii].mark();
offset += moreSizes.get(ii - 1);
}

View file

@ -3,18 +3,32 @@ package org.capnproto;
import java.nio.ByteBuffer;
public class SegmentBuilder extends SegmentReader {
public int pos = 0;
public int pos = 0; // in words
public static final int FAILED_ALLOCATION = -1;
public SegmentBuilder(ByteBuffer buf) {
super(buf);
}
// the total number of words the buffer can hold
private final int capacity() {
this.buffer.reset();
return (this.buffer.limit() - this.buffer.position()) / 8;
}
// return how many words have already been allocated
private final int currentSize() {
throw new Error("unimplemented");
return this.pos;
}
public final int allocate(int amount) {
throw new Error("unimplemented");
// if (amount > ... this.currentSize()
if (amount > this.capacity() - this.currentSize()) {
return FAILED_ALLOCATION; // no space left;
} else {
int result = this.pos;
this.pos += amount;
return result;
}
}
}

View file

@ -6,7 +6,18 @@ final class WireHelpers {
SegmentBuilder segment,
int amount,
byte kind) {
// TODO check for nullness, amount == 0 case.
int allocation = segment.allocate(amount);
if (allocation == SegmentBuilder.FAILED_ALLOCATION) {
//# Need to allocate in a new segment. We'll need to
//# allocate an extra pointer worth of space to act as
//# the landing pad for a far pointer.
throw new Error("unimplemented");
} else {
throw new Error("unimplemented");
}
}
public static ListBuilder initListPointer(int refOffset,