diff --git a/Makefile b/Makefile index 9deccac..c09bcbc 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,14 @@ CXX=g++ -std=c++11 CAPNP_SOURCES=\ + src/capnp/ListPointer.java\ src/capnp/PointerReader.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++ diff --git a/src/capnp/ListPointer.java b/src/capnp/ListPointer.java new file mode 100644 index 0000000..9724442 --- /dev/null +++ b/src/capnp/ListPointer.java @@ -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; + } +} diff --git a/src/capnp/Text.java b/src/capnp/Text.java new file mode 100644 index 0000000..f07a384 --- /dev/null +++ b/src/capnp/Text.java @@ -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 + } + } + } + +} diff --git a/src/capnp/WireHelpers.java b/src/capnp/WireHelpers.java new file mode 100644 index 0000000..e4c8807 --- /dev/null +++ b/src/capnp/WireHelpers.java @@ -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); + } +} diff --git a/src/capnp/WirePointer.java b/src/capnp/WirePointer.java new file mode 100644 index 0000000..23953a5 --- /dev/null +++ b/src/capnp/WirePointer.java @@ -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)); + + } +} diff --git a/src/capnp/WordPointer.java b/src/capnp/WordPointer.java new file mode 100644 index 0000000..143139b --- /dev/null +++ b/src/capnp/WordPointer.java @@ -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; + } +}