Java Code Examples for com.google.protobuf.CodedOutputStream#flush()

The following examples show how to use com.google.protobuf.CodedOutputStream#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StateTest.java    From zetasketch with Apache License 2.0 7 votes vote down vote up
@Test
public void parseUnknownField() throws IOException {
  // Create an aggregator state proto with an unknown field in the middle.
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  CodedOutputStream coded = CodedOutputStream.newInstance(stream);
  coded.writeInt32(AggregatorStateProto.NUM_VALUES_FIELD_NUMBER, 42);
  coded.writeString(999, "foobar");
  coded.writeInt32(AggregatorStateProto.ENCODING_VERSION_FIELD_NUMBER, 43);
  coded.flush();

  // Check that we can parse the proto, despite the unknown field.
  State state = new State();
  state.parse(CodedInputStream.newInstance(stream.toByteArray()));

  // Check that the fields before and after the unknown fields were correctly read.
  assertEquals(42, state.numValues);
  assertEquals(43, state.encodingVersion);
}
 
Example 2
Source File: CpuProfiler.java    From bazel with Apache License 2.0 6 votes vote down vote up
synchronized void writeEvent(int ticks, ImmutableList<Debug.Frame> stack) {
  if (this.error == null) {
    try {
      ByteArrayOutputStream sample = new ByteArrayOutputStream();
      CodedOutputStream sampleEnc = CodedOutputStream.newInstance(sample);
      sampleEnc.writeInt64(SAMPLE_VALUE, ticks * period.toNanos() / 1000L);
      for (Debug.Frame fr : stack.reverse()) {
        sampleEnc.writeUInt64(SAMPLE_LOCATION_ID, getLocationID(fr));
      }
      sampleEnc.flush();
      enc.writeByteArray(PROFILE_SAMPLE, sample.toByteArray());
    } catch (IOException ex) {
      this.error = ex;
    }
  }
}
 
Example 3
Source File: TargetPatternSequenceCodecTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testPatternsOrderSignificant() throws Exception {
  SerializationContext writeContext = new SerializationContext(ImmutableMap.of());

  ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
  CodedOutputStream codedOut = CodedOutputStream.newInstance(outputBytes);
  writeContext.serialize(
      TargetPatternSequence.create(ImmutableList.of("uno", "dos"), "tres"), codedOut);
  codedOut.flush();
  byte[] serialized1 = outputBytes.toByteArray();
  assertThat(serialized1).asList().isNotEmpty();
  outputBytes.reset();
  codedOut = CodedOutputStream.newInstance(outputBytes);
  writeContext.serialize(
      TargetPatternSequence.create(ImmutableList.of("dos", "uno"), "tres"), codedOut);
  codedOut.flush();
  byte[] serialized2 = outputBytes.toByteArray();
  assertThat(serialized2).asList().isNotEmpty();
  assertThat(serialized1).isNotEqualTo(serialized2);
}
 
Example 4
Source File: ProtobufVarint32LengthFieldPrepender.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void encode(
        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), headerLen);
    headerOut.writeRawVarint32(bodyLen);
    headerOut.flush();

    out.writeBytes(msg, msg.readerIndex(), bodyLen);
}
 
Example 5
Source File: ObjectCodecs.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static ByteString serializeToByteString(Object subject, SerializeCall wrapped)
    throws SerializationException {
  ByteString.Output resultOut = ByteString.newOutput();
  CodedOutputStream codedOut = CodedOutputStream.newInstance(resultOut);
  wrapped.serialize(subject, codedOut);
  try {
    codedOut.flush();
    return resultOut.toByteString();
  } catch (IOException e) {
    throw new SerializationException("Failed to serialize " + subject, e);
  }
}
 
Example 6
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * write list to {@link CodedOutputStream} object.
 *
 * @param out target output stream to write
 * @param order field order
 * @param type field type
 * @param list target list object to be serialized
 * @param packed the packed
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void writeToList(CodedOutputStream out, int order, FieldType type, Collection list, boolean packed)
        throws IOException {
    if (list == null || list.isEmpty()) {
        return;
    }
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CodedOutputStream newInstance = CodedOutputStream.newInstance(baos, 0);
    for (Object object : list) {
        if (object == null) {
            throw new NullPointerException("List can not include Null value.");
        }
        writeObject(newInstance, order, type, object, true, !packed);
    }
    newInstance.flush();
    byte[] byteArray = baos.toByteArray();
    

    if (packed) {
        out.writeUInt32NoTag(makeTag(order, WireFormat.WIRETYPE_LENGTH_DELIMITED));
        out.writeUInt32NoTag(byteArray.length); 
    }

    out.write(byteArray, 0, byteArray.length);

}
 
Example 7
Source File: StringTypeClassTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeDecode() {

    StringMessage message = StringMessage.newBuilder().setList("你好!").build();

    byte[] byteArray = message.toByteArray();
    System.out.println(Arrays.toString(byteArray));

    StringTypePOJOClass pojo = new StringTypePOJOClass();
    pojo.setStr("你好!");

    Codec<StringTypePOJOClass> codec = ProtobufProxy.create(StringTypePOJOClass.class, false);
    try {
        byte[] bb = codec.encode(pojo);
        System.out.println(Arrays.toString(bb));
        Assert.assertArrayEquals(byteArray, bb);

        StringTypePOJOClass newPojo = codec.decode(bb);
        System.out.println(newPojo.getStr());
        Assert.assertEquals("你好!", newPojo.getStr());

        OutputStream os = new ByteArrayOutputStream();
        CodedOutputStream newInstance = CodedOutputStream.newInstance(os);
        codec.writeTo(newPojo, newInstance);
        newInstance.flush();
        System.out.println(os.toString());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
 
Example 8
Source File: HBaseCommitTable.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
private static byte[] encodeCommitTimestamp(long startTimestamp, long commitTimestamp) throws IOException {
    assert (startTimestamp < commitTimestamp);
    long diff = commitTimestamp - startTimestamp;
    byte[] bytes = new byte[CodedOutputStream.computeInt64SizeNoTag(diff)];
    CodedOutputStream cos = CodedOutputStream.newInstance(bytes);
    cos.writeInt64NoTag(diff);
    cos.flush();
    return bytes;

}
 
Example 9
Source File: BuildOptions.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context,
    OptionsDiffForReconstruction diff,
    CodedOutputStream codedOut)
    throws SerializationException, IOException {
  OptionsDiffCache cache = context.getDependency(OptionsDiffCache.class);
  ByteString bytes = cache.getBytesFromOptionsDiff(diff);
  if (bytes == null) {
    context = context.getNewNonMemoizingContext();
    ByteString.Output byteStringOut = ByteString.newOutput();
    CodedOutputStream bytesOut = CodedOutputStream.newInstance(byteStringOut);
    context.serialize(diff.differingOptions, bytesOut);
    context.serialize(diff.extraFirstFragmentClasses, bytesOut);
    context.serialize(diff.extraSecondFragments, bytesOut);
    bytesOut.writeByteArrayNoTag(diff.baseFingerprint);
    context.serialize(diff.checksum, bytesOut);
    context.serialize(diff.differingStarlarkOptions, bytesOut);
    context.serialize(diff.extraFirstStarlarkOptions, bytesOut);
    context.serialize(diff.extraSecondStarlarkOptions, bytesOut);
    bytesOut.flush();
    byteStringOut.flush();
    int optionsDiffSize = byteStringOut.size();
    bytes = byteStringOut.toByteString();
    cache.putBytesFromOptionsDiff(diff, bytes);
    logger.atFine().log(
        "Serialized OptionsDiffForReconstruction %s. Diff took %d bytes.",
        diff, optionsDiffSize);
  }
  codedOut.writeBytesNoTag(bytes);
}
 
Example 10
Source File: TestProtoUtil.java    From hadoop with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 11
Source File: WalletProtobufSerializer.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Formats the given wallet (transactions and keys) to the given output stream in protocol buffer format.<p>
 *
 * Equivalent to <tt>walletToProto(wallet).writeTo(output);</tt>
 */
public void writeWallet(Wallet wallet, OutputStream output) throws IOException {
    Protos.Wallet walletProto = walletToProto(wallet);
    final CodedOutputStream codedOutput = CodedOutputStream.newInstance(output, this.walletWriteBufferSize);
    walletProto.writeTo(codedOutput);
    codedOutput.flush();
}
 
Example 12
Source File: CpuProfiler.java    From bazel with Apache License 2.0 5 votes vote down vote up
PprofWriter(OutputStream out, Duration period) {
  this.period = period;
  this.startNano = System.nanoTime();

  try {
    this.gz = new GZIPOutputStream(out);
    this.enc = CodedOutputStream.newInstance(gz);
    getStringID(""); // entry 0 is always ""

    // dimension and unit
    ByteArrayOutputStream unit = new ByteArrayOutputStream();
    CodedOutputStream unitEnc = CodedOutputStream.newInstance(unit);
    unitEnc.writeInt64(VALUETYPE_TYPE, getStringID("CPU"));
    unitEnc.writeInt64(VALUETYPE_UNIT, getStringID("microseconds"));
    unitEnc.flush();

    // informational fields of Profile
    enc.writeByteArray(PROFILE_SAMPLE_TYPE, unit.toByteArray());
    // magnitude of sampling period:
    enc.writeInt64(PROFILE_PERIOD, period.toNanos() / 1000L);
    // dimension and unit of period:
    enc.writeByteArray(PROFILE_PERIOD_TYPE, unit.toByteArray());
    // start (real) time of profile:
    enc.writeInt64(PROFILE_TIME_NANOS, System.currentTimeMillis() * 1000000L);
  } catch (IOException ex) {
    this.error = ex;
  }
}
 
Example 13
Source File: CpuProfiler.java    From bazel with Apache License 2.0 5 votes vote down vote up
private long getFunctionID(StarlarkCallable fn, long addr) throws IOException {
  Long id = functionIDs.get(addr);
  if (id == null) {
    id = addr;

    Location loc = fn.getLocation();
    String filename = loc.file(); // TODO(adonovan): make WORKSPACE-relative
    String name = fn.getName();
    if (name.equals("<toplevel>")) {
      name = filename;
    }

    long nameID = getStringID(name);

    ByteArrayOutputStream fun = new ByteArrayOutputStream();
    CodedOutputStream funEnc = CodedOutputStream.newInstance(fun);
    funEnc.writeUInt64(FUNCTION_ID, id);
    funEnc.writeInt64(FUNCTION_NAME, nameID);
    funEnc.writeInt64(FUNCTION_SYSTEM_NAME, nameID);
    funEnc.writeInt64(FUNCTION_FILENAME, getStringID(filename));
    funEnc.writeInt64(FUNCTION_START_LINE, (long) loc.line());
    funEnc.flush();
    enc.writeByteArray(PROFILE_FUNCTION, fun.toByteArray());

    functionIDs.put(addr, id);
  }
  return id;
}
 
Example 14
Source File: TestUtils.java    From bazel with Apache License 2.0 5 votes vote down vote up
public static <T> ByteString toBytes(SerializationContext serializationContext, T value)
    throws IOException, SerializationException {
  ByteString.Output output = ByteString.newOutput();
  CodedOutputStream codedOut = CodedOutputStream.newInstance(output);
  serializationContext.serialize(value, codedOut);
  codedOut.flush();
  return output.toByteString();
}
 
Example 15
Source File: EncodeUtils.java    From java-sdk with Apache License 2.0 5 votes vote down vote up
public static byte[] aminoWrap(byte[] raw, byte[] typePrefix, boolean isPrefixLength) throws IOException {
    int totalLen = raw.length + typePrefix.length;
    if (isPrefixLength)
        totalLen += CodedOutputStream.computeUInt64SizeNoTag(totalLen);

    byte[] msg = new byte[totalLen];
    CodedOutputStream cos = CodedOutputStream.newInstance(msg);
    if (isPrefixLength)
        cos.writeUInt64NoTag(raw.length + typePrefix.length);
    cos.write(typePrefix, 0, typePrefix.length);
    cos.write(raw, 0, raw.length);
    cos.flush();
    return msg;
}
 
Example 16
Source File: WalletProtobufSerializer.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Formats the given wallet (transactions and keys) to the given output stream in protocol buffer format.<p>
 * <p>
 * Equivalent to <tt>walletToProto(wallet).writeTo(output);</tt>
 */
public void writeWallet(Wallet wallet, OutputStream output) throws IOException {
    Protos.Wallet walletProto = walletToProto(wallet);
    final CodedOutputStream codedOutput = CodedOutputStream.newInstance(output, this.walletWriteBufferSize);
    walletProto.writeTo(codedOutput);
    codedOutput.flush();
}
 
Example 17
Source File: ProtobufHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = getDefaultContentType(message);
		Assert.state(contentType != null, "No content type");
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	if (PROTOBUF.isCompatibleWith(contentType)) {
		setProtoHeader(outputMessage, message);
		CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
		message.writeTo(codedOutputStream);
		codedOutputStream.flush();
	}
	else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
		TextFormat.print(message, outputStreamWriter);
		outputStreamWriter.flush();
		outputMessage.getBody().flush();
	}
	else if (this.protobufFormatSupport != null) {
		this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
		outputMessage.getBody().flush();
	}
}
 
Example 18
Source File: TransactionUtil.java    From snowblossom with Apache License 2.0 4 votes vote down vote up
public static ArrayList<ByteString> extractWireFormatTxOut(Transaction tx)
  throws ValidationException
{
  try
  {
    CodedInputStream code_in = CodedInputStream.newInstance(tx.getInnerData().toByteArray());

    ArrayList<ByteString> lst = new ArrayList<>();

    while(true)
    {
      int tag = code_in.readTag();
      if (tag == 0) break;
      
      // The least signficiate 3 bits are the proto field type
      // so shift to get out field number, which is 5 for TranasctionOutput
      if (tag >> 3 == 5)
      {
        ByteArrayOutputStream b_out = new ByteArrayOutputStream();
        CodedOutputStream c_out = CodedOutputStream.newInstance(b_out);
        code_in.skipField(tag, c_out);
        c_out.flush();

        ByteString bs = ByteString.copyFrom(b_out.toByteArray());

        // So funny story...when you get an inner message like this as opposed to just serializing
        // the object, protobuf puts a tag and size on the coded input stream.  So we need to figure
        // out how many bytes that is an trim it off.
        CodedInputStream read_again = CodedInputStream.newInstance(bs.toByteArray());
        // Expected tag
        int tag2 = read_again.readTag();
        // Size of element
        int size = read_again.readInt32();

        // All we really care is how many bytes those two take.  For shorter messages
        // it will be 2, but could be higher if protobuf needs more bytes to encode the size
        int offset = read_again.getTotalBytesRead();

        bs = bs.substring(offset);
        lst.add(bs);
      }
      else
      {
        code_in.skipField(tag);
      }

    }
    return lst;
  }
  catch(java.io.IOException e)
  {
    throw new ValidationException(e);
  }
}
 
Example 19
Source File: ProtoBufSerialization.java    From eagle with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CodedOutputStream output = CodedOutputStream.newInstance(baos);
    output.writeBoolNoTag(obj == null);
    if (obj == null) {
        output.flush();
        return baos.toByteArray();
    }

    Class<?> clazz = obj.getClass();
    if (clazz == int.class || clazz == Integer.class) {
        output.writeSInt32NoTag((Integer) obj);
    } else if (clazz == long.class || clazz == Long.class) {
        output.writeSInt64NoTag((Long) obj);
    } else if (clazz == boolean.class || clazz == Boolean.class) {
        output.writeBoolNoTag((Boolean) obj);
    } else if (clazz == byte.class || clazz == Byte.class) {
        output.writeRawByte((Byte) obj);
    } else if (clazz == char.class || clazz == Character.class) {
        output.writeSInt32NoTag((Character) obj);
    } else if (clazz == short.class || clazz == Short.class) {
        output.writeSInt32NoTag((Short) obj);
    } else if (clazz == double.class || clazz == Double.class) {
        output.writeDoubleNoTag((Double) obj);
    } else if (clazz == float.class || clazz == Float.class) {
        output.writeFloatNoTag((Float) obj);
    } else if (clazz == String.class) {
        output.writeStringNoTag(obj.toString());
    } else if (MessageLite.class.isAssignableFrom(clazz)) {
        output.writeMessageNoTag((MessageLite) obj);
    } else if (Throwable.class.isAssignableFrom(clazz)) {
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
    } else {
        throw new IllegalArgumentException("can't serialization " + clazz);
    }

    output.flush();
    return baos.toByteArray();
}
 
Example 20
Source File: TezEvent.java    From tez with Apache License 2.0 4 votes vote down vote up
private void serializeEvent(DataOutput out) throws IOException {
  if (event == null) {
    out.writeBoolean(false);
    return;
  }
  out.writeBoolean(true);
  out.writeInt(eventType.ordinal());
  out.writeLong(eventReceivedTime);
  if (eventType.equals(EventType.TASK_STATUS_UPDATE_EVENT)) {
    // TODO NEWTEZ convert to PB
    TaskStatusUpdateEvent sEvt = (TaskStatusUpdateEvent) event;
    sEvt.write(out);
  } else {
    AbstractMessage message;
    switch (eventType) {
    case CUSTOM_PROCESSOR_EVENT:
      message =
          ProtoConverters.convertCustomProcessorEventToProto(
              (CustomProcessorEvent) event);
      break;
    case DATA_MOVEMENT_EVENT:
      message =
          ProtoConverters.convertDataMovementEventToProto(
              (DataMovementEvent) event);
      break;
    case COMPOSITE_ROUTED_DATA_MOVEMENT_EVENT:
        message =
          ProtoConverters.convertCompositeRoutedDataMovementEventToProto(
              (CompositeRoutedDataMovementEvent) event);
    break;
    case COMPOSITE_DATA_MOVEMENT_EVENT:
      message =
          ProtoConverters.convertCompositeDataMovementEventToProto(
              (CompositeDataMovementEvent) event);
      break;
    case VERTEX_MANAGER_EVENT:
      message = ProtoConverters.convertVertexManagerEventToProto((VertexManagerEvent) event);
      break;
    case INPUT_READ_ERROR_EVENT:
      InputReadErrorEvent ideEvt = (InputReadErrorEvent) event;
      message = InputReadErrorEventProto.newBuilder()
          .setIndex(ideEvt.getIndex())
          .setDiagnostics(ideEvt.getDiagnostics())
          .setVersion(ideEvt.getVersion())
          .build();
      break;
    case TASK_ATTEMPT_FAILED_EVENT:
      TaskAttemptFailedEvent tfEvt = (TaskAttemptFailedEvent) event;
      message = TaskAttemptFailedEventProto.newBuilder()
          .setDiagnostics(tfEvt.getDiagnostics())
          .setTaskFailureType(TezConverterUtils.failureTypeToProto(tfEvt.getTaskFailureType()))
          .build();
      break;
      case TASK_ATTEMPT_KILLED_EVENT:
        TaskAttemptKilledEvent tkEvent = (TaskAttemptKilledEvent) event;
        message = TaskAttemptKilledEventProto.newBuilder()
            .setDiagnostics(tkEvent.getDiagnostics()).build();
        break;
    case TASK_ATTEMPT_COMPLETED_EVENT:
      message = TaskAttemptCompletedEventProto.newBuilder()
          .build();
      break;
    case INPUT_FAILED_EVENT:
      InputFailedEvent ifEvt = (InputFailedEvent) event;
      message = InputFailedEventProto.newBuilder()
          .setTargetIndex(ifEvt.getTargetIndex())
          .setVersion(ifEvt.getVersion()).build();
      break;
    case ROOT_INPUT_DATA_INFORMATION_EVENT:
      message = ProtoConverters.convertRootInputDataInformationEventToProto(
          (InputDataInformationEvent) event);
      break;
    case ROOT_INPUT_INITIALIZER_EVENT:
      message = ProtoConverters
          .convertRootInputInitializerEventToProto((InputInitializerEvent) event);
      break;
    default:
      throw new TezUncheckedException("Unknown TezEvent"
         + ", type=" + eventType);
    }
    if (out instanceof OutputStream) { //DataOutputBuffer extends DataOutputStream
      int serializedSize = message.getSerializedSize();
      out.writeInt(serializedSize);
      int buffersize = serializedSize < CodedOutputStream.DEFAULT_BUFFER_SIZE ? serializedSize
          : CodedOutputStream.DEFAULT_BUFFER_SIZE;
      CodedOutputStream codedOut = CodedOutputStream.newInstance(
          (OutputStream) out, buffersize);
      message.writeTo(codedOut);
      codedOut.flush();
    } else {
      byte[] eventBytes = message.toByteArray();
      out.writeInt(eventBytes.length);
      out.write(eventBytes);
    }

  }
}