Package com.google.protobuf

Examples of com.google.protobuf.CodedOutputStream


    if (functionInformationMapOutputPath != null) {
      if (compiler.getFunctionalInformationMap() != null) {
        OutputStream file =
            filenameToOutputStream(functionInformationMapOutputPath);
        CodedOutputStream outputStream = CodedOutputStream.newInstance(file);
        compiler.getFunctionalInformationMap().writeTo(outputStream);
        outputStream.flush();
        file.flush();
        file.close();
      }
    }
  }
View Full Code Here


    return jobLog;
  }

  protected void serialize(SimpleJobLogger logger, OutputStream os) throws IOException {
    GZIPOutputStream gzip = new GZIPOutputStream(os);
    CodedOutputStream out = CodedOutputStream.newInstance(gzip);

    Iterable<JobLogLine> lines = logger.getLogEntries();

    JobDataProtobuf.JobLogLine.Builder protobuf = JobDataProtobuf.JobLogLine.newBuilder();
    for (JobLogLine line : lines) {
      protobuf.setLevel(line.level);
      if (line.message != null) {
        protobuf.setMessage(line.message);
      } else {
        protobuf.clearMessage();
      }
      protobuf.setTimestamp(line.timestamp);
      if (line.type != null) {
        protobuf.setType(line.type);
      } else {
        protobuf.clearType();
      }
      if (line.exception != null) {
        mapToProtobuf(line.exception, protobuf.getExceptionBuilder());
      } else {
        protobuf.clearException();
      }

      JobDataProtobuf.JobLogLine message = protobuf.build();
      int size = message.getSerializedSize();
      out.writeRawVarint32(size);
      message.writeTo(out);
    }

    out.flush();
    gzip.finish();
  }
View Full Code Here

         * 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();
    }
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.getName(), 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

    protected String fileName = "target/performance.openwire";
    protected OpenWire.Destination destination = OpenWire.Destination.newBuilder().setName("FOO.BAR").setType(OpenWire.Destination.DestinationType.QUEUE).build();

    public void testPerformance() throws Exception {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
        CodedOutputStream cout = CodedOutputStream.newInstance(out);

        StopWatch watch = createStopWatch("writer");
        for (long i = 0; i < messageCount; i++) {
            watch.start();
            OpenWire.Message.Builder builder = OpenWire.Message.newBuilder()
                    .setDestination(destination)
                    .setPersistent(true)
                    .setCorrelationId("ABCD");

            if (useProducerId) {
                int producerCounter = (int) i;
                builder = builder.setProducerId(1234)
                        .setProducerCounter(producerCounter);
            }

            OpenWire.Message message = builder.build();

            if (verbose) {
                System.out.println("Writing message: " + i + " = " + message);
            }
            int size = message.getSerializedSize();

            cout.writeRawVarint32(size);
            message.writeTo(cout);

            watch.stop();
        }
        cout.flush();
        out.close();

        // now lets try read them!
        StopWatch watch2 = createStopWatch("reader");
        InputStream in = new BufferedInputStream(new FileInputStream(fileName));
View Full Code Here

    protected String fileName = "target/marshall.openwire";


    public void testMarshalling() throws Exception {
        FileOutputStream out = new FileOutputStream(fileName);
        CodedOutputStream cout = CodedOutputStream.newInstance(out);
        OpenWire.Destination destination = OpenWire.Destination.newBuilder().setName("FOO.BAR").setType(OpenWire.Destination.DestinationType.QUEUE).build();

        for (int i = 0; i < messageCount; i++) {
            OpenWire.Message message = OpenWire.Message.newBuilder()
                    .setDestination(destination)
                    .setPersistent(true)
                    .setProducerId(1234)
                    .setProducerCounter(i)
                    .build();
            //.setType("type:" + i)

            System.out.println("Writing message: " + i + " = " + message);
            int size = message.getSerializedSize();
            cout.writeRawVarint32(size);
            message.writeTo(cout);
            cout.flush();
        }
        out.close();

        // now lets try read them!
        FileInputStream in = new FileInputStream(fileName);
View Full Code Here

        RpcMessage message = (RpcMessage) obj;
        int size = message.getSerializedSize();
        ChannelBuffer buffer = new BigEndianHeapChannelBuffer(4 + size + 4);
        buffer.writeBytes("RPC0".getBytes());
        int writerIndex = buffer.writerIndex();
        CodedOutputStream output = CodedOutputStream.newInstance(
                buffer.array(), buffer.writerIndex(), buffer.writableBytes() - 4);
        message.writeTo(output);
        output.checkNoSpaceLeft();

        buffer.writerIndex(writerIndex + size);
        Adler32 checksum = new Adler32();
        checksum.update(buffer.array(), buffer.arrayOffset(), buffer.readableBytes());
        buffer.writeInt((int) checksum.getValue());
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

    if (functionInformationMapOutputPath != null) {
      if (compiler.getFunctionalInformationMap() != null) {
        OutputStream file =
            filenameToOutputStream(functionInformationMapOutputPath);
        CodedOutputStream outputStream = CodedOutputStream.newInstance(file);
        compiler.getFunctionalInformationMap().writeTo(outputStream);
        outputStream.flush();
        file.flush();
        file.close();
      }
    }
  }
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.