use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class FullBufferReadTest method testFullArrayInputStreamMarshaller.
@Test
public void testFullArrayInputStreamMarshaller() throws Exception {
SerializationContext ctx = createContext();
FileDescriptorSource fileDescriptorSource = new FileDescriptorSource().addProtoFile("file.proto", file);
ctx.registerProtoFiles(fileDescriptorSource);
class MockMarshallerFuncs implements MarshallerFuncs<X> {
public InputStream actualStream = null;
public boolean isInputStream = false;
@Override
public X read(ReadContext rc) throws IOException {
TagReader r = rc.getReader();
isInputStream = r.isInputStream();
actualStream = r.fullBufferInputStream();
return null;
}
@Override
public void write(WriteContext wc, X p) throws IOException {
TagWriter w = wc.getWriter();
w.writeInt32(1, p.f1);
w.writeInt64(2, p.f2);
}
}
MockMarshallerFuncs mockMarshallerFuncs = new MockMarshallerFuncs();
ctx.registerMarshallerProvider(new MockProtobufMarshaller<>(X.class, "test.X", mockMarshallerFuncs));
byte[] fullMsgBytes = ProtobufUtil.toWrappedByteArray(ctx, new X(1234, 4321L));
InputStream in = new ByteArrayInputStream(fullMsgBytes);
ProtobufUtil.fromWrappedStream(ctx, in);
assertNotNull(mockMarshallerFuncs.actualStream);
assertEquals(6, mockMarshallerFuncs.actualStream.available());
// assertTrue(mockMarshallerFuncs.isInputStream); // Currently always false - the InputStream appears to be converted to a byte array decoder after WrappedMessage processed
byte[] actualBytes = new byte[mockMarshallerFuncs.actualStream.available()];
mockMarshallerFuncs.actualStream.read(actualBytes);
byte[] expectedBytes = { 8, -46, 9, 16, -31, 33 };
assertNotNull(expectedBytes);
assertTrue(Arrays.equals(actualBytes, expectedBytes));
}
use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeLongs.
@Override
public void writeLongs(String fieldName, long[] 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 INT64:
for (long value : array) {
out.writeInt64(fieldNumber, value);
}
break;
case FIXED64:
for (long value : array) {
out.writeFixed64(fieldNumber, value);
}
break;
case UINT64:
for (long value : array) {
out.writeUInt64(fieldNumber, value);
}
break;
case SFIXED64:
for (long value : array) {
out.writeSFixed64(fieldNumber, value);
}
break;
case SINT64:
for (long value : array) {
out.writeSInt64(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 writeDoubles.
@Override
public void writeDoubles(String fieldName, double[] array) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
if (fd.getType() != Type.DOUBLE) {
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 (double value : array) {
out.writeDouble(fieldNumber, value);
}
}
use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeCollection.
@Override
public <E> void writeCollection(String fieldName, Collection<? super E> collection, Class<E> elementClass) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
checkRepeatedFieldWrite(fd);
if (collection == 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 GROUP:
for (Object t : collection) {
validateElement(t, elementClass);
writeGroup(fd, t, elementClass);
}
break;
case MESSAGE:
for (Object t : collection) {
validateElement(t, elementClass);
writeMessage(fd, t, elementClass);
}
break;
case ENUM:
for (Object t : collection) {
validateElement(t, elementClass);
writeEnum(fd, (Enum) t);
}
break;
case DOUBLE:
validateElementClass(elementClass, Double.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeDouble(fieldNumber, (Double) value);
}
break;
case FLOAT:
validateElementClass(elementClass, Float.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeFloat(fieldNumber, (Float) value);
}
break;
case BOOL:
validateElementClass(elementClass, Boolean.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeBool(fieldNumber, (Boolean) value);
}
break;
case STRING:
validateElementClass(elementClass, String.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeString(fieldNumber, (String) value);
}
break;
case BYTES:
validateElementClass(elementClass, byte[].class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeBytes(fieldNumber, (byte[]) value);
}
break;
case INT64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeInt64(fieldNumber, (Long) value);
}
break;
case UINT64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeUInt64(fieldNumber, (Long) value);
}
break;
case FIXED64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeFixed64(fieldNumber, (Long) value);
}
break;
case SFIXED64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeSFixed64(fieldNumber, (Long) value);
}
break;
case SINT64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeSInt64(fieldNumber, (Long) value);
}
break;
case INT32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeInt32(fieldNumber, (Integer) value);
}
break;
case FIXED32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeFixed32(fieldNumber, (Integer) value);
}
break;
case UINT32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeUInt32(fieldNumber, (Integer) value);
}
break;
case SFIXED32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeSFixed32(fieldNumber, (Integer) value);
}
break;
case SINT32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeSInt32(fieldNumber, (Integer) 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 writeBytes.
@Override
public void writeBytes(String fieldName, InputStream input) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
checkFieldWrite(fd);
if (fd.getType() != Type.BYTES) {
throw new IllegalArgumentException("Declared field type is not of type bytes : " + fd.getFullName());
}
if (input == null) {
throw new IllegalArgumentException("The input stream cannot be null");
}
int len = 0;
List<byte[]> chunks = new LinkedList<>();
int bufLen;
byte[] buffer = new byte[CHUNK_SIZE];
while ((bufLen = input.read(buffer)) != -1) {
chunks.add(buffer);
len += bufLen;
buffer = new byte[CHUNK_SIZE];
}
input.close();
TagWriter out = messageContext.out;
out.writeTag(fd.getNumber(), WireType.WIRETYPE_LENGTH_DELIMITED);
out.writeVarint32(len);
for (byte[] chunk : chunks) {
out.writeRawBytes(buffer, 0, chunk == buffer ? bufLen : CHUNK_SIZE);
}
}
Aggregations