com.google.protobuf.Message.writeTo()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(256)

本文整理了Java中com.google.protobuf.Message.writeTo()方法的一些代码示例,展示了Message.writeTo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.writeTo()方法的具体详情如下:
包路径:com.google.protobuf.Message
类名称:Message
方法名:writeTo

Message.writeTo介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

  1. private DataBuffer encodeMessage(Message message, DataBufferFactory bufferFactory, boolean streaming) {
  2. DataBuffer buffer = bufferFactory.allocateBuffer();
  3. OutputStream outputStream = buffer.asOutputStream();
  4. try {
  5. if (streaming) {
  6. message.writeDelimitedTo(outputStream);
  7. }
  8. else {
  9. message.writeTo(outputStream);
  10. }
  11. return buffer;
  12. }
  13. catch (IOException ex) {
  14. throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex);
  15. }
  16. }

代码示例来源:origin: SonarSource/sonarqube

  1. /**
  2. * Writes a single message to {@code file}. Existing content is replaced, the message is not
  3. * appended.
  4. */
  5. public static void write(Message message, File toFile) {
  6. OutputStream out = null;
  7. try {
  8. out = new BufferedOutputStream(new FileOutputStream(toFile, false));
  9. message.writeTo(out);
  10. } catch (Exception e) {
  11. throw ContextException.of("Unable to write message", e).addContext("file", toFile);
  12. } finally {
  13. IOUtils.closeQuietly(out);
  14. }
  15. }

代码示例来源:origin: twitter/elephant-bird

  1. @Override
  2. public int compare(Message left, Message right) {
  3. try {
  4. leftBos.reset();
  5. rightBos.reset();
  6. left.writeTo(leftBos);
  7. right.writeTo(rightBos);
  8. return compareByteArrays(leftBos, rightBos);
  9. } catch (IOException e) {
  10. throw new RuntimeException(e);
  11. }
  12. }

代码示例来源:origin: voldemort/voldemort

  1. public static void writeMessage(DataOutputStream output, Message message) throws IOException {
  2. /*
  3. * We don't use varints here because the c++ version of the protocol
  4. * buffer classes seem to be buggy requesting more data than necessary
  5. * from the underlying stream causing it to block forever
  6. */
  7. output.writeInt(message.getSerializedSize());
  8. CodedOutputStream codedOut = CodedOutputStream.newInstance(output);
  9. message.writeTo(codedOut);
  10. codedOut.flush();
  11. }

代码示例来源:origin: org.springframework/spring-web

  1. private DataBuffer encodeMessage(Message message, DataBufferFactory bufferFactory, boolean streaming) {
  2. DataBuffer buffer = bufferFactory.allocateBuffer();
  3. OutputStream outputStream = buffer.asOutputStream();
  4. try {
  5. if (streaming) {
  6. message.writeDelimitedTo(outputStream);
  7. }
  8. else {
  9. message.writeTo(outputStream);
  10. }
  11. return buffer;
  12. }
  13. catch (IOException ex) {
  14. throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex);
  15. }
  16. }

代码示例来源:origin: line/armeria

  1. boolean success = false;
  2. try {
  3. message.writeTo(CodedOutputStream.newInstance(buf.nioBuffer(0, serializedSize)));
  4. buf.writerIndex(serializedSize);
  5. success = true;

代码示例来源:origin: SonarSource/sonarqube

  1. public static void writeProtobuf(Message msg, Request request, Response response) {
  2. OutputStream output = response.stream().output();
  3. try {
  4. if (request.getMediaType().equals(PROTOBUF)) {
  5. response.stream().setMediaType(PROTOBUF);
  6. msg.writeTo(output);
  7. } else {
  8. response.stream().setMediaType(JSON);
  9. try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(output, UTF_8))) {
  10. ProtobufJsonFormat.write(msg, writer);
  11. }
  12. }
  13. } catch (Exception e) {
  14. throw new IllegalStateException("Error while writing protobuf message", e);
  15. } finally {
  16. IOUtils.closeQuietly(output);
  17. }
  18. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Override
  2. protected void writeInternal(Message message, HttpOutputMessage outputMessage)
  3. throws IOException, HttpMessageNotWritableException {
  4. MediaType contentType = outputMessage.getHeaders().getContentType();
  5. if (contentType == null) {
  6. contentType = getDefaultContentType(message);
  7. Assert.state(contentType != null, "No content type");
  8. }
  9. Charset charset = contentType.getCharset();
  10. if (charset == null) {
  11. charset = DEFAULT_CHARSET;
  12. }
  13. if (PROTOBUF.isCompatibleWith(contentType)) {
  14. setProtoHeader(outputMessage, message);
  15. CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
  16. message.writeTo(codedOutputStream);
  17. codedOutputStream.flush();
  18. }
  19. else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
  20. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
  21. TextFormat.print(message, outputStreamWriter);
  22. outputStreamWriter.flush();
  23. outputMessage.getBody().flush();
  24. }
  25. else if (this.protobufFormatSupport != null) {
  26. this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
  27. outputMessage.getBody().flush();
  28. }
  29. }

代码示例来源:origin: org.springframework/spring-web

  1. @Override
  2. protected void writeInternal(Message message, HttpOutputMessage outputMessage)
  3. throws IOException, HttpMessageNotWritableException {
  4. MediaType contentType = outputMessage.getHeaders().getContentType();
  5. if (contentType == null) {
  6. contentType = getDefaultContentType(message);
  7. Assert.state(contentType != null, "No content type");
  8. }
  9. Charset charset = contentType.getCharset();
  10. if (charset == null) {
  11. charset = DEFAULT_CHARSET;
  12. }
  13. if (PROTOBUF.isCompatibleWith(contentType)) {
  14. setProtoHeader(outputMessage, message);
  15. CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
  16. message.writeTo(codedOutputStream);
  17. codedOutputStream.flush();
  18. }
  19. else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
  20. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
  21. TextFormat.print(message, outputStreamWriter);
  22. outputStreamWriter.flush();
  23. outputMessage.getBody().flush();
  24. }
  25. else if (this.protobufFormatSupport != null) {
  26. this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
  27. outputMessage.getBody().flush();
  28. }
  29. }

代码示例来源:origin: org.apache.hadoop/hadoop-common

  1. private byte[] setupResponseForProtobuf(
  2. RpcResponseHeaderProto header, Writable rv) throws IOException {
  3. Message payload = (rv != null)
  4. ? ((RpcWritable.ProtobufWrapper)rv).getMessage() : null;
  5. int length = getDelimitedLength(header);
  6. if (payload != null) {
  7. length += getDelimitedLength(payload);
  8. }
  9. byte[] buf = new byte[length + 4];
  10. CodedOutputStream cos = CodedOutputStream.newInstance(buf);
  11. // the stream only supports little endian ints
  12. cos.writeRawByte((byte)((length >>> 24) & 0xFF));
  13. cos.writeRawByte((byte)((length >>> 16) & 0xFF));
  14. cos.writeRawByte((byte)((length >>> 8) & 0xFF));
  15. cos.writeRawByte((byte)((length >>> 0) & 0xFF));
  16. cos.writeRawVarint32(header.getSerializedSize());
  17. header.writeTo(cos);
  18. if (payload != null) {
  19. cos.writeRawVarint32(payload.getSerializedSize());
  20. payload.writeTo(cos);
  21. }
  22. return buf;
  23. }

代码示例来源:origin: google/bundletool

  1. private void writeProtoFile(Message proto, Path outputFile) {
  2. try (OutputStream outputStream = BufferedIo.outputStream(outputFile)) {
  3. proto.writeTo(outputStream);
  4. } catch (FileNotFoundException e) {
  5. throw new UncheckedIOException(
  6. String.format("Can't create the output file '%s'.", outputFile), e);
  7. } catch (IOException e) {
  8. throw new UncheckedIOException(
  9. String.format("Error while writing the output file '%s'.", outputFile), e);
  10. }
  11. }
  12. }

代码示例来源:origin: googleapis/api-compiler

  1. /** Writes a proto out to a file. */
  2. public static void writeProto(Message content, String outputName) throws IOException {
  3. try (OutputStream outputStream = new FileOutputStream(outputName)) {
  4. content.writeTo(outputStream);
  5. }
  6. }

代码示例来源:origin: googleapis/api-compiler

  1. /**
  2. * Writes a proto to a file in binary format.
  3. */
  4. public static void writeProtoBinaryToFile(File outputFile, Message proto)
  5. throws IOException {
  6. try (OutputStream prodOutputStream = new FileOutputStream(outputFile)) {
  7. proto.writeTo(prodOutputStream);
  8. }
  9. }

代码示例来源:origin: baidu/brpc-java

  1. @Override
  2. public void outputWriteToStream(Object output, CodedOutputStream stream) throws IOException {
  3. if (output instanceof Message) {
  4. ((Message) output).writeTo(stream);
  5. stream.flush();
  6. }
  7. }

代码示例来源:origin: com.baidu/brpc-java

  1. @Override
  2. public void inputWriteToStream(Object input, CodedOutputStream stream) throws IOException {
  3. if (input instanceof Message) {
  4. ((Message) input).writeTo(stream);
  5. stream.flush();
  6. }
  7. }

代码示例来源:origin: com.baidu/brpc-java

  1. @Override
  2. public void outputWriteToStream(Object output, CodedOutputStream stream) throws IOException {
  3. if (output instanceof Message) {
  4. ((Message) output).writeTo(stream);
  5. stream.flush();
  6. }
  7. }

代码示例来源:origin: baidu/brpc-java

  1. @Override
  2. public void inputWriteToStream(Object input, CodedOutputStream stream) throws IOException {
  3. if (input instanceof Message) {
  4. ((Message) input).writeTo(stream);
  5. stream.flush();
  6. }
  7. }

代码示例来源:origin: indeedeng/imhotep

  1. public static void sendProtobuf(Message request, OutputStream os) throws IOException {
  2. os.write(Bytes.intToBytes(request.getSerializedSize()));
  3. request.writeTo(os);
  4. os.flush();
  5. }

代码示例来源:origin: org.apache.beam/beam-sdks-java-extensions-protobuf

  1. @Override
  2. public void encode(T value, OutputStream outStream, Context context) throws IOException {
  3. if (value == null) {
  4. throw new CoderException("cannot encode a null " + protoMessageClass.getSimpleName());
  5. }
  6. if (context.isWholeStream) {
  7. value.writeTo(outStream);
  8. } else {
  9. value.writeDelimitedTo(outStream);
  10. }
  11. }

代码示例来源:origin: com.twitter.elephantbird/elephant-bird-core

  1. protected void serialize() throws IOException {
  2. out_.write(Protobufs.KNOWN_GOOD_POSITION_MARKER);
  3. Message block = SerializedBlock
  4. .newInstance(innerClass_.getCanonicalName(),protoBlobs_)
  5. .getMessage();
  6. protoBlobs_ = new ArrayList<ByteString>(numRecordsPerBlock_);
  7. writeRawLittleEndian32(block.getSerializedSize());
  8. block.writeTo(out_);
  9. }

相关文章