/** * Returns the byte array computed from the var int 32 size */ public static byte[] getRawVarInt32Bytes(int value) { int size = computeRawVarint32Size(value); if (size == 1) return new byte[] { (byte) value }; int offset = 0; byte[] buffer = new byte[size]; for (int i = 0, last = size - 1; i < last; i++, value >>>= 7) buffer[offset++] = (byte) ((value & 0x7F) | 0x80); buffer[offset] = (byte) value; return buffer; }
@Override public void writeInt32(int fieldNumber, int value, boolean repeated) throws IOException { int s = ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT)); s += value < 0 ? 10 : ProtobufOutput.computeRawVarint32Size(value); size += s; }
@Override public void writeUInt32(int fieldNumber, int value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT)) + ProtobufOutput.computeRawVarint32Size(value); }
@Override public void writeByteArray(int fieldNumber, byte[] value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED)); size += ProtobufOutput.computeRawVarint32Size(value.length) + value.length; }
@Override public void writeByteRange(boolean utf8String, int fieldNumber, byte[] value, int offset, int length, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED)); size += ProtobufOutput.computeRawVarint32Size(length) + length; }
@Override public void writeString(int fieldNumber, CharSequence value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED)); final int strSize = computeUTF8Size(value, 0, value.length()); size += ProtobufOutput.computeRawVarint32Size(strSize) + strSize; }
@Override public void writeSInt32(int fieldNumber, int value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT)) + ProtobufOutput.computeRawVarint32Size( ProtobufOutput.encodeZigZag32(value)); }
@Override public void writeBool(int fieldNumber, boolean value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT)) + 1; }
@Override public void writeFixed32(int fieldNumber, int value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32)) + ProtobufOutput.LITTLE_ENDIAN_32_SIZE; }
@Override public void writeFloat(int fieldNumber, float value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32)) + ProtobufOutput.LITTLE_ENDIAN_32_SIZE; }
@Override public void writeSFixed32(int fieldNumber, int value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32)) + ProtobufOutput.LITTLE_ENDIAN_32_SIZE; }
@Override public void writeFixed64(int fieldNumber, long value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64)) + ProtobufOutput.LITTLE_ENDIAN_64_SIZE; }
@Override public void writeSFixed64(int fieldNumber, long value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64)) + ProtobufOutput.LITTLE_ENDIAN_64_SIZE; }
@Override public void writeDouble(int fieldNumber, double value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64)) + ProtobufOutput.LITTLE_ENDIAN_64_SIZE; }
<T> void writeObjectEncodedAsGroup(int fieldNumber, T value, Schema<T> schema, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP)); schema.writeTo(this, value); size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP)); }
@Override public <T> void writeObject(final int fieldNumber, final T value, final Schema<T> schema, final boolean repeated) throws IOException { if (encodeNestedMessageAsGroup) { writeObjectEncodedAsGroup(fieldNumber, value, schema, repeated); return; } size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED)); final int last = size; schema.writeTo(this, value); final int actualSize = ProtobufOutput.computeRawVarint32Size(size - last); size += actualSize; }
@Override public void writeInt64(int fieldNumber, long value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT)) + ProtobufOutput.computeRawVarint64Size(value); }
@Override public void writeUInt64(int fieldNumber, long value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT)) + ProtobufOutput.computeRawVarint64Size(value); }
@Override public void writeSInt64(int fieldNumber, long value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT)) + ProtobufOutput.computeRawVarint64Size( ProtobufOutput.encodeZigZag64(value)); }
public void testBar() throws Exception { Schema<Bar> schema = Bar.getSchema(); Bar message = SerializableObjects.bar; ByteArrayOutputStream out = new ByteArrayOutputStream(); int size = optWriteDelimitedTo(out, message, schema, buf()); int delimSize = ProtobufOutput.computeRawVarint32Size(size); byte[] data = out.toByteArray(); int expectedSize = size + delimSize; assertEquals(expectedSize, data.length); verifyOptData(data, message, schema, buf()); ByteArrayInputStream in = new ByteArrayInputStream(data); Bar parsedMessage = schema.newMessage(); boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf()); assertTrue(merged); assertEquals(message, parsedMessage); }