Java Code Examples for com.google.protobuf.Message#writeDelimitedTo()

The following examples show how to use com.google.protobuf.Message#writeDelimitedTo() . 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: ProtobufEncoder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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);
			}
		}
	}
 
Example 2
Source File: ProtobufEncoder.java    From java-technology-stack with MIT License 6 votes vote down vote up
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);
	}
}
 
Example 3
Source File: AsynchronousFileOutputStream.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
}
 
Example 4
Source File: SocketProtocolBuffersChannel.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: Sender.java    From hadoop with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 6
Source File: Sender.java    From big-c with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: SocketConnectionTest.java    From protobuf-socket-rpc with MIT License 5 votes vote down vote up
private static void writeToOutputStream(Message message,
    ByteArrayOutputStream os, boolean isDelimited) throws IOException {
  if (isDelimited) {
    message.writeDelimitedTo(os);
  } else {
    message.writeTo(os);
  }
}
 
Example 8
Source File: FakeSocket.java    From protobuf-socket-rpc with MIT License 5 votes vote down vote up
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());
}
 
Example 9
Source File: MessageOutputStreamWrapper.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Message m) throws IOException {
  Preconditions.checkNotNull(m);
  m.writeDelimitedTo(stream);
}