本文整理匯總了Java中com.google.protobuf.CodedOutputStream.newInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java CodedOutputStream.newInstance方法的具體用法?Java CodedOutputStream.newInstance怎麽用?Java CodedOutputStream.newInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.protobuf.CodedOutputStream
的用法示例。
在下文中一共展示了CodedOutputStream.newInstance方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encode
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception
{
if (!(msg instanceof ChannelBuffer))
{
return msg;
}
ChannelBuffer body = (ChannelBuffer) msg;
int length = body.readableBytes();
ChannelBuffer header = channel.getConfig().getBufferFactory()
.getBuffer(body.order(), CodedOutputStream.computeRawVarint64Size(length) + 4);
CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(new ChannelBufferOutputStream(header));
codedOutputStream.writeRawVarint64(length);
int value = 0x0;
value |= (0x0 & 0xff);// network version
value |= ((0x0 & 0xff) << 8);// type
if (ctx.getPipeline().get("cryptoEncoder") != null) value |= ((0x1 & 0xf) << 16);// crypto type
else value |= ((0x0 & 0xf) << 16);// crypto type
value |= ((0x0 & 0xfff) << 20);// version
codedOutputStream.writeRawLittleEndian32(value);
codedOutputStream.flush();
return wrappedBuffer(header, body);
}
示例2: toChannelBuffer
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
/**
* Serializes the given protobuf object into a Netty {@link ChannelBuffer}.
* @param method The name of the method of the RPC we're going to send.
* @param pb The protobuf to serialize.
* @return A new channel buffer containing the serialized protobuf, with
* enough free space at the beginning to tack on the RPC header.
*/
static final ChannelBuffer toChannelBuffer(final byte[] method,
final AbstractMessageLite pb) {
final int pblen = pb.getSerializedSize();
final int vlen = CodedOutputStream.computeRawVarint32Size(pblen);
final byte[] buf = new byte[4 + 19 + method.length + vlen + pblen];
try {
final CodedOutputStream out = CodedOutputStream.newInstance(buf, 4 + 19 + method.length,
vlen + pblen);
out.writeRawVarint32(pblen);
pb.writeTo(out);
out.checkNoSpaceLeft();
} catch (IOException e) {
throw new RuntimeException("Should never happen", e);
}
return ChannelBuffers.wrappedBuffer(buf);
}
示例3: saveProperties
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
/**
* Saves track properties by modifying only file tail.
*/
public void saveProperties(FileDataSource source) throws Exception {
Track track = source.tracks.get(0);
// Prepare new properties tail
ByteBuffer buffer = ByteBuffer.allocate(getSerializedPropertiesSize(track));
CodedOutputStream output = CodedOutputStream.newInstance(buffer);
output.writeBytes(FIELD_NAME, ByteString.copyFromUtf8(track.name));
output.writeUInt32(FIELD_COLOR, track.style.color);
output.writeFloat(FIELD_WIDTH, track.style.width);
output.flush();
// Modify tail of file
File file = new File(source.path);
long createTime = file.lastModified();
RandomAccessFile access = new RandomAccessFile(file, "rw");
access.setLength(source.propertiesOffset + 1);
access.seek(source.propertiesOffset);
access.write(buffer.array());
access.close();
//noinspection ResultOfMethodCallIgnored
file.setLastModified(createTime);
}
示例4: toProtos
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
private static <T> List<byte[]> toProtos(ProtobufCodec<T> codec, Collection<T> objs)
throws OrmException {
List<byte[]> result = Lists.newArrayListWithCapacity(objs.size());
ByteArrayOutputStream out = new ByteArrayOutputStream(256);
try {
for (T obj : objs) {
out.reset();
CodedOutputStream cos = CodedOutputStream.newInstance(out);
codec.encode(obj, cos);
cos.flush();
result.add(out.toByteArray());
}
} catch (IOException e) {
throw new OrmException(e);
}
return result;
}
示例5: setupResponseForProtobuf
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
private byte[] setupResponseForProtobuf(
RpcResponseHeaderProto header, Writable rv) throws IOException {
Message payload = (rv != null)
? ((RpcWritable.ProtobufWrapper)rv).getMessage() : null;
int length = getDelimitedLength(header);
if (payload != null) {
length += getDelimitedLength(payload);
}
byte[] buf = new byte[length + 4];
CodedOutputStream cos = CodedOutputStream.newInstance(buf);
// the stream only supports little endian ints
cos.writeRawByte((byte)((length >>> 24) & 0xFF));
cos.writeRawByte((byte)((length >>> 16) & 0xFF));
cos.writeRawByte((byte)((length >>> 8) & 0xFF));
cos.writeRawByte((byte)((length >>> 0) & 0xFF));
cos.writeRawVarint32(header.getSerializedSize());
header.writeTo(cos);
if (payload != null) {
cos.writeRawVarint32(payload.getSerializedSize());
payload.writeTo(cos);
}
return buf;
}
示例6: testSerializeDeserializeInBulk
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
@Test
public void testSerializeDeserializeInBulk() throws Exception {
Integer value1 = 12345;
Integer value2 = 67890;
Integer value3 = 42;
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
CodedOutputStream codedOut = CodedOutputStream.newInstance(bytesOut);
underTest.serialize(KNOWN_CLASSIFIER, value1, codedOut);
underTest.serialize(KNOWN_CLASSIFIER, value2, codedOut);
underTest.serialize(KNOWN_CLASSIFIER, value3, codedOut);
codedOut.flush();
CodedInputStream codedIn = CodedInputStream.newInstance(bytesOut.toByteArray());
assertThat(underTest.deserialize(KNOWN_CLASSIFIER_BYTES, codedIn)).isEqualTo(value1);
assertThat(underTest.deserialize(KNOWN_CLASSIFIER_BYTES, codedIn)).isEqualTo(value2);
assertThat(underTest.deserialize(KNOWN_CLASSIFIER_BYTES, codedIn)).isEqualTo(value3);
}
示例7: serializeOneNestedSet
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
private void serializeOneNestedSet(
Object children, CodedOutputStream codedOut, Map<Object, byte[]> childToDigest)
throws IOException, SerializationException {
// Serialize nested set into an inner byte array so we can take its digest
ByteArrayOutputStream childOutputStream = new ByteArrayOutputStream();
HashingOutputStream hashingOutputStream =
new HashingOutputStream(Hashing.md5(), childOutputStream);
CodedOutputStream childCodedOut = CodedOutputStream.newInstance(hashingOutputStream);
if (children instanceof Object[]) {
serializeMultiItemChildArray((Object[]) children, childToDigest, childCodedOut);
} else if (children != NestedSet.EMPTY_CHILDREN) {
serializeSingleItemChildArray(children, childCodedOut);
} else {
// Empty set
childCodedOut.writeInt32NoTag(0);
}
childCodedOut.flush();
byte[] digest = hashingOutputStream.hash().asBytes();
codedOut.writeByteArrayNoTag(digest);
byte[] childBytes = childOutputStream.toByteArray();
codedOut.writeByteArrayNoTag(childBytes);
childToDigest.put(children, digest);
}
示例8: encode
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
@Override
protected void encode(
ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
int bodyLen = msg.readableBytes();
int headerLen = CodedOutputStream.computeRawVarint32Size(bodyLen);
out.ensureWritable(headerLen + bodyLen);
CodedOutputStream headerOut =
CodedOutputStream.newInstance(new ByteBufOutputStream(out), headerLen);
headerOut.writeRawVarint32(bodyLen);
headerOut.flush();
out.writeBytes(msg, msg.readerIndex(), bodyLen);
}
示例9: readRecordBytes
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
public byte[] readRecordBytes() throws IOException {
in.resetSizeCounter();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
CodedOutputStream os = CodedOutputStream.newInstance(bos);
byte[] recordBytes = in.readByteArray();
os.writeByteArrayNoTag(recordBytes);
os.flush();
return bos.toByteArray();
}
示例10: ProtoConnection
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
public ProtoConnection(NonBlockingConnection connection) {
this.connection = connection;
input = connection.getInputStream();
output = connection.getOutputStream();
codedInput = CodedInputStream.newInstance(input);
codedOutput = CodedOutputStream.newInstance(output);
}
示例11: makeConnectionMessage
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
private static byte[] makeConnectionMessage(Counter.Value value)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
CodedOutputStream codedOutput = CodedOutputStream.newInstance(out);
codedOutput.writeRawLittleEndian32(value.getSerializedSize());
value.writeTo(codedOutput);
codedOutput.flush();
byte[] all = out.toByteArray();
return all;
}
示例12: doVarIntTest
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
private void doVarIntTest(int value) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CodedOutputStream cout = CodedOutputStream.newInstance(baos);
cout.writeRawVarint32(value);
cout.flush();
DataInputStream dis = new DataInputStream(
new ByteArrayInputStream(baos.toByteArray()));
assertEquals(value, ProtoUtil.readRawVarint32(dis));
}
示例13: encode
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
protected void encode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf1, ByteBuf paramByteBuf2)
throws Exception {
int i = paramByteBuf1.readableBytes();
int j = CodedOutputStream.computeRawVarint32Size(i);
paramByteBuf2.ensureWritable(j + i);
CodedOutputStream localCodedOutputStream = CodedOutputStream.newInstance(new ByteBufOutputStream(paramByteBuf2), j);
localCodedOutputStream.writeRawVarint32(i);
localCodedOutputStream.flush();
paramByteBuf2.writeBytes(paramByteBuf1, paramByteBuf1.readerIndex(), i);
}
示例14: encode
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
protected void encode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf1, ByteBuf paramByteBuf2)
throws Exception {
int i = paramByteBuf1.readableBytes();
int j = CodedOutputStream.computeRawVarint32Size(i);
paramByteBuf2.ensureWritable(j + i);
CodedOutputStream localCodedOutputStream = CodedOutputStream.newInstance(new ByteBufOutputStream(paramByteBuf2), j);
localCodedOutputStream.writeRawVarint32(i);
localCodedOutputStream.flush();
paramByteBuf2.writeBytes(paramByteBuf1, paramByteBuf1.readerIndex(), i);
paramByteBuf2.release();
}
示例15: savePreprocessedBlock
import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
private void savePreprocessedBlock(BlockContext context, List<RtbImpression> impressions) throws IOException {
try (OutputStream os = context.createOutputStream(PREPROCESS)) {
CodedOutputStream cos = CodedOutputStream.newInstance(os);
for (RtbImpression impression : impressions) {
if (impression == null) continue;;
byte[] bytes = impression.toByteArray();
cos.writeUInt32NoTag(bytes.length);
cos.writeRawBytes(bytes);
}
cos.flush();
}
}