capnproto-java-rpc/runtime/src/main/java/org/capnproto/SegmentBuilder.java

38 lines
905 B
Java
Raw Normal View History

package org.capnproto;
import java.nio.ByteBuffer;
public final class SegmentBuilder extends SegmentReader {
public static final int FAILED_ALLOCATION = -1;
public int pos = 0; // in words
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.remaining() / 8;
}
// return how many words have already been allocated
private final int currentSize() {
return this.pos;
}
2014-05-24 14:12:44 +00:00
/**
Allocate `amount` words.
*/
public final int allocate(int amount) {
if (amount > this.capacity() - this.currentSize()) {
return FAILED_ALLOCATION; // no space left;
} else {
int result = this.pos;
this.pos += amount;
return result;
}
}
}