Package com.google.protobuf

Examples of com.google.protobuf.CodedOutputStream


        int sampleCount = in.readRawVarint32();
        return new CounterRollup().withCount(value.longValue()).withRate(rate).withSampleCount(sampleCount);
    }
   
    private static void serializeSetRollup(SetRollup rollup, byte[] buf) throws IOException {
        CodedOutputStream out = CodedOutputStream.newInstance(buf);
        SetRollupSize.update(buf.length);
        out.writeRawByte(Constants.VERSION_1_SET_ROLLUP);
        out.writeRawVarint32(rollup.getCount());
        for (Integer i : rollup.getHashes()) {
            out.writeRawVarint32(i);
        }
    }
View Full Code Here


        }
        return rollup;
    }
   
    private static void serializeTimer(TimerRollup rollup, byte[] buf) throws IOException {
        CodedOutputStream out = CodedOutputStream.newInstance(buf);
        timerRollupSize.update(buf.length);
        out.writeRawByte(Constants.VERSION_1_TIMER);
       
        // sum, count, countps, avg, max, min, var
        out.writeRawVarint64(rollup.getSum());
        out.writeRawVarint64(rollup.getCount());
        out.writeDoubleNoTag(rollup.getRate());
        out.writeRawVarint32(rollup.getSampleCount());
        putRollupStat(rollup.getAverage(), out);
        putRollupStat(rollup.getMaxValue(), out);
        putRollupStat(rollup.getMinValue(), out);
        putRollupStat(rollup.getVariance(), out);
       
        // percentiles.
        Map<String, TimerRollup.Percentile> percentiles = rollup.getPercentiles();
        out.writeRawVarint32(percentiles.size());
        for (Map.Entry<String, TimerRollup.Percentile> entry : percentiles.entrySet()) {
            out.writeStringNoTag(entry.getKey());
            putUnversionedDoubleOrLong(entry.getValue().getMean(), out);
        }
    }
View Full Code Here

        return rollup;
    }
   
    private static void serializeGauge(GaugeRollup rollup, byte[] buf) throws IOException {
        rollupSize.update(buf.length);
        CodedOutputStream protobufOut = CodedOutputStream.newInstance(buf);
        serializeRollup(rollup, protobufOut);
        protobufOut.writeRawVarint64(rollup.getTimestamp());
        putUnversionedDoubleOrLong(rollup.getLatestNumericValue(), protobufOut);
    }
View Full Code Here

    public ContainerRequest filter(ContainerRequest request) {

        int contentLength = Integer.parseInt(request.getHeaderValue("Content-Length"));
        // make it big enough to hold the path.
        ByteBuffer buf = ByteBuffer.allocateDirect(contentLength + 128);
        CodedOutputStream out = CodedOutputStream.newInstance(new ByteBufferBackedOutputStream(buf));
        InputStream in = request.getEntityInputStream();
       
        try {
            if (in.available() > 0) {
                // copy the json into a byte array. this will get reused later.
                ByteArrayOutputStream jsonBuf = new ByteArrayOutputStream(contentLength);
                ReaderWriter.writeTo(in, jsonBuf);
                byte[] jsonBytes = jsonBuf.toByteArray();
               
                out.writeStringNoTag(request.getAbsolutePath().getPath());
                out.writeRawVarint32(Integer.parseInt(request.getHeaderValue("Content-Length")));
                out.writeRawBytes(jsonBytes);
                out.flush();
               
                buf.flip();
                // all of buf.remaining() is ready to be serialized.
                // todo: send to the durability object, add the receipt as a header.
               
View Full Code Here

    }
   
    // returns a mutable buffer. you can damage these bytes!
    public static byte[] toBytes(Collection<Metric> metrics) throws IOException {
        AccessibleByteArrayOutputStream out = new AccessibleByteArrayOutputStream(UDPMetricSerialization.computeBinarySize(metrics));
        CodedOutputStream codedOut = CodedOutputStream.newInstance(out);
        codedOut.writeRawVarint32(metrics.size());
        for (Metric metric : metrics) {
            write(metric, codedOut);
        }
        codedOut.flush();
        out.close();
        return out.getUnderlyingBuffer();
    }
View Full Code Here

   */
  private static byte[] serializeKey(Key key) throws StoreIOException {
    try {
      KeyData data = key.buildData().build();
      byte[] bytes = new byte[data.getSerializedSize()];
      CodedOutputStream cos = CodedOutputStream.newInstance(bytes);
      data.writeTo(cos);
      cos.checkNoSpaceLeft();
      return bytes;
    } catch (Exception ex) {
      throw new StoreIOException(
          StoreIOException.Reason.SERIALIZATION_ERROR, ex);
    }
View Full Code Here

      final byte[] inputData = readAllBytes(file);
      final ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData);
      final ByteString inputString = ByteString.copyFrom(inputData);
      final Message sampleMessage = defaultMessage.newBuilderForType().mergeFrom(inputString).build();
      FileOutputStream devNullTemp = null;
      CodedOutputStream reuseDevNullTemp = null;
      try {
        devNullTemp = new FileOutputStream("/dev/null");
        reuseDevNullTemp = CodedOutputStream.newInstance(devNullTemp);
      } catch (FileNotFoundException e) {
        // ignore: this is probably Windows, where /dev/null does not exist
      }
      final FileOutputStream devNull = devNullTemp;
      final CodedOutputStream reuseDevNull = reuseDevNullTemp;
      benchmark("Serialize to byte string", inputData.length, new Action() {
        public void execute() { sampleMessage.toByteString(); }
      });     
      benchmark("Serialize to byte array", inputData.length, new Action() {
        public void execute() { sampleMessage.toByteArray(); }
      });
      benchmark("Serialize to memory stream", inputData.length, new Action() {
        public void execute() throws IOException {
          sampleMessage.writeTo(new ByteArrayOutputStream());
        }
      });
      if (devNull != null) {
        benchmark("Serialize to /dev/null with FileOutputStream", inputData.length, new Action() {
          public void execute() throws IOException {
            sampleMessage.writeTo(devNull);
          }
        });
        benchmark("Serialize to /dev/null reusing FileOutputStream", inputData.length, new Action() {
          public void execute() throws IOException {
            sampleMessage.writeTo(reuseDevNull);
            reuseDevNull.flush()// force the write to the OutputStream
          }
        });
      }
      benchmark("Deserialize from byte string", inputData.length, new Action() {
        public void execute() throws IOException {
View Full Code Here

      writeBoolean(fieldName, value.booleanValue());
   }

   private void writePrimitiveCollection(Descriptors.FieldDescriptor fd, Collection<?> collection, Class elementClass) throws IOException {
      CodedOutputStream out = messageContext.out;
      int fieldNumber = fd.getNumber();
      Descriptors.FieldDescriptor.Type type = fd.getType();
      switch (type) {
         case DOUBLE:
            for (Object value : collection) {  //todo check (value != null && value.getClass() == elementClass)
               out.writeDouble(fieldNumber, (Double) value);
            }
            break;
         case FLOAT:
            for (Object value : collection) {
               out.writeFloat(fieldNumber, (Float) value);
            }
            break;
         case BOOL:
            for (Object value : collection) {
               out.writeBool(fieldNumber, (Boolean) value);
            }
            break;
         case STRING:
            for (Object value : collection) {
               out.writeString(fieldNumber, (String) value);
            }
            break;
         case BYTES:
            for (Object value : collection) {
               if (value instanceof byte[]) {
                  value = ByteString.copyFrom((byte[]) value);
               }
               out.writeBytes(fieldNumber, (ByteString) value);
            }
            break;
         case INT64:
            for (Object value : collection) {
               out.writeInt64(fieldNumber, (Long) value);
            }
            break;
         case UINT64:
            for (Object value : collection) {
               out.writeUInt64(fieldNumber, (Long) value);
            }
            break;
         case FIXED64:
            for (Object value : collection) {
               out.writeFixed64(fieldNumber, (Long) value);
            }
            break;
         case SFIXED64:
            for (Object value : collection) {
               out.writeSFixed64(fieldNumber, (Long) value);
            }
            break;
         case SINT64:
            for (Object value : collection) {
               out.writeSInt64(fieldNumber, (Long) value);
            }
            break;
         case INT32:
            for (Object value : collection) {
               out.writeInt32(fieldNumber, (Integer) value);
            }
            break;
         case FIXED32:
            for (Object value : collection) {
               out.writeFixed32(fieldNumber, (Integer) value);
            }
            break;
         case UINT32:
            for (Object value : collection) {
               out.writeUInt32(fieldNumber, (Integer) value);
            }
            break;
         case SFIXED32:
            for (Object value : collection) {
               out.writeSFixed32(fieldNumber, (Integer) value);
            }
            break;
         case SINT32:
            for (Object value : collection) {
               out.writeSInt32(fieldNumber, (Integer) value);
            }
            break;
         default:
            throw new IllegalStateException("Unexpected field type : " + type);
      }
View Full Code Here

   }

   private void writeMessage(String fieldName, Descriptors.FieldDescriptor fd, Object value, Class clazz) throws IOException {
      BaseMarshallerDelegate marshallerDelegate = ctx.getMarshallerDelegate(clazz);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      CodedOutputStream out = CodedOutputStream.newInstance(baos);
      marshallerDelegate.marshall(fieldName, fd, value, this, out);
      out.flush();
      messageContext.out.writeTag(fd.getNumber(), WireFormat.WIRETYPE_LENGTH_DELIMITED);
      messageContext.out.writeRawVarint32(baos.size());
      messageContext.out.writeRawBytes(baos.toByteArray());
   }
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);
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.