pointers pointers pointers

This commit is contained in:
David Renshaw 2014-05-10 09:50:10 -04:00
parent 1ff499697a
commit 366fda8daf
6 changed files with 104 additions and 1 deletions

View file

@ -1,9 +1,14 @@
CXX=g++ -std=c++11 CXX=g++ -std=c++11
CAPNP_SOURCES=\ CAPNP_SOURCES=\
src/capnp/ListPointer.java\
src/capnp/PointerReader.java\ src/capnp/PointerReader.java\
src/capnp/SegmentReader.java\ src/capnp/SegmentReader.java\
src/capnp/StructReader.java src/capnp/StructReader.java\
src/capnp/Text.java\
src/capnp/WireHelpers.java\
src/capnp/WirePointer.java\
src/capnp/WordPointer.java
CAPNPC_JAVA_SOURCES=src/compiler/capnpc-java.c++ CAPNPC_JAVA_SOURCES=src/compiler/capnpc-java.c++

View file

@ -0,0 +1,14 @@
package capnp;
import java.nio.ByteBuffer;
class ListPointer extends WirePointer {
public ListPointer(ByteBuffer buffer, int buffer_offset) {
super(buffer, buffer_offset);
}
public int elementCount() {
return this.buffer.getInt(buffer_offset * 2) >> 3;
}
}

29
src/capnp/Text.java Normal file
View file

@ -0,0 +1,29 @@
package capnp;
import java.nio.ByteBuffer;
public class Text {
public static class Reader {
public final ByteBuffer buffer;
public final int offset; // in bytes
public final int size; // in bytes
public Reader(ListPointer ptr) {
this.buffer = ptr.buffer;
this.offset = ptr.buffer_offset * 8;
this.size = ptr.elementCount();
}
public String toString() {
byte[] bytes = new byte[this.size];
buffer.get(bytes, this.offset, this.size);
try {
return new String(bytes, "UTF-8");
} catch(java.io.UnsupportedEncodingException e) {
return "unsupported encoding"; // XXX
}
}
}
}

View file

@ -0,0 +1,10 @@
package capnp;
class WireHelpers {
public static Text.Reader readTextPointer(SegmentReader segment,
WirePointer ref) {
ref.target();
ListPointer listPtr = (ListPointer)ref;
return new Text.Reader(listPtr);
}
}

View file

@ -0,0 +1,32 @@
package capnp;
import java.nio.ByteBuffer;
class WirePointer {
public final ByteBuffer buffer;
public final int buffer_offset; // in words
public final byte STRUCT = 0;
public final byte LIST = 1;
public final byte FAR = 2;
public final byte OTHER = 3;
public WirePointer(ByteBuffer buffer, int offset) {
this.buffer = buffer;
this.buffer_offset = offset;
}
public int offset_and_kind() {
return this.buffer.getInt(buffer_offset * 2);
}
public byte kind() {
return (byte) (this.offset_and_kind() & 3);
}
public WordPointer target() {
return new WordPointer(buffer,
1 + (this.offset_and_kind() >> 2));
}
}

View file

@ -0,0 +1,13 @@
package capnp;
import java.nio.ByteBuffer;
class WordPointer {
public final ByteBuffer buffer;
public final int offset; // in words
public WordPointer(ByteBuffer buffer, int offset) {
this.buffer = buffer;
this.offset = offset;
}
}