Java Code Examples for com.google.protobuf.Message#getSerializedSize()

The following examples show how to use com.google.protobuf.Message#getSerializedSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtoBufFile.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Save a protobuf message to file.
 *
 * @param msg  protobuf message
 * @param sync if sync flush data to disk
 * @return true if save success
 */
public boolean save(final Message msg, final boolean sync) throws IOException {
    // Write message into temp file
    final File file = new File(this.path + ".tmp");
    try (final FileOutputStream fOut = new FileOutputStream(file);
            final BufferedOutputStream output = new BufferedOutputStream(fOut)) {
        final byte[] lenBytes = new byte[4];

        // name len + name
        final String fullName = msg.getDescriptorForType().getFullName();
        final int nameLen = fullName.length();
        Bits.putInt(lenBytes, 0, nameLen);
        output.write(lenBytes);
        output.write(fullName.getBytes());
        // msg len + msg
        final int msgLen = msg.getSerializedSize();
        Bits.putInt(lenBytes, 0, msgLen);
        output.write(lenBytes);
        msg.writeTo(output);
        if (sync) {
            fOut.getFD().sync();
        }
    }

    return Utils.atomicMoveFile(file, new File(this.path));
}
 
Example 2
Source File: AsynchronousFileOutputStream.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Writes a delimited protocol buffer message in the same format as {@link
 * MessageLite#writeDelimitedTo(java.io.OutputStream)}.
 *
 * <p>Unfortunately, {@link MessageLite#writeDelimitedTo(java.io.OutputStream)} may result in
 * multiple calls to write on the underlying stream, so we have to provide this method here
 * instead of the caller using it directly.
 */
@Override
public void write(Message m) {
  Preconditions.checkNotNull(m);
  final int size = m.getSerializedSize();
  ByteArrayOutputStream bos =
      new ByteArrayOutputStream(CodedOutputStream.computeUInt32SizeNoTag(size) + size);
  try {
    m.writeDelimitedTo(bos);
  } catch (IOException e) {
    // This should never happen with an in-memory stream.
    exception.compareAndSet(null, new IllegalStateException(e.toString()));
    return;
  }
  write(bos.toByteArray());
}
 
Example 3
Source File: Trezor.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void messageWrite(final Message msg) {
    final int msg_size = msg.getSerializedSize();
    final String msg_name = msg.getClass().getSimpleName();
    final int msg_id = MessageType.valueOf("MessageType_" + msg_name).getNumber();
    Log.d(TAG, String.format("Got message: %s (%d bytes)", msg_name, msg_size));
    final ByteBuffer data = ByteBuffer.allocate(32768);
    data.put((byte)'#');
    data.put((byte)'#');
    data.put((byte)((msg_id >> 8) & 0xFF));
    data.put((byte)(msg_id & 0xFF));
    data.put((byte)((msg_size >> 24) & 0xFF));
    data.put((byte)((msg_size >> 16) & 0xFF));
    data.put((byte)((msg_size >> 8) & 0xFF));
    data.put((byte)(msg_size & 0xFF));
    data.put(msg.toByteArray());
    while (data.position() % 63 > 0)
        data.put((byte)0);
    final UsbRequest request = new UsbRequest();
    request.initialize(mConn, mWriteEndpoint);
    final int chunks = data.position() / 63;
    Log.d(TAG, String.format("Writing %d chunks", chunks));
    data.rewind();
    for (int i = 0; i < chunks; ++i) {
        final byte[] buffer = new byte[64];
        buffer[0] = (byte)'?';
        data.get(buffer, 1, 63);
        //logData("chunk:", buffer);
        request.queue(ByteBuffer.wrap(buffer), 64);
        mConn.requestWait();
    }
    request.close();
}
 
Example 4
Source File: Trezor.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void messageWrite(final Message msg) {
    final int msg_size = msg.getSerializedSize();
    final String msg_name = msg.getClass().getSimpleName();
    final int msg_id = MessageType.valueOf("MessageType_" + msg_name).getNumber();
    Log.i(TAG, String.format("Got message: %s (%d bytes)", msg_name, msg_size));
    final ByteBuffer data = ByteBuffer.allocate(32768);
    data.put((byte)'#');
    data.put((byte)'#');
    data.put((byte)((msg_id >> 8) & 0xFF));
    data.put((byte)(msg_id & 0xFF));
    data.put((byte)((msg_size >> 24) & 0xFF));
    data.put((byte)((msg_size >> 16) & 0xFF));
    data.put((byte)((msg_size >> 8) & 0xFF));
    data.put((byte)(msg_size & 0xFF));
    data.put(msg.toByteArray());
    while (data.position() % 63 > 0)
        data.put((byte)0);
    final UsbRequest request = new UsbRequest();
    request.initialize(mConn, mWriteEndpoint);
    final int chunks = data.position() / 63;
    Log.i(TAG, String.format("Writing %d chunks", chunks));
    data.rewind();
    for (int i = 0; i < chunks; ++i) {
        final byte[] buffer = new byte[64];
        buffer[0] = (byte)'?';
        data.get(buffer, 1, 63);
        logData("chunk:", buffer);
        request.queue(ByteBuffer.wrap(buffer), 64);
        mConn.requestWait();
    }
    request.close();
}
 
Example 5
Source File: AbstractErrorUtils.java    From google-ads-java with Apache License 2.0 4 votes vote down vote up
/** Checks if a result in a mutate response is a partial failure. */
public boolean isPartialFailureResult(Message message) {
  return message.getSerializedSize() == 0;
}
 
Example 6
Source File: OutgoingPacket.java    From incubator-heron with Apache License 2.0 4 votes vote down vote up
public static int sizeRequiredToPackMessage(Message msg) {
  return 4 + msg.getSerializedSize();
}