From b2694582326cf27eccef2b88e221f38d5e7e5827 Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Fri, 3 Oct 2014 10:40:00 -0400 Subject: [PATCH] thread safety; const indentation --- compiler/src/main/cpp/capnpc-java.c++ | 36 +++++++++---------- runtime/src/main/java/org/capnproto/Data.java | 12 +++---- runtime/src/main/java/org/capnproto/Text.java | 10 +++--- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/compiler/src/main/cpp/capnpc-java.c++ b/compiler/src/main/cpp/capnpc-java.c++ index 3990c87..7f4391d 100644 --- a/compiler/src/main/cpp/capnpc-java.c++ +++ b/compiler/src/main/cpp/capnpc-java.c++ @@ -1313,7 +1313,7 @@ private: kj::StringTree decl; }; - ConstText makeConstText(kj::StringPtr scope, kj::StringPtr name, ConstSchema schema) { + ConstText makeConstText(kj::StringPtr scope, kj::StringPtr name, ConstSchema schema, int indent) { auto proto = schema.getProto(); auto constProto = proto.getConst(); auto type = constProto.getType(); @@ -1339,14 +1339,15 @@ private: case schema::Value::ENUM: return ConstText { false, - kj::strTree("public static final ", typeName_, ' ', upperCase, " = ", - literalValue(constProto.getType(), constProto.getValue()), ";\n") + kj::strTree(spaces(indent), "public static final ", typeName_, ' ', upperCase, " = ", + literalValue(constProto.getType(), constProto.getValue()), ";\n") }; case schema::Value::TEXT: { return ConstText { true, - kj::strTree("public static final org.capnproto.Text.Reader ", upperCase, + kj::strTree(spaces(indent), + "public static final org.capnproto.Text.Reader ", upperCase, " = new org.capnproto.Text.Reader(Schemas.b_", kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(), ", ", constProto.getValue().getText().size(), ");\n") @@ -1356,7 +1357,8 @@ private: case schema::Value::DATA: { return ConstText { true, - kj::strTree("public static final org.capnproto.Data.Reader ", upperCase, + kj::strTree(spaces(indent), + "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") @@ -1591,20 +1593,18 @@ private: return NodeTextNoSchema { kj::strTree( - spaces(indent), "public enum ", name, " {\n", - KJ_MAP(e, enumerants) { - return kj::strTree(spaces(indent), " ", toUpperCase(e.getProto().getName()), ",\n"); - }, - spaces(indent), " _UNKNOWN,\n", - spaces(indent), "}\n" - "\n"), + spaces(indent), "public enum ", name, " {\n", + KJ_MAP(e, enumerants) { + return kj::strTree(spaces(indent), " ", toUpperCase(e.getProto().getName()), ",\n"); + }, + spaces(indent), " _UNKNOWN,\n", + spaces(indent), "}\n" + "\n"), kj::strTree(), kj::strTree(), - - kj::strTree(), - kj::strTree(), - + kj::strTree(), + kj::strTree(), kj::strTree(), }; } @@ -1615,10 +1615,10 @@ private: } case schema::Node::CONST: { - auto constText = makeConstText(scope, name, schema.asConst()); + auto constText = makeConstText(scope, name, schema.asConst(), indent); return NodeTextNoSchema { - kj::strTree(" ", kj::mv(constText.decl)), + kj::strTree(kj::mv(constText.decl)), kj::strTree(), kj::strTree(), diff --git a/runtime/src/main/java/org/capnproto/Data.java b/runtime/src/main/java/org/capnproto/Data.java index 4bccfc8..e18c7f7 100644 --- a/runtime/src/main/java/org/capnproto/Data.java +++ b/runtime/src/main/java/org/capnproto/Data.java @@ -20,18 +20,18 @@ public final class Data { } public ByteBuffer asByteBuffer() { - // not thread safe - this.buffer.position(this.offset); - ByteBuffer result = this.buffer.slice(); + ByteBuffer dup = this.buffer.duplicate(); + dup.position(this.offset); + ByteBuffer result = dup.slice(); result.limit(this.size); return result; } public byte[] asArray() { - // not thread safe + ByteBuffer dup = this.buffer.duplicate(); byte result[] = new byte[this.size]; - this.buffer.position(this.offset); - this.buffer.get(result, 0, this.size); + dup.position(this.offset); + dup.get(result, 0, this.size); return result; } } diff --git a/runtime/src/main/java/org/capnproto/Text.java b/runtime/src/main/java/org/capnproto/Text.java index d844754..8490715 100644 --- a/runtime/src/main/java/org/capnproto/Text.java +++ b/runtime/src/main/java/org/capnproto/Text.java @@ -34,8 +34,9 @@ public final class Text { public final String toString() { byte[] bytes = new byte[this.size]; - this.buffer.position(this.offset); - this.buffer.get(bytes, 0, this.size); + ByteBuffer dup = this.buffer.duplicate(); + dup.position(this.offset); + dup.get(bytes, 0, this.size); try { return new String(bytes, "UTF-8"); @@ -61,8 +62,9 @@ public final class Text { public final String toString() { byte[] bytes = new byte[this.size]; - this.buffer.position(this.offset); - this.buffer.get(bytes, 0, this.size); + ByteBuffer dup = this.buffer.duplicate(); + dup.position(this.offset); + dup.get(bytes, 0, this.size); try { return new String(bytes, "UTF-8");