Java Code Examples for com.google.protobuf.ByteString.Output

The following examples show how to use com.google.protobuf.ByteString.Output. These examples are extracted from open source projects. 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 Project: kogito-runtimes   Source File: PersisterHelper.java    License: Apache License 2.0 6 votes vote down vote up
private static void writeStrategiesIndex(MarshallerWriteContext context,
                                          ProtobufMessages.Header.Builder _header) throws IOException {
     for( Entry<ObjectMarshallingStrategy,Integer> entry : context.usedStrategies.entrySet() ) {
Builder _strat = ProtobufMessages.Header.StrategyIndex.newBuilder()
                                  .setId( entry.getValue().intValue() )
                                  .setName( entry.getKey().getName()  );

         Context ctx = context.strategyContext.get( entry.getKey() );
         if( ctx != null ) {
             Output os = ByteString.newOutput();
             ctx.write( new DroolsObjectOutputStream( os ) );
             _strat.setData( os.toByteString() );
             os.close();
         }
         _header.addStrategy( _strat.build() );
     }
 }
 
Example 2
Source Project: dremio-oss   Source File: ProtobufByteStringSerDe.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Serialize the given value to byte string using the given mapper, employing the given codec algorithm.
 *
 * @param mapper object mapper
 * @param value  value to serialize
 * @param codec  codec
 * @return serialized bytes
 * @throws JsonGenerationException in case of serialization errors
 */
public static ByteString writeValue(ObjectMapper mapper, Object value, Codec codec)
    throws JsonGenerationException {
  final Output output = ByteString.newOutput();

  try {
    final OutputStream os = codec.compress(output);
    try {
      mapper.writer()
          .without(SerializationFeature.INDENT_OUTPUT)
          .writeValue(os, value);
    } finally {
      os.close();
    }
  } catch (IOException e) {
    // Should not happen but...
    throw new JsonGenerationException(e, null);
  }

  // Javadoc says data is copied, but it's more of a transfer of ownership!
  return output.toByteString();
}
 
Example 3
Source Project: travelguide   Source File: ByteStringTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testNewOutput_ArrayWrite() throws IOException {
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {128, 256, length / 2, length - 1, length, length + 1,
                       2 * length, 3 * length};
  int[] writeSizes = {1, 4, 5, 7, 23, bytes.length};

  for (int bufferSize : bufferSizes) {
    for (int writeSize : writeSizes) {
      // Test writing the entire output writeSize bytes at a time.
      ByteString.Output output = ByteString.newOutput(bufferSize);
      for (int i = 0; i < length; i += writeSize) {
        output.write(bytes, i, Math.min(writeSize, length - i));
      }
      ByteString byteString = output.toByteString();
      assertTrue("String built from newOutput() must contain the expected bytes",
          isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
    }
  }
}
 
Example 4
Source Project: travelguide   Source File: ByteStringTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testNewOutput_WriteChar() throws IOException {
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {0, 1, 128, 256, length / 2,
                       length - 1, length, length + 1,
                       2 * length, 3 * length};
  for (int bufferSize : bufferSizes) {
    ByteString.Output output = ByteString.newOutput(bufferSize);
    for (byte byteValue : bytes) {
      output.write(byteValue);
    }
    ByteString byteString = output.toByteString();
    assertTrue("String built from newOutput() must contain the expected bytes",
        isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
  }
}
 
Example 5
Source Project: OpenYOLO-Android   Source File: ProtoListUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link ByteString} by serializing the list of protos. Use
 * {@link #readMessageList(ByteString, Parser)} to deserialize.
 */
public static <T extends MessageLite> ByteString writeMessageList(List<T> protos) {
    Output output = ByteString.newOutput();
    try {
        writeMessageListTo(output, protos);
    } catch (IOException ex) {
        throw new IllegalStateException("Unable to write protobufs to memory");
    }

    return output.toByteString();
}
 
Example 6
Source Project: travelguide   Source File: ByteStringTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testNewOutput_InitialCapacity() throws IOException {
  byte[] bytes = getTestBytes();
  ByteString.Output output = ByteString.newOutput(bytes.length + 100);
  output.write(bytes);
  ByteString byteString = output.toByteString();
  assertTrue(
      "String built from newOutput(int) must contain the expected bytes",
      isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
}
 
Example 7
Source Project: travelguide   Source File: ByteStringTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testNewOutput_Mixed() throws IOException {
  Random rng = new Random(1);
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {0, 1, 128, 256, length / 2,
                       length - 1, length, length + 1,
                       2 * length, 3 * length};

  for (int bufferSize : bufferSizes) {
    // Test writing the entire output using a mixture of write sizes and
    // methods;
    ByteString.Output output = ByteString.newOutput(bufferSize);
    int position = 0;
    while (position < bytes.length) {
      if (rng.nextBoolean()) {
        int count = 1 + rng.nextInt(bytes.length - position);
        output.write(bytes, position, count);
        position += count;
      } else {
        output.write(bytes[position]);
        position++;
      }
      assertEquals("size() returns the right value", position, output.size());
      assertTrue("newOutput() substring must have correct bytes",
          isArrayRange(output.toByteString().toByteArray(),
              bytes, 0, position));
    }
    ByteString byteString = output.toByteString();
    assertTrue("String built from newOutput() must contain the expected bytes",
        isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
  }
}
 
Example 8
Source Project: travelguide   Source File: ByteStringTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testNewOutput_Mutating() throws IOException {
  Output os = ByteString.newOutput(5);
  os.write(new byte[] {1, 2, 3, 4, 5});
  EvilOutputStream eos = new EvilOutputStream();
  os.writeTo(eos);
  byte[] capturedArray = eos.capturedArray;
  ByteString byteString = os.toByteString();
  byte[] oldValue = byteString.toByteArray();
  Arrays.fill(capturedArray, (byte) 0);
  byte[] newValue = byteString.toByteArray();
  assertTrue("Output must not provide access to the underlying byte array",
      Arrays.equals(oldValue, newValue));
}
 
Example 9
Source Project: incubator-tez   Source File: DagTypeConverters.java    License: Apache License 2.0 5 votes vote down vote up
public static ByteString convertCredentialsToProto(Credentials credentials) {
  if (credentials == null) {
    return null;
  }
  Output output = ByteString.newOutput();
  DataOutputStream dos = new DataOutputStream(output);
  try {
    credentials.writeTokenStorageToStream(dos);
    return output.toByteString();
  } catch (IOException e) {
    throw new TezUncheckedException("Failed to serialize Credentials", e);
  }
}
 
Example 10
Source Project: tez   Source File: DagTypeConverters.java    License: Apache License 2.0 5 votes vote down vote up
public static ByteString convertCredentialsToProto(Credentials credentials) {
  if (credentials == null) {
    return null;
  }
  Output output = ByteString.newOutput();
  DataOutputStream dos = new DataOutputStream(output);
  try {
    credentials.writeTokenStorageToStream(dos);
    return output.toByteString();
  } catch (IOException e) {
    throw new TezUncheckedException("Failed to serialize Credentials", e);
  }
}