下面列出了com.google.protobuf.CodedOutputStream# writeByteArrayNoTag ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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, 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);
}
@Override
public void serialize(SerializationContext context, T t, CodedOutputStream codedOut)
throws IOException {
// TODO(michajlo): See how usefully mnemonic actually winds up being for debugging, we may
// want to just toss it and trust that the classifier for this value is good enough.
codedOut.writeByteArrayNoTag(mnemonic);
}
@Override
public void serialize(
SerializationContext context,
OptionsDiffForReconstruction diff,
CodedOutputStream codedOut)
throws SerializationException, IOException {
OptionsDiffCache cache = context.getDependency(OptionsDiffCache.class);
ByteString bytes = cache.getBytesFromOptionsDiff(diff);
if (bytes == null) {
context = context.getNewNonMemoizingContext();
ByteString.Output byteStringOut = ByteString.newOutput();
CodedOutputStream bytesOut = CodedOutputStream.newInstance(byteStringOut);
context.serialize(diff.differingOptions, bytesOut);
context.serialize(diff.extraFirstFragmentClasses, bytesOut);
context.serialize(diff.extraSecondFragments, bytesOut);
bytesOut.writeByteArrayNoTag(diff.baseFingerprint);
context.serialize(diff.checksum, bytesOut);
context.serialize(diff.differingStarlarkOptions, bytesOut);
context.serialize(diff.extraFirstStarlarkOptions, bytesOut);
context.serialize(diff.extraSecondStarlarkOptions, bytesOut);
bytesOut.flush();
byteStringOut.flush();
int optionsDiffSize = byteStringOut.size();
bytes = byteStringOut.toByteString();
cache.putBytesFromOptionsDiff(diff, bytes);
logger.atFine().log(
"Serialized OptionsDiffForReconstruction %s. Diff took %d bytes.",
diff, optionsDiffSize);
}
codedOut.writeBytesNoTag(bytes);
}
/**
* 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;
}
}
@Override
public void serialize(SerializationContext context, ByteString obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
codedOut.writeByteArrayNoTag(obj.toByteArray());
}
@Override
public void serialize(SerializationContext context, BigInteger obj, CodedOutputStream codedOut)
throws IOException {
codedOut.writeByteArrayNoTag(obj.toByteArray());
}
@Override
public void serialize(SerializationContext context, byte[] obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
codedOut.writeByteArrayNoTag(obj);
}
@Override
public void serialize(SerializationContext context, HashCode obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
codedOut.writeByteArrayNoTag(obj.asBytes());
}