thread safety; const indentation

This commit is contained in:
David Renshaw 2014-10-03 10:40:00 -04:00
parent 7e49ede5a3
commit b269458232
3 changed files with 30 additions and 28 deletions

View file

@ -1313,7 +1313,7 @@ private:
kj::StringTree decl; 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 proto = schema.getProto();
auto constProto = proto.getConst(); auto constProto = proto.getConst();
auto type = constProto.getType(); auto type = constProto.getType();
@ -1339,14 +1339,15 @@ private:
case schema::Value::ENUM: case schema::Value::ENUM:
return ConstText { return ConstText {
false, false,
kj::strTree("public static final ", typeName_, ' ', upperCase, " = ", kj::strTree(spaces(indent), "public static final ", typeName_, ' ', upperCase, " = ",
literalValue(constProto.getType(), constProto.getValue()), ";\n") literalValue(constProto.getType(), constProto.getValue()), ";\n")
}; };
case schema::Value::TEXT: { case schema::Value::TEXT: {
return ConstText { return ConstText {
true, 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_", " = new org.capnproto.Text.Reader(Schemas.b_",
kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(), kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(),
", ", constProto.getValue().getText().size(), ");\n") ", ", constProto.getValue().getText().size(), ");\n")
@ -1356,7 +1357,8 @@ private:
case schema::Value::DATA: { case schema::Value::DATA: {
return ConstText { return ConstText {
true, 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_", " = new org.capnproto.Data.Reader(Schemas.b_",
kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(), kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(),
", ", constProto.getValue().getData().size(), ");\n") ", ", constProto.getValue().getData().size(), ");\n")
@ -1601,10 +1603,8 @@ private:
kj::strTree(), kj::strTree(),
kj::strTree(), kj::strTree(),
kj::strTree(), kj::strTree(),
kj::strTree(), kj::strTree(),
kj::strTree(), kj::strTree(),
}; };
} }
@ -1615,10 +1615,10 @@ private:
} }
case schema::Node::CONST: { case schema::Node::CONST: {
auto constText = makeConstText(scope, name, schema.asConst()); auto constText = makeConstText(scope, name, schema.asConst(), indent);
return NodeTextNoSchema { return NodeTextNoSchema {
kj::strTree(" ", kj::mv(constText.decl)), kj::strTree(kj::mv(constText.decl)),
kj::strTree(), kj::strTree(),
kj::strTree(), kj::strTree(),

View file

@ -20,18 +20,18 @@ public final class Data {
} }
public ByteBuffer asByteBuffer() { public ByteBuffer asByteBuffer() {
// not thread safe ByteBuffer dup = this.buffer.duplicate();
this.buffer.position(this.offset); dup.position(this.offset);
ByteBuffer result = this.buffer.slice(); ByteBuffer result = dup.slice();
result.limit(this.size); result.limit(this.size);
return result; return result;
} }
public byte[] asArray() { public byte[] asArray() {
// not thread safe ByteBuffer dup = this.buffer.duplicate();
byte result[] = new byte[this.size]; byte result[] = new byte[this.size];
this.buffer.position(this.offset); dup.position(this.offset);
this.buffer.get(result, 0, this.size); dup.get(result, 0, this.size);
return result; return result;
} }
} }

View file

@ -34,8 +34,9 @@ public final class Text {
public final String toString() { public final String toString() {
byte[] bytes = new byte[this.size]; byte[] bytes = new byte[this.size];
this.buffer.position(this.offset); ByteBuffer dup = this.buffer.duplicate();
this.buffer.get(bytes, 0, this.size); dup.position(this.offset);
dup.get(bytes, 0, this.size);
try { try {
return new String(bytes, "UTF-8"); return new String(bytes, "UTF-8");
@ -61,8 +62,9 @@ public final class Text {
public final String toString() { public final String toString() {
byte[] bytes = new byte[this.size]; byte[] bytes = new byte[this.size];
this.buffer.position(this.offset); ByteBuffer dup = this.buffer.duplicate();
this.buffer.get(bytes, 0, this.size); dup.position(this.offset);
dup.get(bytes, 0, this.size);
try { try {
return new String(bytes, "UTF-8"); return new String(bytes, "UTF-8");