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

The following examples show how to use com.google.protobuf.CodedOutputStream#writeInt32NoTag() . 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: EnumMapCodec.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(SerializationContext context, EnumMap<E, V> obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  if (!obj.getClass().equals(EnumMap.class)) {
    throw new SerializationException(
        "Cannot serialize subclasses of EnumMap: " + obj.getClass() + " (" + obj + ")");
  }
  codedOut.writeInt32NoTag(obj.size());
  if (obj.isEmpty()) {
    // Do gross hack to get key type of map, since we have no concrete element to examine.
    Unsafe unsafe = UnsafeProvider.getInstance();
    context.serialize(unsafe.getObject(obj, classTypeOffset), codedOut);
    return;
  }
  context.serialize(obj.keySet().iterator().next().getDeclaringClass(), codedOut);
  for (Map.Entry<E, V> entry : obj.entrySet()) {
    codedOut.writeInt32NoTag(entry.getKey().ordinal());
    context.serialize(entry.getValue(), codedOut);
  }
}
 
Example 2
Source File: UnsafeJdk9StringCodec.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(SerializationContext context, String obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  byte coder = stringUnsafe.getCoder(obj);
  byte[] value = stringUnsafe.getByteArray(obj);
  // Optimize for the case that coder == 0, in which case we can just write the length here,
  // potentially using just one byte. If coder != 0, we'll use 4 bytes, but that's vanishingly
  // rare.
  if (coder == 0) {
    codedOut.writeInt32NoTag(value.length);
  } else if (coder == 1) {
    codedOut.writeInt32NoTag(-value.length);
  } else {
    throw new SerializationException("Unexpected coder value: " + coder + " for " + obj);
  }
  codedOut.writeRawBytes(value);
}
 
Example 3
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 4
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 5
Source File: StringLiteral.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(SerializationContext context, StringLiteral lit, CodedOutputStream out)
    throws SerializationException, IOException {
  // Enable de-duplication of strings during encoding.
  // The encoder does not intern and de-duplicate Strings by default,
  // though it does for all other objects;
  // see skyframe.serialization.strings.StringCodec.getStrategy.
  // If that were to change, we could delete StringLiteralCodec.
  // (One wonders why Identifier.name strings are not similarly de-duped,
  // as they are as numerous and more repetitive than string literals.)
  context.serializeWithAdHocMemoizationStrategy(
      lit.getValue(), MemoizationStrategy.MEMOIZE_AFTER, out);
  out.writeInt32NoTag(lit.startOffset);
  out.writeInt32NoTag(lit.endOffset);
  context.serialize(lit.locs, out);
}
 
Example 6
Source File: EOSWalletTest.java    From token-core-android with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeToBinary() {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream);
  try {

    codedOutputStream.writeStringNoTag("jc");
    codedOutputStream.writeStringNoTag("dan");
    codedOutputStream.writeInt32NoTag(1);
    codedOutputStream.writeStringNoTag("abc");
    codedOutputStream.writeStringNoTag("");
    codedOutputStream.writeByteArrayNoTag(NumericUtil.hexToBytes("0f0f0f"));
    codedOutputStream.flush();
    ByteBuffer byteBuffer = ByteBuffer.allocate(100);

  } catch (IOException e) {
    e.printStackTrace();
  }
}
 
Example 7
Source File: ImmutableListCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, ImmutableList<T> list, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeInt32NoTag(list.size());
  for (T item : list) {
    codec.serialize(context, item, codedOut);
  }
}
 
Example 8
Source File: HashSetCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SerializationContext context, HashSet<E> obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeInt32NoTag(obj.size());
  for (Object object : obj) {
    context.serialize(object, codedOut);
  }
}
 
Example 9
Source File: ImmutableBiMapCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, ImmutableBiMap<K, V> map, CodedOutputStream codedOut)
    throws SerializationException, IOException {

  codedOut.writeInt32NoTag(map.size());
  ImmutableMapCodec.serializeEntries(context, map.entrySet(), codedOut);
}
 
Example 10
Source File: FluentIterableCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, FluentIterable object, CodedOutputStream codedOut)
    throws IOException, SerializationException {
  codedOut.writeInt32NoTag(object.size());
  for (Object obj : object) {
    context.serialize(obj, codedOut);
  }
}
 
Example 11
Source File: ImmutableSortedSetCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, ImmutableSortedSet<E> object, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  context.serialize(object.comparator(), codedOut);
  codedOut.writeInt32NoTag(object.size());
  for (Object obj : object) {
    context.serialize(obj, codedOut);
  }
}
 
Example 12
Source File: SerializationContextTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, ArrayList<?> obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeInt32NoTag(obj.size());
  for (Object item : obj) {
    context.serialize(item, codedOut);
  }
}
 
Example 13
Source File: NullableListCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SerializationContext context, List<T> list, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeInt32NoTag(list.size());
  for (T item : list) {
    context.serialize(item, codedOut);
  }
}
 
Example 14
Source File: LongArrayCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SerializationContext context, long[] obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeInt32NoTag(obj.length);
  for (long l : obj) {
    codedOut.writeInt64NoTag(l);
  }
}
 
Example 15
Source File: ImmutableListRuntimeCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, ImmutableList object, CodedOutputStream codedOut)
    throws IOException, SerializationException {
  codedOut.writeInt32NoTag(object.size());
  for (Object obj : object) {
    context.serialize(obj, codedOut);
  }
}
 
Example 16
Source File: ImmutableTableCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, ImmutableTable<R, C, V> object, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  Set<Cell<R, C, V>> cellSet = object.cellSet();
  codedOut.writeInt32NoTag(cellSet.size());
  for (Cell<R, C, V> cell : cellSet) {
    context.serialize(cell.getRowKey(), codedOut);
    context.serialize(cell.getColumnKey(), codedOut);
    context.serialize(cell.getValue(), codedOut);
  }
}
 
Example 17
Source File: ArrayCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SerializationContext context, Object[] obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeInt32NoTag(obj.length);
  try {
    for (Object item : obj) {
      context.serialize(item, codedOut);
    }
  } catch (StackOverflowError e) {
    // TODO(janakr): figure out if we need to handle this better and handle it better if so.
    throw new SerializationException("StackOverflow serializing array", e);
  }
}
 
Example 18
Source File: SerializationContextTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, ImmutableList<Object> obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  context = context.getMemoizingContext();
  codedOut.writeInt32NoTag(obj.size());
  for (Object item : obj) {
    context.serialize(item, codedOut);
  }
}
 
Example 19
Source File: ObjectCodecsTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(SerializationContext context, Integer obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeInt32NoTag(obj);
}
 
Example 20
Source File: PatternCodec.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(SerializationContext context, Pattern pattern, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  context.serialize(pattern.pattern(), codedOut);
  codedOut.writeInt32NoTag(pattern.flags());
}