PointerBuilder.getText()
This commit is contained in:
parent
e3a7434e45
commit
6364670998
5 changed files with 57 additions and 4 deletions
|
@ -969,7 +969,8 @@ private:
|
|||
spaces(indent), " return !_builder.getPointerField(", offset, ").isNull();\n",
|
||||
spaces(indent), " }\n",
|
||||
spaces(indent), " public final ", type, ".Builder get", titleCase, "() {\n",
|
||||
spaces(indent), " throw new Error();\n",
|
||||
spaces(indent), " return _builder.getPointerField(",
|
||||
offset, ").get", blobKind, " ();\n", // XXX
|
||||
spaces(indent), " }\n",
|
||||
spaces(indent), " public final void set", titleCase, "(", type, ".Reader value) {\n",
|
||||
unionDiscrim.set,
|
||||
|
|
|
@ -53,7 +53,7 @@ object TestUtil {
|
|||
assert(builder.getUInt64Field() == 0x1234567890123456L);
|
||||
assert(builder.getFloat32Field() == 1234.5f);
|
||||
assert(builder.getFloat64Field() == -123e45);
|
||||
//assert(builder.getTextField().toString() == "foo");
|
||||
assert(builder.getTextField().toString() == "foo");
|
||||
|
||||
{
|
||||
val subBuilder = builder.getStructField();
|
||||
|
|
|
@ -21,6 +21,15 @@ public final class PointerBuilder {
|
|||
return WireHelpers.getWritableStructPointer(this.pointer, this.segment, size);
|
||||
}
|
||||
|
||||
public final Text.Builder getText() {
|
||||
return WireHelpers.getWritableTextPointer(
|
||||
this.pointer, this.segment);
|
||||
}
|
||||
|
||||
public final Data.Builder getData() {
|
||||
throw new Error("unimplemented");
|
||||
}
|
||||
|
||||
public final StructBuilder initStruct(StructSize size) {
|
||||
return WireHelpers.initStructPointer(this.pointer, this.segment, size);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package org.capnproto;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public final class Text {
|
||||
|
||||
|
@ -17,7 +18,7 @@ public final class Text {
|
|||
|
||||
public Reader(String value) {
|
||||
try {
|
||||
byte[] bytes = value.getBytes("UTF-8");
|
||||
byte[] bytes = value.getBytes(StandardCharsets.UTF_8.name());
|
||||
this.buffer = ByteBuffer.wrap(bytes);
|
||||
this.offset = 0;
|
||||
this.size = bytes.length;
|
||||
|
@ -34,7 +35,7 @@ public final class Text {
|
|||
this.buffer.get(bytes, 0, this.size);
|
||||
|
||||
try {
|
||||
return new String(bytes, "UTF-8");
|
||||
return new String(bytes, StandardCharsets.UTF_8.name());
|
||||
} catch (java.io.UnsupportedEncodingException e) {
|
||||
return "unsupported encoding"; // XXX
|
||||
}
|
||||
|
@ -54,6 +55,21 @@ public final class Text {
|
|||
this.offset = offset;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
byte[] bytes = new byte[this.size];
|
||||
|
||||
this.buffer.position(this.offset);
|
||||
this.buffer.get(bytes, 0, this.size);
|
||||
|
||||
try {
|
||||
return new String(bytes, StandardCharsets.UTF_8.name());
|
||||
} catch (java.io.UnsupportedEncodingException e) {
|
||||
return "unsupported encoding"; // XXX
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -124,6 +124,33 @@ final class WireHelpers {
|
|||
return builder;
|
||||
}
|
||||
|
||||
public static Text.Builder getWritableTextPointer(int refOffset,
|
||||
SegmentBuilder segment) {
|
||||
long ref = WirePointer.get(segment.buffer, refOffset);
|
||||
|
||||
if (WirePointer.isNull(ref)) {
|
||||
// TODO default values
|
||||
return new Text.Builder(null, 0, 0);
|
||||
}
|
||||
|
||||
int refTarget = WirePointer.target(refOffset, ref);
|
||||
int ptr = refTarget;
|
||||
|
||||
if (WirePointer.kind(ref) != WirePointer.LIST) {
|
||||
throw new DecodeException("Called getText{Field,Element} but existing pointer is not a list.");
|
||||
}
|
||||
if (ListPointer.elementSize(WirePointer.listPointer(ref)) != FieldSize.BYTE) {
|
||||
throw new DecodeException(
|
||||
"Called getText{Field,Element} but existing list pointer is not byte-sized.");
|
||||
}
|
||||
|
||||
|
||||
//# Subtract 1 from the size for the NUL terminator.
|
||||
return new Text.Builder(segment.buffer, ptr * 8,
|
||||
ListPointer.elementCount(WirePointer.listPointer(ref)) - 1);
|
||||
|
||||
}
|
||||
|
||||
public static StructReader readStructPointer(SegmentReader segment,
|
||||
int refOffset,
|
||||
int nestingLimit) {
|
||||
|
|
Loading…
Reference in a new issue