1 package org.simantics.databoard.streaming;
3 import java.io.DataOutput;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStream;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.databoard.serialization.Serializer;
10 import org.simantics.databoard.type.Datatype;
11 import org.simantics.databoard.util.binary.Endian;
12 import org.simantics.databoard.util.binary.UTF8;
15 * This is an utility class that encapsulates the databoard value encoding scheme.
16 * It can be used for streaming value writing.
18 public class DataWriter {
19 private static final Serializer DATATYPE_SERIALIZER = Bindings.getSerializerUnchecked(Bindings.getBindingUnchecked( Datatype.class ));
21 private final DataOutput out;
23 public DataWriter(DataOutput out) {
27 public DataWriter(OutputStream stream) {
28 this.out = new DataOutputStream(stream);
31 public void writeBoolean(boolean value) throws IOException {
32 out.write(value ? 1 : 0);
35 public void writeByte(byte value) throws IOException {
39 public void writeInteger(int value) throws IOException {
43 public void writeLong(long value) throws IOException {
47 public void writeFloat(float value) throws IOException {
48 out.writeFloat(value);
51 public void writeDouble(double value) throws IOException {
52 out.writeDouble(value);
55 public void writeString(String value) throws IOException {
56 int utflen = UTF8.getModifiedUTF8EncodingByteLength(value);
57 Endian.writeDynamicUInt32(out, utflen);
58 UTF8.writeModifiedUTF(out, value);
61 public void writeDatatype(Datatype datatype) throws IOException {
62 DATATYPE_SERIALIZER.serialize(out, datatype);
66 * A variable length array is started with the length
67 * followed by the serialization of all elements.
69 public void beginVariableLengthArray(int length) throws IOException {
74 * A map is started with its size followed by
75 * serialization of interleaved keys and values.
77 public void beginMap(int size) throws IOException {
82 * An optional value that is null is written as byte 0.
84 public void writeOptionalNull() throws IOException {
89 * An optional value that is not null is started
90 * with byte 1 followed by the actual value.
92 public void beginOptionalValue() throws IOException {
97 * Selects the constructor of the union type.
98 * It is written as a variable length integer,
99 * so the total number of tags is required.
101 public void writeUnionTag(int tag, int tagCount) throws IOException {
102 Endian.putUInt(out, tag, tagCount-1);
105 public void writeReferenceToKnownReferableRecord(int id) throws IOException {
109 public void beginUnknownReferableRecord(int id) throws IOException {