下面列出了com.google.protobuf.Message#writeDelimitedTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private DataBuffer encodeValue(Message message, DataBufferFactory bufferFactory, boolean delimited) {
DataBuffer buffer = bufferFactory.allocateBuffer();
boolean release = true;
try {
if (delimited) {
message.writeDelimitedTo(buffer.asOutputStream());
}
else {
message.writeTo(buffer.asOutputStream());
}
release = false;
return buffer;
}
catch (IOException ex) {
throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex);
}
finally {
if (release) {
DataBufferUtils.release(buffer);
}
}
}
private DataBuffer encodeMessage(Message message, DataBufferFactory bufferFactory, boolean streaming) {
DataBuffer buffer = bufferFactory.allocateBuffer();
OutputStream outputStream = buffer.asOutputStream();
try {
if (streaming) {
message.writeDelimitedTo(outputStream);
}
else {
message.writeTo(outputStream);
}
return buffer;
}
catch (IOException ex) {
throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex);
}
}
/**
* Writes a delimited protocol buffer message in the same format as {@link
* MessageLite#writeDelimitedTo(java.io.OutputStream)}.
*
* <p>Unfortunately, {@link MessageLite#writeDelimitedTo(java.io.OutputStream)} may result in
* multiple calls to write on the underlying stream, so we have to provide this method here
* instead of the caller using it directly.
*/
@Override
public void write(Message m) {
Preconditions.checkNotNull(m);
final int size = m.getSerializedSize();
ByteArrayOutputStream bos =
new ByteArrayOutputStream(CodedOutputStream.computeUInt32SizeNoTag(size) + size);
try {
m.writeDelimitedTo(bos);
} catch (IOException e) {
// This should never happen with an in-memory stream.
exception.compareAndSet(null, new IllegalStateException(e.toString()));
return;
}
write(bos.toByteArray());
}
public Message callBlockingMethod(MethodDescriptorContainer methodDescriptor, Message request) throws ServiceException {
try {
dataOutputStream.writeUTF(methodDescriptor.getServiceDescriptorContainer().getName());
dataOutputStream.writeUTF(methodDescriptor.getName());
dataOutputStream.writeUTF(tokenHolder.getToken() == null ? "" : tokenHolder.getToken());
request.writeDelimitedTo(dataOutputStream);
dataOutputStream.flush();
DynamicMessage response = DynamicMessage.getDefaultInstance(methodDescriptor.getOutputDescriptor());
Builder responseBuilder = response.newBuilderForType();
responseBuilder.mergeDelimitedFrom(inputStream);
return responseBuilder.build();
} catch (IOException e) {
LOGGER.error("", e);
}
return null;
}
private static void send(final DataOutputStream out, final Op opcode,
final Message proto) throws IOException {
if (LOG.isTraceEnabled()) {
LOG.trace("Sending DataTransferOp " + proto.getClass().getSimpleName()
+ ": " + proto);
}
op(out, opcode);
proto.writeDelimitedTo(out);
out.flush();
}
private static void send(final DataOutputStream out, final Op opcode,
final Message proto) throws IOException {
if (LOG.isTraceEnabled()) {
LOG.trace("Sending DataTransferOp " + proto.getClass().getSimpleName()
+ ": " + proto);
}
op(out, opcode);
proto.writeDelimitedTo(out);
out.flush();
}
private static void writeToOutputStream(Message message,
ByteArrayOutputStream os, boolean isDelimited) throws IOException {
if (isDelimited) {
message.writeDelimitedTo(os);
} else {
message.writeTo(os);
}
}
private void setMessage(Message message)
throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
if (delimited) {
message.writeDelimitedTo(os);
} else {
message.writeTo(os);
}
input = new ByteArrayInputStream(os.toByteArray());
}
@Override
public void write(Message m) throws IOException {
Preconditions.checkNotNull(m);
m.writeDelimitedTo(stream);
}