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

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

本文整理了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

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);
  }
}

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

/**
 * Writes a single message to {@code file}. Existing content is replaced, the message is not
 * appended.
 */
public static void write(Message message, File toFile) {
 OutputStream out = null;
 try {
  out = new BufferedOutputStream(new FileOutputStream(toFile, false));
  message.writeTo(out);
 } catch (Exception e) {
  throw ContextException.of("Unable to write message", e).addContext("file", toFile);
 } finally {
  IOUtils.closeQuietly(out);
 }
}

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

@Override
public int compare(Message left, Message right) {
 try {
  leftBos.reset();
  rightBos.reset();
  left.writeTo(leftBos);
  right.writeTo(rightBos);
  return compareByteArrays(leftBos, rightBos);
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

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

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

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

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);
  }
}

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

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

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

public static void writeProtobuf(Message msg, Request request, Response response) {
 OutputStream output = response.stream().output();
 try {
  if (request.getMediaType().equals(PROTOBUF)) {
   response.stream().setMediaType(PROTOBUF);
   msg.writeTo(output);
  } else {
   response.stream().setMediaType(JSON);
   try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(output, UTF_8))) {
    ProtobufJsonFormat.write(msg, writer);
   }
  }
 } catch (Exception e) {
  throw new IllegalStateException("Error while writing protobuf message", e);
 } finally {
  IOUtils.closeQuietly(output);
 }
}

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

@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
    throws IOException, HttpMessageNotWritableException {
  MediaType contentType = outputMessage.getHeaders().getContentType();
  if (contentType == null) {
    contentType = getDefaultContentType(message);
    Assert.state(contentType != null, "No content type");
  }
  Charset charset = contentType.getCharset();
  if (charset == null) {
    charset = DEFAULT_CHARSET;
  }
  if (PROTOBUF.isCompatibleWith(contentType)) {
    setProtoHeader(outputMessage, message);
    CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
    message.writeTo(codedOutputStream);
    codedOutputStream.flush();
  }
  else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
    TextFormat.print(message, outputStreamWriter);
    outputStreamWriter.flush();
    outputMessage.getBody().flush();
  }
  else if (this.protobufFormatSupport != null) {
    this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
    outputMessage.getBody().flush();
  }
}

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

@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
    throws IOException, HttpMessageNotWritableException {
  MediaType contentType = outputMessage.getHeaders().getContentType();
  if (contentType == null) {
    contentType = getDefaultContentType(message);
    Assert.state(contentType != null, "No content type");
  }
  Charset charset = contentType.getCharset();
  if (charset == null) {
    charset = DEFAULT_CHARSET;
  }
  if (PROTOBUF.isCompatibleWith(contentType)) {
    setProtoHeader(outputMessage, message);
    CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
    message.writeTo(codedOutputStream);
    codedOutputStream.flush();
  }
  else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
    TextFormat.print(message, outputStreamWriter);
    outputStreamWriter.flush();
    outputMessage.getBody().flush();
  }
  else if (this.protobufFormatSupport != null) {
    this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
    outputMessage.getBody().flush();
  }
}

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

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;
}

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

private void writeProtoFile(Message proto, Path outputFile) {
  try (OutputStream outputStream = BufferedIo.outputStream(outputFile)) {
   proto.writeTo(outputStream);
  } catch (FileNotFoundException e) {
   throw new UncheckedIOException(
     String.format("Can't create the output file '%s'.", outputFile), e);
  } catch (IOException e) {
   throw new UncheckedIOException(
     String.format("Error while writing the output file '%s'.", outputFile), e);
  }
 }
}

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

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

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

/**
 * Writes a proto to a file in binary format.
 */
public static void writeProtoBinaryToFile(File outputFile, Message proto)
  throws IOException {
 try (OutputStream prodOutputStream = new FileOutputStream(outputFile)) {
  proto.writeTo(prodOutputStream);
 }
}

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

@Override
public void outputWriteToStream(Object output, CodedOutputStream stream) throws IOException {
  if (output instanceof Message) {
    ((Message) output).writeTo(stream);
    stream.flush();
  }
}

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

@Override
public void inputWriteToStream(Object input, CodedOutputStream stream) throws IOException {
  if (input instanceof Message) {
    ((Message) input).writeTo(stream);
    stream.flush();
  }
}

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

@Override
public void outputWriteToStream(Object output, CodedOutputStream stream) throws IOException {
  if (output instanceof Message) {
    ((Message) output).writeTo(stream);
    stream.flush();
  }
}

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

@Override
public void inputWriteToStream(Object input, CodedOutputStream stream) throws IOException {
  if (input instanceof Message) {
    ((Message) input).writeTo(stream);
    stream.flush();
  }
}

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

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

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

@Override
public void encode(T value, OutputStream outStream, Context context) throws IOException {
 if (value == null) {
  throw new CoderException("cannot encode a null " + protoMessageClass.getSimpleName());
 }
 if (context.isWholeStream) {
  value.writeTo(outStream);
 } else {
  value.writeDelimitedTo(outStream);
 }
}

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

protected void serialize() throws IOException {
 out_.write(Protobufs.KNOWN_GOOD_POSITION_MARKER);
 Message block = SerializedBlock
   .newInstance(innerClass_.getCanonicalName(),protoBlobs_)
   .getMessage();
 protoBlobs_ = new ArrayList<ByteString>(numRecordsPerBlock_);
 writeRawLittleEndian32(block.getSerializedSize());
 block.writeTo(out_);
}

相关文章