Package com.google.protobuf

Examples of com.google.protobuf.CodedOutputStream


        int length = body.readableBytes();
        ChannelBuffer header =
            channel.getConfig().getBufferFactory().getBuffer(
                    body.order(),
                    CodedOutputStream.computeRawVarint32Size(length));
        CodedOutputStream codedOutputStream = CodedOutputStream
                .newInstance(new ChannelBufferOutputStream(header));
        codedOutputStream.writeRawVarint32(length);
        codedOutputStream.flush();
        return wrappedBuffer(header, body);
    }
View Full Code Here


        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);
        cos.flush(); // need to flush so that dbody goes after if cos is caching.
       
        CompositeByteBuf cbb = new CompositeByteBuf(buf.alloc(), true, 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());
View Full Code Here

      if (MessageLite.class.isAssignableFrom(clazz)) {
         messageContext.out.writeMessage(fd.getNumber(), (MessageLite) value);
      } else {
         BaseMarshaller marshaller = ctx.getMarshaller(clazz);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();      //todo here we should use a better buffer allocation strategy
         CodedOutputStream out = CodedOutputStream.newInstance(baos);
         enterContext(fd.getMessageType(), out);
         marshall(value, marshaller, out);
         out.flush();
         exitContext();
         messageContext.out.writeTag(fd.getNumber(), WireFormat.WIRETYPE_LENGTH_DELIMITED);
         messageContext.out.writeRawVarint32(baos.size());
         messageContext.out.writeRawBytes(baos.toByteArray());
      }
View Full Code Here

   private static final int wrappedMessageBytes = 17;
   private static final int wrappedEnum = 18;

   public static byte[] toWrappedByteArray(SerializationContext ctx, Object t) throws IOException {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      CodedOutputStream out = CodedOutputStream.newInstance(baos);
      toWrappedByteArray(ctx, out, t);
      return baos.toByteArray();
   }
View Full Code Here

        payload.readerIndex(offset);
        // The first int is the size of the message, excluding the 4 bytes
        // needed for the size itself, hence the `-4'.
        payload.setInt(offset, payload.readableBytes() - 4); // 4 bytes
        try {
          final CodedOutputStream output =
            CodedOutputStream.newInstance(payload.array(), 4 + offset,
                                          1 + pblen);
          output.writeRawByte(pblen)// varint but always on 1 byte here.
          header.writeTo(output);
          output.checkNoSpaceLeft();
        } catch (IOException e) {
          throw new RuntimeException("Should never happen", e);
        }
      } else if (server_version >= SERVER_VERSION_092_OR_ABOVE) {
        // The first int is the size of the message, excluding the 4 bytes
View Full Code Here

    final int pblen = pb.getSerializedSize();
    final byte[] buf = new byte[HBASE.length + 4 + pblen];
    final ChannelBuffer header = commonHeader(buf, HBASE);
    header.writeInt(pblen)// 4 bytes
    try {
      final CodedOutputStream output =
        CodedOutputStream.newInstance(buf, HBASE.length + 4, pblen);
      pb.writeTo(output);
      output.checkNoSpaceLeft();
    } catch (IOException e) {
      throw new RuntimeException("Should never happen", e);
    }
    // We wrote to the underlying buffer but Netty didn't see the writes,
    // so move the write index forward.
View Full Code Here

                                             final AbstractMessageLite pb) {
    final int pblen = pb.getSerializedSize();
    final int vlen = CodedOutputStream.computeRawVarint32Size(pblen);
    final byte[] buf = new byte[4 + 19 + method.length + vlen + pblen];
    try {
      final CodedOutputStream out = CodedOutputStream.newInstance(buf, 4 + 19 + method.length,
                                                                  vlen + pblen);
      out.writeRawVarint32(pblen);
      pb.writeTo(out);
      out.checkNoSpaceLeft();
    } catch (IOException e) {
      throw new RuntimeException("Should never happen", e);
    }
    return ChannelBuffers.wrappedBuffer(buf);
  }
View Full Code Here

            ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
        int bodyLen = msg.readableBytes();
        int headerLen = CodedOutputStream.computeRawVarint32Size(bodyLen);
        out.ensureWritable(headerLen + bodyLen);

        CodedOutputStream headerOut =
                CodedOutputStream.newInstance(new ByteBufOutputStream(out));
        headerOut.writeRawVarint32(bodyLen);
        headerOut.flush();

        out.writeBytes(msg, msg.readerIndex(), bodyLen);
    }
View Full Code Here

      if (t == null) {
         throw new IllegalArgumentException("Object to marshall cannot be null");
      }

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      CodedOutputStream out = CodedOutputStream.newInstance(baos);

      if (t instanceof String) {
         out.writeString(wrappedInt64, (String) t);
      } else if (t instanceof Long) {
         out.writeInt64(wrappedInt64, (Long) t);
      } else if (t instanceof Integer) {
         out.writeInt32(wrappedInt32, (Integer) t);
      } else if (t instanceof Double) {
         out.writeDouble(wrappedDouble, (Double) t);
      } else if (t instanceof Float) {
         out.writeFloat(wrappedFloat, (Float) t);
      } else if (t instanceof Boolean) {
         out.writeBool(wrappedBool, (Boolean) t);
      } else if (t instanceof byte[]) {
         byte[] bytes = (byte[]) t;
         out.writeTag(wrappedBytes, WireFormat.WIRETYPE_LENGTH_DELIMITED);
         out.writeRawVarint32(bytes.length);
         out.writeRawBytes(bytes);
      } else if (t instanceof Enum) {
         // use an enum encoder
         EnumEncoder enumEncoder = ctx.getEnumEncoder((Class<Enum>) t.getClass());
         out.writeString(wrappedDescriptorFullName, enumEncoder.getFullName());
         out.writeEnum(wrappedEnum, enumEncoder.encode((Enum) t));
      } else {
         // this is either an unknown primitive type or a message type
         // try to use a message marshaller
         MessageMarshaller marshaller = ctx.getMarshaller(t.getClass());
         out.writeString(wrappedDescriptorFullName, marshaller.getFullName());
         out.writeTag(wrappedMessageBytes, WireFormat.WIRETYPE_LENGTH_DELIMITED);

         ByteArrayOutputStream baos2 = new ByteArrayOutputStream();      //todo here we should use a better buffer allocation strategy
         CodedOutputStream out2 = CodedOutputStream.newInstance(baos2);
         ProtobufWriterImpl writer = new ProtobufWriterImpl(ctx);
         writer.write(out2, t);
         out.writeRawVarint32(baos2.size());
         out.writeRawBytes(baos2.toByteArray());
      }
View Full Code Here

    }
  }
 
  private void doVarIntTest(int value) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CodedOutputStream cout = CodedOutputStream.newInstance(baos);
    cout.writeRawVarint32(value);
    cout.flush();

    DataInputStream dis = new DataInputStream(
        new ByteArrayInputStream(baos.toByteArray()));
    assertEquals(value, ProtoUtil.readRawVarint32(dis));
  }
View Full Code Here

TOP

Related Classes of com.google.protobuf.CodedOutputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.