Java Code Examples for com.google.protobuf.CodedOutputStream#computeUInt32SizeNoTag()

The following examples show how to use com.google.protobuf.CodedOutputStream#computeUInt32SizeNoTag() . 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: State.java    From zetasketch with Apache License 2.0 6 votes vote down vote up
public int getSerializedSize() {
  int size = 0;

  size += CodedOutputStream.computeUInt32SizeNoTag(TYPE_TAG);
  size += CodedOutputStream.computeEnumSizeNoTag(type.getNumber());

  size += CodedOutputStream.computeUInt32SizeNoTag(NUM_VALUES_TAG);
  size += CodedOutputStream.computeInt64SizeNoTag(numValues);

  if (encodingVersion != DEFAULT_ENCODING_VERSION) {
    size += CodedOutputStream.computeUInt32SizeNoTag(ENCODING_VERSION_TAG);
    size += CodedOutputStream.computeInt32SizeNoTag(encodingVersion);
  }

  if (!valueType.equals(DEFAULT_VALUE_TYPE)) {
    size += CodedOutputStream.computeUInt32SizeNoTag(VALUE_TYPE_TAG);
    size += CodedOutputStream.computeEnumSizeNoTag(valueType.getNumber());
  }

  int hllSize = getSerializedHllSize();
  size += CodedOutputStream.computeUInt32SizeNoTag(HYPERLOGLOGPLUS_UNIQUE_STATE_TAG);
  size += CodedOutputStream.computeUInt32SizeNoTag(hllSize);
  size += hllSize;

  return size;
}
 
Example 2
Source File: StackdriverSender.java    From zipkin-gcp with Apache License 2.0 6 votes vote down vote up
StackdriverSender(Builder builder) {
  channel = builder.channel;
  callOptions = builder.callOptions;
  projectName = ByteString.copyFromUtf8("projects/" + builder.projectId);
  serverResponseTimeoutMs = builder.serverResponseTimeoutMs;
  traceIdPrefix = projectName.concat(ByteString.copyFromUtf8("/traces/"));
  shutdownChannelOnClose = builder.shutdownChannelOnClose;
  projectNameFieldSize = CodedOutputStream.computeBytesSize(1, projectName);

  // The size of the contents of the Span.name field, used to preallocate the correct sized
  // buffer when computing Span.name.
  spanNameSize = traceIdPrefix.size() + 32 + SPAN_ID_PREFIX.size() + 16;

  spanNameFieldSize = CodedOutputStream.computeTagSize(1)
      + CodedOutputStream.computeUInt32SizeNoTag(spanNameSize) + spanNameSize;

  BatchWriteSpansRequest healthcheckRequest = BatchWriteSpansRequest.newBuilder()
      .setNameBytes(projectName)
      .addSpans(Span.newBuilder().build())
      .build();
  healthcheckCall = new BatchWriteSpansCall(healthcheckRequest);
}
 
Example 3
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 4
Source File: IdXmlResourceValue.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output)
    throws IOException {
  DataValueXml.Builder xmlValue =
      SerializeFormat.DataValueXml.newBuilder()
          .setType(XmlType.ID)
          .putAllNamespace(namespaces.asMap());
  if (value != null) {
    xmlValue.setValue(value);
  }
  SerializeFormat.DataValue dataValue =
      XmlResourceValues.newSerializableDataValueBuilder(sourceId).setXmlValue(xmlValue).build();
  dataValue.writeDelimitedTo(output);
  return CodedOutputStream.computeUInt32SizeNoTag(dataValue.getSerializedSize())
      + dataValue.getSerializedSize();
}
 
Example 5
Source File: PluralXmlResourceValue.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output)
    throws IOException {
  SerializeFormat.DataValue.Builder builder =
      XmlResourceValues.newSerializableDataValueBuilder(sourceId);
  SerializeFormat.DataValue value =
      builder
          .setXmlValue(
              builder
                  .getXmlValueBuilder()
                  .setType(XmlType.PLURAL)
                  .putAllNamespace(namespaces.asMap())
                  .putAllAttribute(attributes)
                  .putAllMappedStringValue(values))
          .build();
  value.writeDelimitedTo(output);
  return CodedOutputStream.computeUInt32SizeNoTag(value.getSerializedSize())
      + value.getSerializedSize();
}
 
Example 6
Source File: State.java    From zetasketch with Apache License 2.0 5 votes vote down vote up
private int getSerializedHllSize() {
  int size = 0;

  if (sparseSize != DEFAULT_SPARSE_SIZE) {
    size += CodedOutputStream.computeUInt32SizeNoTag(SPARSE_SIZE_TAG);
    size += CodedOutputStream.computeInt32SizeNoTag(sparseSize);
  }

  if (precision != DEFAULT_PRECISION_OR_NUM_BUCKETS) {
    size += CodedOutputStream.computeUInt32SizeNoTag(PRECISION_OR_NUM_BUCKETS_TAG);
    size += CodedOutputStream.computeInt32SizeNoTag(precision);
  }

  if (sparsePrecision != DEFAULT_SPARSE_PRECISION_OR_NUM_BUCKETS) {
    size += CodedOutputStream.computeUInt32SizeNoTag(SPARSE_PRECISION_OR_NUM_BUCKETS_TAG);
    size += CodedOutputStream.computeInt32SizeNoTag(sparsePrecision);
  }

  if (data != null) {
    int dataLength = data.remaining();
    size += CodedOutputStream.computeUInt32SizeNoTag(DATA_TAG);
    size += CodedOutputStream.computeUInt32SizeNoTag(dataLength);
    size += dataLength;
  }

  if (sparseData != null) {
    int sparseDataLength = sparseData.remaining();
    size += CodedOutputStream.computeUInt32SizeNoTag(SPARSE_DATA_TAG);
    size += CodedOutputStream.computeUInt32SizeNoTag(sparseDataLength);
    size += sparseDataLength;
  }

  return size;
}
 
Example 7
Source File: BinaryFormatFileTransport.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
protected byte[] serializeEvent(BuildEventStreamProtos.BuildEvent buildEvent) {
  final int size = buildEvent.getSerializedSize();
  ByteArrayOutputStream bos =
      new ByteArrayOutputStream(CodedOutputStream.computeUInt32SizeNoTag(size) + size);
  try {
    buildEvent.writeDelimitedTo(bos);
  } catch (IOException e) {
    throw new RuntimeException(
        "Unexpected error serializing protobuf to in memory outputstream.", e);
  }
  return bos.toByteArray();
}
 
Example 8
Source File: XmlResourceValues.java    From bazel with Apache License 2.0 5 votes vote down vote up
public static int serializeProtoDataValue(
    OutputStream output, SerializeFormat.DataValue.Builder builder) throws IOException {
  SerializeFormat.DataValue value = builder.build();
  value.writeDelimitedTo(output);
  return CodedOutputStream.computeUInt32SizeNoTag(value.getSerializedSize())
      + value.getSerializedSize();
}
 
Example 9
Source File: DataValueFile.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public int serializeTo(DataSourceTable sourceTable, OutputStream output)
    throws IOException {
  SerializeFormat.DataValue.Builder builder = SerializeFormat.DataValue.newBuilder();
  SerializeFormat.DataValue value = builder.setSourceId(sourceTable.getSourceId(source)).build();
  value.writeDelimitedTo(output);
  return CodedOutputStream.computeUInt32SizeNoTag(value.getSerializedSize())
      + value.getSerializedSize();
}
 
Example 10
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * Compute the number of bytes that would be needed to encode a particular value of arbitrary type, excluding tag.
 *
 * @param type The field's type.
 * @param value Object representing the field's value. Must be of the exact type which would be returned by
 *            {@link Message#getField(Descriptors.FieldDescriptor)} for this field.
 * @return the int
 */
public static int computeElementSizeNoTag(final WireFormat.FieldType type, final Object value) {
    switch (type) {
        // Note: Minor violation of 80-char limit rule here because this would
        // actually be harder to read if we wrapped the lines.
        case DOUBLE:
            return CodedOutputStream.computeDoubleSizeNoTag((Double) value);
        case FLOAT:
            return CodedOutputStream.computeFloatSizeNoTag((Float) value);
        case INT64:
            return CodedOutputStream.computeInt64SizeNoTag((Long) value);
        case UINT64:
            return CodedOutputStream.computeUInt64SizeNoTag((Long) value);
        case INT32:
            return CodedOutputStream.computeInt32SizeNoTag((Integer) value);
        case FIXED64:
            return CodedOutputStream.computeFixed64SizeNoTag((Long) value);
        case FIXED32:
            return CodedOutputStream.computeFixed32SizeNoTag((Integer) value);
        case BOOL:
            return CodedOutputStream.computeBoolSizeNoTag((Boolean) value);
        case STRING:
            return CodedOutputStream.computeStringSizeNoTag((String) value);
        case GROUP:
            return CodedOutputStream.computeGroupSizeNoTag((MessageLite) value);
        case BYTES:
            if (value instanceof ByteString) {
                return CodedOutputStream.computeBytesSizeNoTag((ByteString) value);
            } else {
                if (value instanceof Byte[]) {
                    return computeLengthDelimitedFieldSize(((Byte[]) value).length);
                }
                return CodedOutputStream.computeByteArraySizeNoTag((byte[]) value);
            }
        case UINT32:
            return CodedOutputStream.computeUInt32SizeNoTag((Integer) value);
        case SFIXED32:
            return CodedOutputStream.computeSFixed32SizeNoTag((Integer) value);
        case SFIXED64:
            return CodedOutputStream.computeSFixed64SizeNoTag((Long) value);
        case SINT32:
            return CodedOutputStream.computeSInt32SizeNoTag((Integer) value);
        case SINT64:
            return CodedOutputStream.computeSInt64SizeNoTag((Long) value);

        case MESSAGE:
            if (value instanceof LazyField) {
                return CodedOutputStream.computeLazyFieldSizeNoTag((LazyField) value);
            } else {
                return computeObjectSizeNoTag(value);
            }

        case ENUM:
            if (value instanceof Internal.EnumLite) {
                return CodedOutputStream.computeEnumSizeNoTag(((Internal.EnumLite) value).getNumber());
            } else {
                if (value instanceof EnumReadable) {
                    return CodedOutputStream.computeEnumSizeNoTag(((EnumReadable) value).value());
                } else if (value instanceof Enum) {
                    return CodedOutputStream.computeEnumSizeNoTag(((Enum) value).ordinal());
                }

                return CodedOutputStream.computeEnumSizeNoTag((Integer) value);
            }
    }

    throw new RuntimeException("There is no way to get here, but the compiler thinks otherwise.");
}
 
Example 11
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 2 votes vote down vote up
/**
 * Compute length delimited field size.
 *
 * @param fieldLength the field length
 * @return the int
 */
public static int computeLengthDelimitedFieldSize(int fieldLength) {
    return CodedOutputStream.computeUInt32SizeNoTag(fieldLength) + fieldLength;
}