SegmentBuilder.currentSize() and capacity()
This commit is contained in:
parent
d20aecb327
commit
0d99987eff
3 changed files with 33 additions and 5 deletions
|
@ -27,6 +27,7 @@ public class InputStreamMessageReader {
|
||||||
static ByteBuffer makeByteBuffer(byte[] bytes) {
|
static ByteBuffer makeByteBuffer(byte[] bytes) {
|
||||||
ByteBuffer result = ByteBuffer.wrap(bytes);
|
ByteBuffer result = ByteBuffer.wrap(bytes);
|
||||||
result.order(ByteOrder.LITTLE_ENDIAN);
|
result.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
result.mark();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,12 +67,14 @@ public class InputStreamMessageReader {
|
||||||
|
|
||||||
segmentSlices[0] = ByteBuffer.wrap(allSegments, 0, segment0Size * 8);
|
segmentSlices[0] = ByteBuffer.wrap(allSegments, 0, segment0Size * 8);
|
||||||
segmentSlices[0].order(ByteOrder.LITTLE_ENDIAN);
|
segmentSlices[0].order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
segmentSlices[0].mark();
|
||||||
|
|
||||||
int offset = segment0Size;
|
int offset = segment0Size;
|
||||||
|
|
||||||
for (int ii = 1; ii < segmentCount; ++ii) {
|
for (int ii = 1; ii < segmentCount; ++ii) {
|
||||||
segmentSlices[ii] = ByteBuffer.wrap(allSegments, offset * 8, moreSizes.get(ii - 1) * 8);
|
segmentSlices[ii] = ByteBuffer.wrap(allSegments, offset * 8, moreSizes.get(ii - 1) * 8);
|
||||||
segmentSlices[ii].order(ByteOrder.LITTLE_ENDIAN);
|
segmentSlices[ii].order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
segmentSlices[ii].mark();
|
||||||
offset += moreSizes.get(ii - 1);
|
offset += moreSizes.get(ii - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,18 +3,32 @@ package org.capnproto;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class SegmentBuilder extends SegmentReader {
|
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) {
|
public SegmentBuilder(ByteBuffer buf) {
|
||||||
super(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() {
|
private final int currentSize() {
|
||||||
throw new Error("unimplemented");
|
return this.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final int allocate(int amount) {
|
public final int allocate(int amount) {
|
||||||
throw new Error("unimplemented");
|
if (amount > this.capacity() - this.currentSize()) {
|
||||||
// if (amount > ... this.currentSize()
|
return FAILED_ALLOCATION; // no space left;
|
||||||
|
} else {
|
||||||
|
int result = this.pos;
|
||||||
|
this.pos += amount;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,18 @@ final class WireHelpers {
|
||||||
SegmentBuilder segment,
|
SegmentBuilder segment,
|
||||||
int amount,
|
int amount,
|
||||||
byte kind) {
|
byte kind) {
|
||||||
throw new Error("unimplemented");
|
|
||||||
|
// 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,
|
public static ListBuilder initListPointer(int refOffset,
|
||||||
|
|
Loading…
Reference in a new issue