下面列出了com.google.protobuf.CodedOutputStream# newInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
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();
}
@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;
}
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;
}
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);
}
/**
* 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();
}
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);
}
}
/**
* 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();
}
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);
}
}
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;
}
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() );
}
}
/**
* 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();
}
@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);
}
@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;
}
@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);
}
}
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();
}
@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());
}
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;
}
Builder() {
out = ByteString.newOutput(64*1024);
cos = CodedOutputStream.newInstance(out);
}
private StreamedQueryResultFormatter(OutputStream out) {
this.codedOut = CodedOutputStream.newInstance(out, OUTPUT_BUFFER_SIZE);
}