diff --git a/compiler/src/main/cpp/capnpc-java.c++ b/compiler/src/main/cpp/capnpc-java.c++ index abc7724..2c93b73 100644 --- a/compiler/src/main/cpp/capnpc-java.c++ +++ b/compiler/src/main/cpp/capnpc-java.c++ @@ -1420,12 +1420,12 @@ private: } case schema::Value::DATA: { - kj::String constType = kj::strTree( - "::capnp::_::ConstData<", schema.as().size(), ">").flatten(); return ConstText { true, - kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", - kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") + kj::strTree("public static final org.capnproto.Data.Reader ", upperCase, + " = new org.capnproto.Data.Reader(Schemas.b_", + kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(), + ", ", constProto.getValue().getData().size(), ");\n") }; } diff --git a/compiler/src/test/scala/org/capnproto/EncodingTest.scala b/compiler/src/test/scala/org/capnproto/EncodingTest.scala index 9b80ae1..0687d4d 100644 --- a/compiler/src/test/scala/org/capnproto/EncodingTest.scala +++ b/compiler/src/test/scala/org/capnproto/EncodingTest.scala @@ -85,6 +85,7 @@ class EncodingSuite extends FunSuite { (-123e45) should equal (TestConstants.FLOAT64_CONST); (TestConstants.TEXT_CONST.toString()) should equal ("foo"); + (TestConstants.DATA_CONST.asArray()) should equal (TestUtil.data("bar")); (TestConstants.ENUM_CONST) should equal (TestEnum.CORGE); diff --git a/compiler/src/test/scala/org/capnproto/TestUtil.scala b/compiler/src/test/scala/org/capnproto/TestUtil.scala index 6e465b5..8d9be76 100644 --- a/compiler/src/test/scala/org/capnproto/TestUtil.scala +++ b/compiler/src/test/scala/org/capnproto/TestUtil.scala @@ -2,9 +2,16 @@ package org.capnproto; import org.capnproto.test.Test._; - - object TestUtil { + + def data(str : String) : Array[Byte] = { + try { + return str.getBytes("ISO_8859-1"); + } catch { + case e: Exception => throw new Error("could not decode"); + } + } + def initTestMessage(builder : TestAllTypes.Builder) { builder.setVoidField(org.capnproto.Void.VOID); builder.setBoolField(true); @@ -18,7 +25,7 @@ object TestUtil { builder.setUInt64Field(0x1234567890123456L); builder.setFloat32Field(1234.5f); builder.setFloat64Field(-123e45); - builder.setTextField(new Text.Reader("foo")); + builder.setTextField("foo"); // builder.setDataField(data("bar")); { diff --git a/compiler/src/test/schema/test.capnp b/compiler/src/test/schema/test.capnp index 6e865f5..72c6ec0 100644 --- a/compiler/src/test/schema/test.capnp +++ b/compiler/src/test/schema/test.capnp @@ -204,6 +204,7 @@ struct TestConstants { const float32Const :Float32 = 1234.5; const float64Const :Float64 = -123e45; const textConst :Text = "foo"; + const dataConst :Data = "bar"; # ... const enumConst :TestEnum = corge; } diff --git a/runtime/src/main/java/org/capnproto/Data.java b/runtime/src/main/java/org/capnproto/Data.java index 91ad805..4bccfc8 100644 --- a/runtime/src/main/java/org/capnproto/Data.java +++ b/runtime/src/main/java/org/capnproto/Data.java @@ -5,9 +5,46 @@ import java.nio.ByteBuffer; public final class Data { public static final class Reader { + public final ByteBuffer buffer; + public final int offset; // in bytes + public final int size; // in bytes + + public Reader(ByteBuffer buffer, int offset, int size) { + this.buffer = buffer; + this.offset = offset * 8; + this.size = size; + } + + public final int size() { + return this.size; + } + + public ByteBuffer asByteBuffer() { + // not thread safe + this.buffer.position(this.offset); + ByteBuffer result = this.buffer.slice(); + result.limit(this.size); + return result; + } + + public byte[] asArray() { + // not thread safe + byte result[] = new byte[this.size]; + this.buffer.position(this.offset); + this.buffer.get(result, 0, this.size); + return result; + } } public static final class Builder { - } + public final ByteBuffer buffer; + public final int offset; // in bytes + public final int size; // in bytes + public Builder(ByteBuffer buffer, int offset, int size) { + this.buffer = buffer; + this.offset = offset; + this.size = size; + } + } } diff --git a/runtime/src/main/java/org/capnproto/Text.java b/runtime/src/main/java/org/capnproto/Text.java index e742682..d844754 100644 --- a/runtime/src/main/java/org/capnproto/Text.java +++ b/runtime/src/main/java/org/capnproto/Text.java @@ -40,7 +40,7 @@ public final class Text { try { return new String(bytes, "UTF-8"); } catch (java.io.UnsupportedEncodingException e) { - return "unsupported encoding"; // XXX + throw new Error("UTF-8 is unsupported"); } } @@ -48,10 +48,8 @@ public final class Text { public static final class Builder { public final ByteBuffer buffer; - public final int offset; - - // size not including the NUL terminator - public final int size; + public final int offset; // in bytes + public final int size; // in bytes public Builder(ByteBuffer buffer, int offset, int size) { this.buffer = buffer; @@ -69,7 +67,7 @@ public final class Text { try { return new String(bytes, "UTF-8"); } catch (java.io.UnsupportedEncodingException e) { - return "unsupported encoding"; // XXX + throw new Error("UTF-8 is unsupported"); } }