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

The following examples show how to use com.google.protobuf.CodedOutputStream#writeEnumNoTag() . 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 void writeTo(CodedOutputStream stream) throws IOException {
  // We use the NoTag write methods for consistency with the parsing functions and for
  // consistency with the variable-length writes where we can't use any convenience function.
  stream.writeUInt32NoTag(TYPE_TAG);
  stream.writeEnumNoTag(type.getNumber());

  stream.writeUInt32NoTag(NUM_VALUES_TAG);
  stream.writeInt64NoTag(numValues);

  if (encodingVersion != DEFAULT_ENCODING_VERSION) {
    stream.writeUInt32NoTag(ENCODING_VERSION_TAG);
    stream.writeInt32NoTag(encodingVersion);
  }

  if (!valueType.equals(DEFAULT_VALUE_TYPE)) {
    stream.writeUInt32NoTag(VALUE_TYPE_TAG);
    stream.writeEnumNoTag(valueType.getNumber());
  }

  stream.writeUInt32NoTag(HYPERLOGLOGPLUS_UNIQUE_STATE_TAG);
  stream.writeUInt32NoTag(getSerializedHllSize());
  writeHllTo(stream);
}
 
Example 2
Source File: NestedSetCodecWithStore.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(SerializationContext context, NestedSet<?> obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  context.serialize(obj.getOrder(), codedOut);
  if (obj.isEmpty()) {
    // If the NestedSet is empty, it needs to be assigned to the EMPTY_CHILDREN constant on
    // deserialization.
    codedOut.writeEnumNoTag(NestedSetSize.EMPTY.ordinal());
  } else if (obj.isSingleton()) {
    // If the NestedSet is a singleton, we serialize directly as an optimization.
    codedOut.writeEnumNoTag(NestedSetSize.LEAF.ordinal());
    context.serialize(obj.getChildren(), codedOut);
  } else {
    codedOut.writeEnumNoTag(NestedSetSize.NONLEAF.ordinal());
    context.serialize(obj.getApproxDepth(), codedOut);
    FingerprintComputationResult fingerprintComputationResult =
        nestedSetStore.computeFingerprintAndStore((Object[]) obj.getChildren(), context);
    context.addFutureToBlockWritingOn(fingerprintComputationResult.writeStatus());
    codedOut.writeByteArrayNoTag(fingerprintComputationResult.fingerprint().toByteArray());
  }
  interner.put(new EqualsWrapper(obj), obj);
}
 
Example 3
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * Write a field of arbitrary type, without its tag, to the stream.
 *
 * @param output The output stream.
 * @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.
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void writeElementNoTag(final CodedOutputStream output, final WireFormat.FieldType type,
        final Object value) throws IOException {
    switch (type) {
        case DOUBLE:
            output.writeDoubleNoTag((Double) value);
            break;
        case FLOAT:
            output.writeFloatNoTag((Float) value);
            break;
        case INT64:
            output.writeInt64NoTag((Long) value);
            break;
        case UINT64:
            output.writeUInt64NoTag((Long) value);
            break;
        case INT32:
            output.writeInt32NoTag((Integer) value);
            break;
        case FIXED64:
            output.writeFixed64NoTag((Long) value);
            break;
        case FIXED32:
            output.writeFixed32NoTag((Integer) value);
            break;
        case BOOL:
            output.writeBoolNoTag((Boolean) value);
            break;
        case STRING:
            output.writeStringNoTag((String) value);
            break;
        // group not support yet
        // case GROUP : output.writeGroupNoTag ((MessageLite) value); break;
        case MESSAGE:
            writeObject(output, 0, FieldType.OBJECT, value, false, false);
            break;
        case BYTES:
            if (value instanceof ByteString) {
                output.writeBytesNoTag((ByteString) value);
            } else {
                byte[] v;
                if (value instanceof Byte[]) {
                    v = toByteArray((Byte[]) value);
                } else {
                    v = (byte[]) value;
                }
                output.writeByteArrayNoTag(v);
            }
            break;
        case UINT32:
            output.writeUInt32NoTag((Integer) value);
            break;
        case SFIXED32:
            output.writeSFixed32NoTag((Integer) value);
            break;
        case SFIXED64:
            output.writeSFixed64NoTag((Long) value);
            break;
        case SINT32:
            output.writeSInt32NoTag((Integer) value);
            break;
        case SINT64:
            output.writeSInt64NoTag((Long) value);
            break;

        case ENUM:
            if (value instanceof Internal.EnumLite) {
                output.writeEnumNoTag(((Internal.EnumLite) value).getNumber());
            } else {

                if (value instanceof EnumReadable) {
                    output.writeEnumNoTag(((EnumReadable) value).value());
                } else if (value instanceof Enum) {
                    output.writeEnumNoTag(((Enum) value).ordinal());
                } else {
                    output.writeEnumNoTag(((Integer) value).intValue());
                }

            }
            break;
    }
}
 
Example 4
Source File: EnumCodec.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(SerializationContext context, T value, CodedOutputStream codedOut)
    throws IOException {
  Preconditions.checkNotNull(value, "Enum value for %s is null", enumClass);
  codedOut.writeEnumNoTag(value.ordinal());
}