@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 writeSInt64(int fieldNumber, long value, boolean repeated) throws IOException { size += ProtobufOutput.computeRawVarint32Size(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT)) + ProtobufOutput.computeRawVarint64Size( ProtobufOutput.encodeZigZag64(value)); }
private LinkedBuffer getProtobufBuffer() throws IOException { // Generate protobuf with packed repeated fields final LinkedBuffer buffer = new LinkedBuffer(LinkedBuffer.DEFAULT_BUFFER_SIZE); final ProtobufOutput output = new ProtobufOutput(buffer); // 03 // first element (varint 3) // 8E 02 // second element (varint 270) output.writeByteRange( false, 1, new byte[] { (byte) 0x03, (byte) 0x8E, (byte) 0x02 }, 0, 3, true); // Interleave output.writeFixed64(2, 8, true); // Non packed output.writeInt32(1, 1234, true); // Interleave output.writeByteRange( false, 2, new byte[] { 9, 0, 0, 0, 0, 0, 0, 0 }, 0, 8, true); // 9E A7 05 // third element (varint 86942) output.writeByteRange( false, 1, new byte[] { (byte) 0x9E, (byte) 0xA7, (byte) 0x05 }, 0, 3, true); return buffer; }
/** * Returns a byte array encoded with the tag and little endian 64 */ public static byte[] getTagAndRawLittleEndian64Bytes(int tag, long value) { int tagSize = computeRawVarint32Size(tag); int offset = 0; byte[] buffer = new byte[tagSize + LITTLE_ENDIAN_64_SIZE]; if (tagSize == 1) { buffer[offset++] = (byte) tag; } else { for (int i = 0, last = tagSize - 1; i < last; i++, tag >>>= 7) buffer[offset++] = (byte) ((tag & 0x7F) | 0x80); buffer[offset++] = (byte) tag; } writeRawLittleEndian64(value, buffer, offset); return buffer; }
/** * Returns a byte array encoded with the tag and little endian 32 */ public static byte[] getTagAndRawLittleEndian32Bytes(int tag, int value) { int tagSize = computeRawVarint32Size(tag); int offset = 0; byte[] buffer = new byte[tagSize + LITTLE_ENDIAN_32_SIZE]; if (tagSize == 1) { buffer[offset++] = (byte) tag; } else { for (int i = 0, last = tagSize - 1; i < last; i++, tag >>>= 7) buffer[offset++] = (byte) ((tag & 0x7F) | 0x80); buffer[offset++] = (byte) tag; } writeRawLittleEndian32(value, buffer, offset); return buffer; }
int tagSize = computeRawVarint32Size(tag); int size = computeRawVarint64Size(value); int offset = 0; byte[] buffer = new byte[tagSize + size];
@Override public void writeSInt64(int fieldNumber, long value, boolean repeated) throws IOException { writeVarInt32(makeTag(fieldNumber, WIRETYPE_VARINT)); writeVarInt64(encodeZigZag64(value)); }
@Override public void writeSInt32(int fieldNumber, int value, boolean repeated) throws IOException { writeVarInt32(makeTag(fieldNumber, WIRETYPE_VARINT)); writeVarInt32(encodeZigZag32(value)); }
/** * Serializes the {@code messages} (delimited) into an {@link OutputStream} using the given schema. * * @return the total size of the messages (excluding the length prefix varint) */ public static <T> int writeListTo(OutputStream out, List<T> messages, Schema<T> schema, LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtobufOutput output = new ProtobufOutput(buffer); int totalSize = 0; for (T m : messages) { schema.writeTo(output, m); final int size = output.getSize(); ProtobufOutput.writeRawVarInt32Bytes(out, size); final int msgSize = LinkedBuffer.writeTo(out, buffer); assert size == msgSize; totalSize += size; output.clear(); } return totalSize; }
/** * Serializes the {@code message}, prefixed with its length, into an {@link OutputStream}. * * @return the size of the message */ public static <T> int writeDelimitedTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtobufOutput output = new ProtobufOutput(buffer); schema.writeTo(output, message); final int size = output.getSize(); ProtobufOutput.writeRawVarInt32Bytes(out, size); final int msgSize = LinkedBuffer.writeTo(out, buffer); assert size == msgSize; return size; }
/** * Writes the {@code message} into the {@link LinkedBuffer} using the given schema. * * @return the size of the message */ public static <T> int writeTo(LinkedBuffer buffer, T message, Schema<T> schema) { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtobufOutput output = new ProtobufOutput(buffer); try { schema.writeTo(output, message); } catch (IOException e) { throw new RuntimeException("Serializing to a LinkedBuffer threw an IOException " + "(should never happen).", e); } return output.getSize(); }
/** * 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; }
/** * Serializes the {@code message} into a byte array using the given schema. * * @return the byte array containing the data. */ public static <T> byte[] toByteArray(T message, Schema<T> schema, LinkedBuffer buffer) { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtobufOutput output = new ProtobufOutput(buffer); try { schema.writeTo(output, message); } catch (IOException e) { throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } return output.toByteArray(); }
@Override public void writeSInt32(int fieldNumber, int value, boolean repeated) throws IOException { tail = writeTagAndRawVarInt32( makeTag(fieldNumber, WIRETYPE_VARINT), encodeZigZag32(value), this, tail); }
@Override public void writeSInt64(int fieldNumber, long value, boolean repeated) throws IOException { tail = writeTagAndRawVarInt64( makeTag(fieldNumber, WIRETYPE_VARINT), encodeZigZag64(value), this, tail); }
/** * Serializes the {@code message} into an {@link OutputStream} using the given schema. * * @return the size of the message */ public static <T> int writeTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtobufOutput output = new ProtobufOutput(buffer); schema.writeTo(output, message); return LinkedBuffer.writeTo(out, buffer); }
@Override public void writeEnum(int fieldNumber, int number, boolean repeated) throws IOException { writeInt32(fieldNumber, number, repeated); }
/** * Writes a ByteBuffer field. */ @Override public void writeBytes(int fieldNumber, ByteBuffer value, boolean repeated) throws IOException { writeByteRange(false, fieldNumber, value.array(), value.arrayOffset() + value.position(), value.remaining(), repeated); }
tail = lastBuffer = writeRawVarInt32( makeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED), this, tail); final byte[] delimited = new byte[computeRawVarint32Size(msgSize)]; writeRawVarInt32(msgSize, delimited, 0); final byte[] delimited = new byte[computeRawVarint32Size(msgSize)]; writeRawVarInt32(msgSize, delimited, 0);
public byte[] toBytes() { return output.toByteArray(); }