working WirePointer static methods
This commit is contained in:
parent
edc75358bb
commit
5ee4fe295c
6 changed files with 79 additions and 13 deletions
|
@ -2,7 +2,7 @@ package org.capnproto;
|
|||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
class ListPointer {
|
||||
final class ListPointer {
|
||||
public WirePointer ptr;
|
||||
|
||||
public ListPointer(WirePointer ptr) {
|
||||
|
@ -13,11 +13,23 @@ class ListPointer {
|
|||
return (byte)(this.ptr.buffer.getInt(this.ptr.buffer_offset * 8 + 4) & 7);
|
||||
}
|
||||
|
||||
public static byte elementSize(int elementSizeAndCount) {
|
||||
return (byte) (elementSizeAndCount & 7);
|
||||
}
|
||||
|
||||
public int elementCount() {
|
||||
return this.ptr.buffer.getInt(this.ptr.buffer_offset * 8 + 4) >> 3;
|
||||
}
|
||||
|
||||
public static int elementCount(int elementSizeAndCount) {
|
||||
return elementSizeAndCount >> 3;
|
||||
}
|
||||
|
||||
public int inlineCompositeWordCount() {
|
||||
return this.elementCount();
|
||||
}
|
||||
|
||||
public static int inlineCompositeWordCount(int elementSizeAndCount) {
|
||||
return elementCount(elementSizeAndCount);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,6 @@ public class PointerReader {
|
|||
|
||||
public Text.Reader getText() {
|
||||
return WireHelpers.readTextPointer(this.segment,
|
||||
new WirePointer(this.segment.ptr,
|
||||
this.pointer));
|
||||
this.pointer);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.capnproto;
|
|||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
class StructPointer{
|
||||
final class StructPointer{
|
||||
public WirePointer ptr;
|
||||
|
||||
public StructPointer(WirePointer ptr) {
|
||||
|
@ -13,11 +13,23 @@ class StructPointer{
|
|||
return this.ptr.buffer.getShort(this.ptr.buffer_offset * 8 + 4);
|
||||
}
|
||||
|
||||
public static short dataSize(int structRef) {
|
||||
return (short)(structRef & 0xffff);
|
||||
}
|
||||
|
||||
public short ptrCount() {
|
||||
return this.ptr.buffer.getShort(this.ptr.buffer_offset * 8 + 6);
|
||||
}
|
||||
|
||||
public static short ptrCount(int structRef) {
|
||||
return (short)(structRef >> 16);
|
||||
}
|
||||
|
||||
public int wordSize() {
|
||||
return this.dataSize() + this.ptrCount();
|
||||
}
|
||||
|
||||
public static int wordSize(int structRef) {
|
||||
return (int)dataSize(structRef) + (int)ptrCount(structRef);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,9 +9,9 @@ public class Text {
|
|||
public final int offset; // in bytes
|
||||
public final int size; // in bytes
|
||||
|
||||
public Reader(WordPointer ptr, int size) {
|
||||
this.buffer = ptr.buffer;
|
||||
this.offset = ptr.offset * 8;
|
||||
public Reader(ByteBuffer buffer, int offset, int size) {
|
||||
this.buffer = buffer;
|
||||
this.offset = offset * 8;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
|
|
|
@ -72,10 +72,12 @@ class WireHelpers {
|
|||
}
|
||||
|
||||
public static Text.Reader readTextPointer(SegmentReader segment,
|
||||
WirePointer ref) {
|
||||
WordPointer ptr = ref.target();
|
||||
ListPointer listPtr = new ListPointer(ref);
|
||||
int size = listPtr.elementCount();
|
||||
return new Text.Reader(ptr, size);
|
||||
int refOffset) {
|
||||
long ref = WirePointer.get(segment.ptr, refOffset);
|
||||
int ptrOffset = WirePointer.target(refOffset, ref);
|
||||
long ptr = WirePointer.get(segment.ptr, ptrOffset);
|
||||
int listPtr = WirePointer.listPointer(ref);
|
||||
int size = ListPointer.elementCount(listPtr);
|
||||
return new Text.Reader(segment.ptr, ptrOffset, size);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.capnproto;
|
|||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
class WirePointer {
|
||||
final class WirePointer {
|
||||
public final ByteBuffer buffer;
|
||||
public final int buffer_offset; // in words
|
||||
|
||||
|
@ -25,20 +25,61 @@ class WirePointer {
|
|||
return this.buffer.getLong(this.buffer_offset * 8) == 0;
|
||||
}
|
||||
|
||||
public static boolean isNull(long wirePointer) {
|
||||
return wirePointer == 0;
|
||||
}
|
||||
|
||||
public int offsetAndKind() {
|
||||
return this.buffer.getInt(this.buffer_offset * 8);
|
||||
}
|
||||
|
||||
public static int offsetAndKind(long wirePointer) {
|
||||
return (int)(wirePointer & 0xffffffff);
|
||||
}
|
||||
|
||||
public byte kind() {
|
||||
return (byte) (this.offsetAndKind() & 3);
|
||||
}
|
||||
|
||||
public static byte kind(long wirePointer) {
|
||||
return (byte)(offsetAndKind(wirePointer) & 3);
|
||||
}
|
||||
|
||||
public WordPointer target() {
|
||||
return new WordPointer(buffer,
|
||||
this.buffer_offset + 1 + (this.offsetAndKind() >> 2));
|
||||
}
|
||||
|
||||
public static int target(int offset, long wirePointer) {
|
||||
return offset + 1 + (offsetAndKind(wirePointer) >> 2);
|
||||
}
|
||||
|
||||
public int inlineCompositeListElementCount() {
|
||||
return this.offsetAndKind() >> 2;
|
||||
}
|
||||
|
||||
public static int inlineCompositeListElementCount(long wirePointer) {
|
||||
return offsetAndKind(wirePointer) >> 2;
|
||||
}
|
||||
|
||||
// offset is in words
|
||||
public static int upper32Bits(ByteBuffer buffer, int offset) {
|
||||
return buffer.getInt(offset * 8 + 4);
|
||||
}
|
||||
|
||||
public static int upper32Bits(long wirePointer) {
|
||||
return (int)(wirePointer >> 32);
|
||||
}
|
||||
|
||||
public static int listPointer(long wirePointer) {
|
||||
return upper32Bits(wirePointer);
|
||||
}
|
||||
|
||||
public static int structPointer(long wirePointer) {
|
||||
return upper32Bits(wirePointer);
|
||||
}
|
||||
|
||||
public static long get(ByteBuffer buffer, int offset) {
|
||||
return buffer.getLong(offset * 8);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue