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

The following examples show how to use com.google.protobuf.CodedOutputStream#newInstance() . 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: StateTest.java    From zetasketch with Apache License 2.0 7 votes vote down vote up
@Test
public void parseUnknownField() throws IOException {
  // Create an aggregator state proto with an unknown field in the middle.
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  CodedOutputStream coded = CodedOutputStream.newInstance(stream);
  coded.writeInt32(AggregatorStateProto.NUM_VALUES_FIELD_NUMBER, 42);
  coded.writeString(999, "foobar");
  coded.writeInt32(AggregatorStateProto.ENCODING_VERSION_FIELD_NUMBER, 43);
  coded.flush();

  // Check that we can parse the proto, despite the unknown field.
  State state = new State();
  state.parse(CodedInputStream.newInstance(stream.toByteArray()));

  // Check that the fields before and after the unknown fields were correctly read.
  assertEquals(42, state.numValues);
  assertEquals(43, state.encodingVersion);
}
 
Example 2
Source File: GzipCompress.java    From brpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf compressInput(Object proto, RpcMethodInfo rpcMethodInfo) throws IOException {
    int protoSize = rpcMethodInfo.getInputSerializedSize(proto);
    ByteBuf resBuf = Unpooled.buffer(protoSize);
    OutputStream outputStream = new ByteBufOutputStream(resBuf);
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);

    if (protoSize > CodedOutputStream.DEFAULT_BUFFER_SIZE) {
        protoSize = CodedOutputStream.DEFAULT_BUFFER_SIZE;
    }
    final CodedOutputStream codedOutputStream =
            CodedOutputStream.newInstance(gzipOutputStream, protoSize);
    rpcMethodInfo.inputWriteToStream(proto, codedOutputStream);
    gzipOutputStream.close();
    return resBuf;
}
 
Example 3
Source File: PhysicalWriterImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
public PhysicalWriterImpl(FSDataOutputStream out, OrcFile.WriterOptions opts) throws IOException {
	if (opts.isEnforceBufferSize()) {
		this.bufferSize = opts.getBufferSize();
	} else {
		this.bufferSize = getEstimatedBufferSize(
			opts.getStripeSize(), opts.getSchema().getMaximumId() + 1, opts.getBufferSize());
	}

	this.out = out;
	this.blockOffset = 0;
	this.blockSize = opts.getBlockSize();
	this.maxPadding = (int) (opts.getPaddingTolerance() * (double) opts.getBufferSize());
	this.compress = opts.getCompress();
	this.codec = OrcCodecPool.getCodec(this.compress);
	this.streams  = new TreeMap<>();
	this.writer = new OutStream("metadata", this.bufferSize, this.codec, new DirectStream(this.out));
	this.shims = opts.getHadoopShims();
	this.addBlockPadding = opts.getBlockPadding();
	this.protobufWriter = CodedOutputStream.newInstance(this.writer);
	this.writeVariableLengthBlocks = opts.getWriteVariableLengthBlocks();
}
 
Example 4
Source File: ProtoDiff.java    From metastore with Apache License 2.0 5 votes vote down vote up
private ByteString serializePayload(Message field) {
  ByteBuffer byteBuffer = ByteBuffer.allocate(field.getSerializedSize());
  try {
    CodedOutputStream stream = CodedOutputStream.newInstance(byteBuffer);
    field.writeTo(stream);
  } catch (IOException e) {
    throw new RuntimeException("failed to serialize unknown field with number ", e);
  }
  return ByteString.copyFrom(byteBuffer);
}
 
Example 5
Source File: TestJprotobufRpcMethodInfo.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testInputWriteToStream() throws Exception {
    Method method = EchoService.class.getMethod("echo", EchoRequest.class);
    JprotobufRpcMethodInfo rpcMethodInfo = new JprotobufRpcMethodInfo(method);

    EchoRequest request = new EchoRequest();
    request.setMessage("hello");
    ByteBuf buf = Unpooled.buffer(64);
    OutputStream outputStream = new ByteBufOutputStream(buf);
    CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream);
    rpcMethodInfo.inputWriteToStream(request, codedOutputStream);
    Assert.assertTrue(buf.readableBytes()
            == Echo.EchoRequest.newBuilder().setMessage("hello").build().getSerializedSize());
}
 
Example 6
Source File: TestUtils.java    From bazel with Apache License 2.0 5 votes vote down vote up
public static ByteString toBytesMemoized(Object original, ObjectCodecRegistry registry)
    throws IOException, SerializationException {
  ByteString.Output output = ByteString.newOutput();
  CodedOutputStream codedOut = CodedOutputStream.newInstance(output);
  new ObjectCodecs(registry).serializeMemoized(original, codedOut);
  codedOut.flush();
  return output.toByteString();
}
 
Example 7
Source File: BasicRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
public ByteBuffer serializeV1( BasicRollup basicRollup ) {

    try {
        byte[] buf = new byte[sizeOf( basicRollup, VERSION_1_ROLLUP)];
        CodedOutputStream protobufOut = CodedOutputStream.newInstance(buf);
        serializeRollupV1( basicRollup, protobufOut );
        return ByteBuffer.wrap(buf);
    } catch(IOException e) {
        throw new RuntimeException(e);
    }

}
 
Example 8
Source File: ReflectiveCodec.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode(T t) throws IOException {
	if (t == null) {
		throw new RuntimeException("target object to encode is null.");
	}

	int size = size(t);
	byte[] bytes = new byte[size];
	CodedOutputStream out = CodedOutputStream.newInstance(bytes);
	writeTo(t, out);

	return bytes;
}
 
Example 9
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 10
Source File: WalletProtobufSerializer.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Formats the given wallet (transactions and keys) to the given output stream in protocol buffer format.<p>
 * <p>
 * Equivalent to <tt>walletToProto(wallet).writeTo(output);</tt>
 */
public void writeWallet(Wallet wallet, OutputStream output) throws IOException {
    Protos.Wallet walletProto = walletToProto(wallet);
    final CodedOutputStream codedOutput = CodedOutputStream.newInstance(output, this.walletWriteBufferSize);
    walletProto.writeTo(codedOutput);
    codedOutput.flush();
}
 
Example 11
Source File: BasicRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
protected void serializeRollup(BasicRollup basicRollup, byte[] buf) throws IOException {
    rollupSize.update(buf.length);
    CodedOutputStream protobufOut = CodedOutputStream.newInstance(buf);
    protobufOut.writeRawByte(Constants.VERSION_2_ROLLUP);

    serializeBaseRollupHelper( basicRollup, protobufOut );

    if (basicRollup.getCount() > 0) {
        protobufOut.writeDoubleNoTag( basicRollup.getSum() );
    }
}
 
Example 12
Source File: ChangeSetHelper.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] save(ChangeSet changeSet) {
    ArgumentValidation.notNull("changeSet", changeSet);
    checkFields(changeSet);
    int size = changeSet.getSerializedSize();
    byte[] buffer = new byte[size];
    CodedOutputStream stream = CodedOutputStream.newInstance(buffer);
    try {
        changeSet.writeTo(stream);
    } catch(IOException e) {
        // Only throws OutOfSpace, which shouldn't happen
        throw new IllegalStateException(e);
    }
    return buffer;
}
 
Example 13
Source File: WriterImpl.java    From hive-dwrf with Apache License 2.0 5 votes vote down vote up
private void ensureWriter() throws IOException {
  if (rawWriter == null) {
    rawWriter = fs.create(path, false);
    rawWriter.writeBytes(OrcFile.MAGIC);
    headerLength = rawWriter.getPos();
    writer = new OutStream("metadata", bufferSize, codec,
      new DirectStream(rawWriter), memoryEstimate);
    protobufWriter = CodedOutputStream.newInstance(writer);
  }
}
 
Example 14
Source File: ProtoBufArray.java    From LiquidDonkey with MIT License 5 votes vote down vote up
/**
 * Encode custom protobuf variable length array.
 *
 * @param <T> the item type
 * @param items the list of items, not null
 * @return the encoded list, not null
 * @throws IOException, not null
 * @throws NullPointerException if any arguments are null
 */
public static <T extends GeneratedMessage> byte[] encode(List<T> items) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    CodedOutputStream stream = CodedOutputStream.newInstance(bytes);
    for (T item : items) {
        byte[] encoded = item.toByteArray();
        stream.writeRawVarint32(encoded.length);
        stream.writeRawBytes(encoded);
    }
    stream.flush();
    return bytes.toByteArray();
}
 
Example 15
Source File: State.java    From zetasketch with Apache License 2.0 5 votes vote down vote up
public byte[] toByteArray() {
  try {
    final byte[] result = new byte[getSerializedSize()];
    final CodedOutputStream output = CodedOutputStream.newInstance(result);
    writeTo(output);
    output.checkNoSpaceLeft();
    return result;
  } catch (IOException e) {
    throw new RuntimeException("Unexpected IOException serializing to byte array", e);
  }
}
 
Example 16
Source File: WalletProtobufSerializer.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Formats the given wallet (transactions and keys) to the given output stream in protocol buffer format.<p>
 *
 * Equivalent to <tt>walletToProto(wallet).writeTo(output);</tt>
 */
public void writeWallet(Wallet wallet, OutputStream output) throws IOException {
    Protos.Wallet walletProto = walletToProto(wallet);
    final CodedOutputStream codedOutput = CodedOutputStream.newInstance(output, this.walletWriteBufferSize);
    walletProto.writeTo(codedOutput);
    codedOutput.flush();
}
 
Example 17
Source File: WalletClient.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] serializeProtobuf(final GeneratedMessage msg) {
    final byte[] byteArray = new byte[msg.getSerializedSize()];
    final CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(byteArray);
    try {
        msg.writeTo(codedOutputStream);
    } catch (final IOException e) {
        Log.e(TAG, "failed to serialize message: " + e.toString());
        e.printStackTrace();
        return null;
    }
    return byteArray;
}
 
Example 18
Source File: CpuProfiler.java    From bazel with Apache License 2.0 4 votes vote down vote up
private long getLocationID(Debug.Frame fr) throws IOException {
  StarlarkCallable fn = fr.getFunction();
  // fnAddr identifies a function as a whole.
  int fnAddr = System.identityHashCode(fn); // very imperfect

  // pcAddr identifies the current program point.
  //
  // For now, this is the same as fnAddr, because
  // we don't track the syntax node currently being
  // evaluated. Statement-level profile information
  // in the leaf function (displayed by 'pprof list <fn>')
  // is thus unreliable for now.
  long pcAddr = fnAddr;
  if (fn instanceof StarlarkFunction) {
    // TODO(adonovan): when we use a byte code representation
    // of function bodies, mix the program counter fr.pc into fnAddr.
    // TODO(adonovan): even cleaner: treat each function's byte
    // code segment as its own Profile.Mapping, indexed by pc.
    //
    // pcAddr = (pcAddr << 16) ^ fr.pc;
  }

  Long id = locationIDs.get(pcAddr);
  if (id == null) {
    id = pcAddr;

    ByteArrayOutputStream line = new ByteArrayOutputStream();
    CodedOutputStream lineenc = CodedOutputStream.newInstance(line);
    lineenc.writeUInt64(LINE_FUNCTION_ID, getFunctionID(fn, fnAddr));
    lineenc.writeInt64(LINE_LINE, (long) fr.getLocation().line());
    lineenc.flush();

    ByteArrayOutputStream loc = new ByteArrayOutputStream();
    CodedOutputStream locenc = CodedOutputStream.newInstance(loc);
    locenc.writeUInt64(LOCATION_ID, id);
    locenc.writeUInt64(LOCATION_ADDRESS, pcAddr);
    locenc.writeByteArray(LOCATION_LINE, line.toByteArray());
    locenc.flush();
    enc.writeByteArray(PROFILE_LOCATION, loc.toByteArray());

    locationIDs.put(pcAddr, id);
  }
  return id;
}
 
Example 19
Source File: BlockListAsLongs.java    From hadoop with Apache License 2.0 4 votes vote down vote up
Builder() {
  out = ByteString.newOutput(64*1024);
  cos = CodedOutputStream.newInstance(out);
}
 
Example 20
Source File: ProtoOutputFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
private StreamedQueryResultFormatter(OutputStream out) {
  this.codedOut = CodedOutputStream.newInstance(out, OUTPUT_BUFFER_SIZE);
}