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 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); } }
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(); }
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); } }
boolean success = false; try { message.writeTo(CodedOutputStream.newInstance(buf.nioBuffer(0, serializedSize))); buf.writerIndex(serializedSize); success = true;
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); } }
@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(); } }
@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(); } }
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; }
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); } } }
/** 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); } }
/** * 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); } }
@Override public void outputWriteToStream(Object output, CodedOutputStream stream) throws IOException { if (output instanceof Message) { ((Message) output).writeTo(stream); stream.flush(); } }
@Override public void inputWriteToStream(Object input, CodedOutputStream stream) throws IOException { if (input instanceof Message) { ((Message) input).writeTo(stream); stream.flush(); } }
@Override public void outputWriteToStream(Object output, CodedOutputStream stream) throws IOException { if (output instanceof Message) { ((Message) output).writeTo(stream); stream.flush(); } }
@Override public void inputWriteToStream(Object input, CodedOutputStream stream) throws IOException { if (input instanceof Message) { ((Message) input).writeTo(stream); stream.flush(); } }
public static void sendProtobuf(Message request, OutputStream os) throws IOException { os.write(Bytes.intToBytes(request.getSerializedSize())); request.writeTo(os); os.flush(); }
@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); } }
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_); }