use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeInts.
@Override
public void writeInts(String fieldName, int[] array) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
checkRepeatedFieldWrite(fd);
if (array == null) {
// a repeated field can never be flagged as required
return;
}
final TagWriter out = messageContext.out;
final int fieldNumber = fd.getNumber();
switch(fd.getType()) {
case INT32:
for (int value : array) {
out.writeInt32(fieldNumber, value);
}
break;
case FIXED32:
for (int value : array) {
out.writeFixed32(fieldNumber, value);
}
break;
case UINT32:
for (int value : array) {
out.writeUInt32(fieldNumber, value);
}
break;
case SFIXED32:
for (int value : array) {
out.writeSFixed32(fieldNumber, value);
}
break;
case SINT32:
for (int value : array) {
out.writeSInt32(fieldNumber, value);
}
break;
default:
throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
}
}
use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeFloats.
@Override
public void writeFloats(String fieldName, float[] array) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
if (fd.getType() != Type.FLOAT) {
throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
}
checkRepeatedFieldWrite(fd);
if (array == null) {
// a repeated field can never be flagged as required
return;
}
final TagWriter out = messageContext.out;
final int fieldNumber = fd.getNumber();
for (float value : array) {
out.writeFloat(fieldNumber, value);
}
}
use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class UnknownFieldSetImpl method writeExternal.
@Override
public void writeExternal(ObjectOutput out) throws IOException {
ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx();
TagWriter output = TagWriterImpl.newInstanceNoBuffer(null, baos);
writeTo(output);
output.flush();
ByteBuffer buffer = baos.getByteBuffer();
int off = buffer.arrayOffset();
int len = buffer.limit() - off;
out.writeInt(len);
out.write(buffer.array(), off, len);
}
use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class JsonUtils method fromCanonicalJSON.
public static byte[] fromCanonicalJSON(ImmutableSerializationContext ctx, Reader reader) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(ProtobufUtil.DEFAULT_ARRAY_BUFFER_SIZE);
TagWriter writer = TagWriterImpl.newInstanceNoBuffer(ctx, baos);
JsonParser parser = jsonFactory.createParser(reader);
try {
while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
break;
}
switch(token) {
case START_OBJECT:
processDocument(ctx, parser, writer);
break;
case VALUE_NULL:
// we got null input, we write nothing out
break;
default:
throw new IllegalStateException("Invalid top level object! Found token: " + token);
}
}
writer.flush();
return baos.toByteArray();
} catch (JsonProcessingException e) {
throw new IllegalStateException("Invalid JSON", e);
} finally {
baos.close();
reader.close();
}
}
use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class TagWriterImplTest method doTest.
private void doTest(Factory factory) throws IOException {
log.infof("SEED is %s", SEED);
Random random = new Random(SEED);
Data data = new Data(random);
SerializationContext ctx = ProtobufUtil.newSerializationContext();
TagWriter writer = factory.newWriter(ctx);
int tag = 0;
// bool
writer.writeBool(++tag, data.b);
// ints
writer.writeInt32(++tag, data.i);
writer.writeSInt32(++tag, data.i);
writer.writeUInt32(++tag, Math.abs(data.i));
// longs
writer.writeInt64(++tag, data.l);
writer.writeSInt64(++tag, data.l);
writer.writeUInt64(++tag, Math.abs(data.l));
// double
writer.writeDouble(++tag, data.d);
// float
writer.writeFloat(++tag, data.f);
// string
writer.writeString(++tag, data.s);
// byte(s)
writer.writeBytes(++tag, data.bytes);
writer.writeBytes(++tag, data.bytes, 1, 2);
writer.flush();
TagReader reader = factory.newReader(ctx);
tag = 0;
// bool
checkFieldNumber(++tag, reader);
assertEquals(data.b, reader.readBool());
// ints
checkFieldNumber(++tag, reader);
assertEquals(data.i, reader.readInt32());
checkFieldNumber(++tag, reader);
assertEquals(data.i, reader.readSInt32());
checkFieldNumber(++tag, reader);
assertEquals(Math.abs(data.i), reader.readUInt32());
// longs
checkFieldNumber(++tag, reader);
assertEquals(data.l, reader.readInt64());
checkFieldNumber(++tag, reader);
assertEquals(data.l, reader.readSInt64());
checkFieldNumber(++tag, reader);
assertEquals(Math.abs(data.l), reader.readUInt64());
// double
checkFieldNumber(++tag, reader);
assertEquals(data.d, reader.readDouble(), 1);
// float
checkFieldNumber(++tag, reader);
assertEquals(data.f, reader.readFloat(), 1);
// string
checkFieldNumber(++tag, reader);
assertEquals(data.s, reader.readString());
// byte(s)
checkFieldNumber(++tag, reader);
assertArrayEquals(data.bytes, reader.readByteArray());
checkFieldNumber(++tag, reader);
assertArrayEquals(new byte[] { data.bytes[1], data.bytes[2] }, reader.readByteArray());
}
Aggregations