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

The following examples show how to use com.google.protobuf.CodedOutputStream#writeByteArrayNoTag() . 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: 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 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: SingletonCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: BuildOptions.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
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 6
Source File: ByteStringCodec.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(SerializationContext context, ByteString obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeByteArrayNoTag(obj.toByteArray());
}
 
Example 7
Source File: BigIntegerCodec.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(SerializationContext context, BigInteger obj, CodedOutputStream codedOut)
    throws IOException {
  codedOut.writeByteArrayNoTag(obj.toByteArray());
}
 
Example 8
Source File: ByteArrayCodec.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(SerializationContext context, byte[] obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeByteArrayNoTag(obj);
}
 
Example 9
Source File: HashCodeCodec.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(SerializationContext context, HashCode obj, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeByteArrayNoTag(obj.asBytes());
}