Search in sources :

Example 6 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project grpc-java by grpc.

the class ProtoInputStream method read.

@Override
public int read(byte[] b, int off, int len) throws IOException {
    if (message != null) {
        int size = message.getSerializedSize();
        if (size == 0) {
            message = null;
            partial = null;
            return -1;
        }
        if (len >= size) {
            // This is the only case that is zero-copy.
            CodedOutputStream stream = CodedOutputStream.newInstance(b, off, size);
            message.writeTo(stream);
            stream.flush();
            stream.checkNoSpaceLeft();
            message = null;
            partial = null;
            return size;
        }
        partial = new ByteArrayInputStream(message.toByteArray());
        message = null;
    }
    if (partial != null) {
        return partial.read(b, off, len);
    }
    return -1;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream)

Example 7 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project voldemort by voldemort.

the class ProtoUtils method writeMessage.

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();
}
Also used : CodedOutputStream(com.google.protobuf.CodedOutputStream)

Example 8 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project hadoop by apache.

the class BlockListAsLongs method writeTo.

public void writeTo(OutputStream os) throws IOException {
    CodedOutputStream cos = CodedOutputStream.newInstance(os);
    cos.writeInt32(1, getNumberOfBlocks());
    cos.writeBytes(2, getBlocksBuffer());
    cos.flush();
}
Also used : CodedOutputStream(com.google.protobuf.CodedOutputStream)

Example 9 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project hadoop by apache.

the class Server method setupResponseForProtobuf.

// writing to a pre-allocated array is the most efficient way to construct
// a protobuf response.
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;
}
Also used : Message(com.google.protobuf.Message) CodedOutputStream(com.google.protobuf.CodedOutputStream)

Example 10 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project drill by apache.

the class RpcEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, OutboundRpcMessage msg, List<Object> out) throws Exception {
    if (RpcConstants.EXTRA_DEBUGGING) {
        logger.debug("Rpc Encoder called with msg {}", msg);
    }
    if (!ctx.channel().isOpen()) {
        //output.add(ctx.alloc().buffer(0));
        logger.debug("Channel closed, skipping encode.");
        msg.release();
        return;
    }
    try {
        if (RpcConstants.EXTRA_DEBUGGING) {
            logger.debug("Encoding outbound message {}", msg);
        }
        // first we build the RpcHeader
        RpcHeader header = //
        RpcHeader.newBuilder().setMode(//
        msg.mode).setCoordinationId(//
        msg.coordinationId).setRpcType(msg.rpcType).build();
        // figure out the full length
        int headerLength = header.getSerializedSize();
        int protoBodyLength = msg.pBody.getSerializedSize();
        int rawBodyLength = msg.getRawBodySize();
        //
        int fullLength = //
        HEADER_TAG_LENGTH + getRawVarintSize(headerLength) + headerLength + PROTOBUF_BODY_TAG_LENGTH + getRawVarintSize(protoBodyLength) + //
        protoBodyLength;
        if (rawBodyLength > 0) {
            fullLength += (RAW_BODY_TAG_LENGTH + getRawVarintSize(rawBodyLength) + rawBodyLength);
        }
        ByteBuf buf = ctx.alloc().buffer();
        OutputStream os = new ByteBufOutputStream(buf);
        CodedOutputStream cos = CodedOutputStream.newInstance(os);
        // write full length first (this is length delimited stream).
        cos.writeRawVarint32(fullLength);
        // write header
        cos.writeRawVarint32(HEADER_TAG);
        cos.writeRawVarint32(headerLength);
        header.writeTo(cos);
        // write protobuf body length and body
        cos.writeRawVarint32(PROTOBUF_BODY_TAG);
        cos.writeRawVarint32(protoBodyLength);
        msg.pBody.writeTo(cos);
        // if exists, write data body and tag.
        if (msg.getRawBodySize() > 0) {
            if (RpcConstants.EXTRA_DEBUGGING) {
                logger.debug("Writing raw body of size {}", msg.getRawBodySize());
            }
            cos.writeRawVarint32(RAW_BODY_TAG);
            cos.writeRawVarint32(rawBodyLength);
            // need to flush so that dbody goes after if cos is caching.
            cos.flush();
            final CompositeByteBuf cbb = ctx.alloc().compositeBuffer(msg.dBodies.length + 1);
            cbb.addComponent(buf);
            int bufLength = buf.readableBytes();
            for (ByteBuf b : msg.dBodies) {
                cbb.addComponent(b);
                bufLength += b.readableBytes();
            }
            cbb.writerIndex(bufLength);
            out.add(cbb);
        } else {
            cos.flush();
            out.add(buf);
        }
        if (RpcConstants.SOME_DEBUGGING) {
            logger.debug("Wrote message length {}:{} bytes (head:body).  Message: " + msg, getRawVarintSize(fullLength), fullLength);
        }
        if (RpcConstants.EXTRA_DEBUGGING) {
            logger.debug("Sent message.  Ending writer index was {}.", buf.writerIndex());
        }
    } finally {
    // make sure to release Rpc Messages underlying byte buffers.
    //msg.release();
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) RpcHeader(org.apache.drill.exec.proto.GeneralRPCProtos.RpcHeader) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CodedOutputStream (com.google.protobuf.CodedOutputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 OrmException (com.google.gwtorm.server.OrmException)1 Message (com.google.protobuf.Message)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 DataInputStream (java.io.DataInputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Charset (java.nio.charset.Charset)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 RpcHeader (org.apache.drill.exec.proto.GeneralRPCProtos.RpcHeader)1 JobLogLine (org.platformlayer.jobs.model.JobLogLine)1 JobDataProtobuf (org.platformlayer.ops.jobstore.protobuf.JobDataProtobuf)1 MediaType (org.springframework.http.MediaType)1