use of org.infinispan.protostream.descriptors.FieldDescriptor 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);
}
}
use of org.infinispan.protostream.descriptors.FieldDescriptor 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.descriptors.FieldDescriptor in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeEnum.
@Override
public <E extends Enum<E>> void writeEnum(String fieldName, E value) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
if (fd.getType() != Type.ENUM) {
throw new IllegalArgumentException("Declared field type is not an enum : " + fd.getFullName());
}
checkFieldWrite(fd);
if (value == null) {
if (fd.isRequired()) {
throw new IllegalArgumentException("A required field cannot be null : " + fd.getFullName());
}
return;
}
writeEnum(fd, value);
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeString.
@Override
public void writeString(String fieldName, String value) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
checkFieldWrite(fd);
if (fd.getType() != Type.STRING) {
throw new IllegalArgumentException("Declared field type is not of type string : " + fd.getFullName());
}
if (value == null) {
if (fd.isRequired()) {
throw new IllegalArgumentException("A required field cannot be null : " + fd.getFullName());
}
return;
}
messageContext.out.writeString(fd.getNumber(), value);
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeDouble.
@Override
public void writeDouble(String fieldName, double value) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
checkFieldWrite(fd);
if (fd.getType() != Type.DOUBLE) {
throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
}
messageContext.out.writeDouble(fd.getNumber(), value);
}
Aggregations