下面列出了com.google.protobuf.CodedOutputStream# writeInt32NoTag ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
@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();
}
}
@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);
}
@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);
}
}
@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);
}
}
@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);
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
@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);
}
}
@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);
}
}
@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);
}
}
@Override
public void serialize(SerializationContext context, long[] obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
codedOut.writeInt32NoTag(obj.length);
for (long l : obj) {
codedOut.writeInt64NoTag(l);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@Override
public void serialize(SerializationContext context, Integer obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
codedOut.writeInt32NoTag(obj);
}
@Override
public void serialize(SerializationContext context, Pattern pattern, CodedOutputStream codedOut)
throws SerializationException, IOException {
context.serialize(pattern.pattern(), codedOut);
codedOut.writeInt32NoTag(pattern.flags());
}