Package com.google.protobuf

Examples of com.google.protobuf.CodedOutputStream


    if (m == null) return null;
    int serializedSize = m.getSerializedSize();
    int vintSize = CodedOutputStream.computeRawVarint32Size(serializedSize);
    byte [] buffer = new byte[serializedSize + vintSize];
    // Passing in a byte array saves COS creating a buffer which it does when using streams.
    CodedOutputStream cos = CodedOutputStream.newInstance(buffer);
    // This will write out the vint preamble and the message serialized.
    cos.writeMessageNoTag(m);
    cos.flush();
    cos.checkNoSpaceLeft();
    return ByteBuffer.wrap(buffer);
  }
View Full Code Here


        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

    }

    private static byte[] makeConnectionMessage(Counter.Value value)
            throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        CodedOutputStream codedOutput = CodedOutputStream.newInstance(out);
        codedOutput.writeRawLittleEndian32(value.getSerializedSize());
        value.writeTo(codedOutput);
        codedOutput.flush();

        byte[] all = out.toByteArray();
        return all;
    }
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

    }
  }

  @Override
  public int encode(PositionedByteRange dst, CellProtos.Cell val) {
    CodedOutputStream os = outputStreamFromByteRange(dst);
    try {
      int before = os.spaceLeft(), after, written;
      val.writeTo(os);
      after = os.spaceLeft();
      written = before - after;
      dst.setPosition(dst.getPosition() + written);
      return written;
    } catch (IOException e) {
      throw new RuntimeException("Error while encoding type.", e);
View Full Code Here

        return new HistogramRollup(bins);
    }

    private void serializeBins(Collection<Bin<SimpleTarget>> bins, byte[] buf) throws IOException {
        CodedOutputStream protobufOut = CodedOutputStream.newInstance(buf);

        protobufOut.writeRawByte(Constants.VERSION_1_HISTOGRAM);

        for (Bin<SimpleTarget> bin : bins) {
            protobufOut.writeRawVarint64((long) bin.getCount());
            protobufOut.writeDoubleNoTag(bin.getMean());
        }
    }
View Full Code Here

    @Override
    public ByteBuffer toByteBuffer(String o) {
        try {
            byte[] buf = new byte[computeBufLength(o)];
            CodedOutputStream out = CodedOutputStream.newInstance(buf);
            writeToOutputStream(o, out);
            return ByteBuffer.wrap(buf);
        } catch (IOException e) {
            throw new RuntimeException("Serialization problems", e);
        }
View Full Code Here

            return (AbstractSerializer<T>)fullInstance;  
    }

    private static void serializeRollup(BasicRollup basicRollup, byte[] buf) throws IOException {
        rollupSize.update(buf.length);
        CodedOutputStream protobufOut = CodedOutputStream.newInstance(buf);
        serializeRollup(basicRollup, protobufOut);
    }
View Full Code Here

        else
            return in.readRawVarint64();
    }

    private static void serializeFullResMetric(Object o, byte[] buf) throws IOException {
        CodedOutputStream protobufOut = CodedOutputStream.newInstance(buf);
        byte type = typeOf(o);
        fullResSize.update(sizeOf(o, type));
        protobufOut.writeRawByte(Constants.VERSION_1_FULL_RES);

        switch (type) {
            case Constants.B_I32:
                protobufOut.writeRawByte(type);
                protobufOut.writeRawVarint32((Integer) o);
                break;
            case Constants.B_I64:
                protobufOut.writeRawByte(type);
                protobufOut.writeRawVarint64((Long) o);
                break;
            case Constants.B_DOUBLE:
                protobufOut.writeRawByte(type);
                protobufOut.writeDoubleNoTag((Double) o);
                break;
            case Type.B_FLOAT_AS_DOUBLE:
                protobufOut.writeRawByte(Constants.B_DOUBLE);
                protobufOut.writeDoubleNoTag(((Float) o).doubleValue());
                break;
            default:
                throw new SerializationException(String.format("Cannot serialize %s", o.getClass().getName()));
        }
    }
View Full Code Here

        }
        return sz;
    }
   
    private static void serializeCounterRollup(CounterRollup rollup, byte[] buf) throws IOException {
        CodedOutputStream out = CodedOutputStream.newInstance(buf);
        CounterRollupSize.update(buf.length);
        out.writeRawByte(Constants.VERSION_1_COUNTER_ROLLUP);
        putUnversionedDoubleOrLong(rollup.getCount(), out);
        out.writeDoubleNoTag(rollup.getRate());
        out.writeRawVarint32(rollup.getSampleCount());
    }
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.