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

The following examples show how to use com.google.protobuf.CodedOutputStream#writeBoolNoTag() . 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: Root.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(SerializationContext context, Root root, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  RootCodecDependencies codecDeps = context.getDependency(RootCodecDependencies.class);
  if (root.equals(codecDeps.likelyPopularRoot)) {
    codedOut.writeBoolNoTag(true);
    return;
  }

  codedOut.writeBoolNoTag(false);
  if (root instanceof PathRoot) {
    codedOut.writeBoolNoTag(true);
    PathRoot pathRoot = (PathRoot) root;
    context.serialize(pathRoot.path, codedOut);
    return;
  }

  if (root instanceof AbsoluteRoot) {
    codedOut.writeBoolNoTag(false);
    AbsoluteRoot absoluteRoot = (AbsoluteRoot) root;
    context.serialize(absoluteRoot.fileSystem, codedOut);
    return;
  }

  throw new IllegalStateException("Unexpected Root: " + root);
}
 
Example 2
Source File: MultimapCodec.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, Multimap<K, V> obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  if (obj instanceof ListMultimap) {
    codedOut.writeBoolNoTag(true);
  } else if (obj instanceof SetMultimap) {
    codedOut.writeBoolNoTag(false);
  } else {
    throw new SerializationException("Unexpected multimap type: " + obj.getClass());
  }
  codedOut.writeInt32NoTag(obj.asMap().size());
  for (Map.Entry<K, Collection<V>> entry : obj.asMap().entrySet()) {
    context.serialize(entry.getKey(), codedOut);
    context.serialize(entry.getValue(), codedOut);
  }
}
 
Example 3
Source File: Aspect.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SerializationContext context, Aspect obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  context.serialize(obj.getDescriptor(), codedOut);
  boolean nativeAspect = obj.getDescriptor().getAspectClass() instanceof NativeAspectClass;
  codedOut.writeBoolNoTag(nativeAspect);
  if (!nativeAspect) {
    context.serialize(obj.getDefinition(), codedOut);
  }
}
 
Example 4
Source File: ClassCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SerializationContext context, Class<?> obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeBoolNoTag(obj.isPrimitive());
  if (obj.isPrimitive()) {
    codedOut.writeInt32NoTag(Preconditions.checkNotNull(PRIMITIVE_CLASS_INDEX_MAP.get(obj), obj));
  } else {
    context.serialize(obj.getName(), codedOut);
  }
}
 
Example 5
Source File: OptionalCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SerializationContext context, Optional<?> obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeBoolNoTag(obj.isPresent());
  if (obj.isPresent()) {
    context.serialize(obj.get(), codedOut);
  }
}
 
Example 6
Source File: Optional8Codec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SerializationContext context, Optional<?> obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeBoolNoTag(obj.isPresent());
  if (obj.isPresent()) {
    context.serialize(obj.get(), codedOut);
  }
}
 
Example 7
Source File: ImmutableMapCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, ImmutableMap<?, V> map, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeInt32NoTag(map.size());
  boolean serializeAsSortedMap = false;
  if (map instanceof ImmutableSortedMap) {
    Comparator<?> comparator = ((ImmutableSortedMap<?, ?>) map).comparator();
    // In practice the comparator seems to always be Ordering.natural(), but be flexible.
    serializeAsSortedMap =
        comparator.equals(Ordering.natural()) || comparator.equals(Comparator.naturalOrder());
  }
  codedOut.writeBoolNoTag(serializeAsSortedMap);
  serializeEntries(context, map.entrySet(), codedOut);
}
 
Example 8
Source File: ProtoBufSerialization.java    From eagle with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CodedOutputStream output = CodedOutputStream.newInstance(baos);
    output.writeBoolNoTag(obj == null);
    if (obj == null) {
        output.flush();
        return baos.toByteArray();
    }

    Class<?> clazz = obj.getClass();
    if (clazz == int.class || clazz == Integer.class) {
        output.writeSInt32NoTag((Integer) obj);
    } else if (clazz == long.class || clazz == Long.class) {
        output.writeSInt64NoTag((Long) obj);
    } else if (clazz == boolean.class || clazz == Boolean.class) {
        output.writeBoolNoTag((Boolean) obj);
    } else if (clazz == byte.class || clazz == Byte.class) {
        output.writeRawByte((Byte) obj);
    } else if (clazz == char.class || clazz == Character.class) {
        output.writeSInt32NoTag((Character) obj);
    } else if (clazz == short.class || clazz == Short.class) {
        output.writeSInt32NoTag((Short) obj);
    } else if (clazz == double.class || clazz == Double.class) {
        output.writeDoubleNoTag((Double) obj);
    } else if (clazz == float.class || clazz == Float.class) {
        output.writeFloatNoTag((Float) obj);
    } else if (clazz == String.class) {
        output.writeStringNoTag(obj.toString());
    } else if (MessageLite.class.isAssignableFrom(clazz)) {
        output.writeMessageNoTag((MessageLite) obj);
    } else if (Throwable.class.isAssignableFrom(clazz)) {
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
    } else {
        throw new IllegalArgumentException("can't serialization " + clazz);
    }

    output.flush();
    return baos.toByteArray();
}
 
Example 9
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 10
Source File: BooleanCodec.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(SerializationContext context, Boolean value, CodedOutputStream codedOut)
    throws IOException {
  codedOut.writeBoolNoTag(value);
}