Java Code Examples for io.protostuff.ProtobufIOUtil#toByteArray()

The following examples show how to use io.protostuff.ProtobufIOUtil#toByteArray() . 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: PolymorphicSerializationTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testProtobuf() throws Exception
{
    Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class);
    Zoo p = filledZoo();

    byte[] data = ProtobufIOUtil.toByteArray(p, schema, buf());
    // System.err.println("protobuf: " + data.length);

    Zoo p2 = new Zoo();
    ProtobufIOUtil.mergeFrom(data, p2, schema);

    assertEquals(p, p2);

    List<Zoo> list = new ArrayList<Zoo>();
    list.add(p);
    list.add(p2);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ProtobufIOUtil.writeListTo(out, list, schema, buf());
    byte[] listData = out.toByteArray();

    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    List<Zoo> parsedList = ProtobufIOUtil.parseListFrom(in, schema);

    assertEquals(list, parsedList);
}
 
Example 2
Source File: DateTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testProtobuf() throws Exception
{
    Schema<Entity> schema = RuntimeSchema.getSchema(Entity.class);
    Entity p = filledEntity();

    byte[] data = ProtobufIOUtil.toByteArray(p, schema,
            LinkedBuffer.allocate(512));

    Entity p2 = new Entity();
    ProtostuffIOUtil.mergeFrom(data, p2, schema);

    assertEquals(p, p2);

    List<Entity> list = new ArrayList<Entity>();
    list.add(p);
    list.add(p2);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ProtobufIOUtil.writeListTo(out, list, schema, buf());
    byte[] listData = out.toByteArray();

    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    List<Entity> parsedList = ProtobufIOUtil.parseListFrom(in, schema);

    assertEquals(list, parsedList);
}
 
Example 3
Source File: ProtostuffSerializer.java    From BigData-In-Practice with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(String topic, Company data) {
    if (data == null) {
        return null;
    }
    Schema schema = RuntimeSchema.getSchema(data.getClass());
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
    byte[] protostuff = null;
    try {
        protostuff = ProtobufIOUtil.toByteArray(data, schema, buffer);
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    } finally {
        buffer.clear();
    }
    return protostuff;
}
 
Example 4
Source File: FileFormat.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@JsonIgnore
@SuppressWarnings({ "rawtypes", "unchecked" })
public FileConfig asFileConfig() {
  buffer.clear();
  FileConfig fc = new FileConfig();
  fc.setType(getFileType());
  fc.setName(name);
  fc.setOwner(owner);
  fc.setCtime(ctime);
  fc.setType(getFileType());
  fc.setTag(getVersion());
  fc.setLocation(location);
  byte[] bytes = ProtobufIOUtil.toByteArray(this, (Schema) getPrivateSchema(), buffer);
  fc.setExtendedConfig(ByteString.copyFrom(bytes));
  fc.setFullPathList(fullPath);
  return fc;
}
 
Example 5
Source File: EnumTest.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
@Test
void unknownEnumValue_toString() {
    TestUnknownEnumValue2 source = TestUnknownEnumValue2.newBuilder()
            .setField(TestUnknownEnumValue2.E2.C)
            .build();
    byte[] data = ProtobufIOUtil.toByteArray(source, TestUnknownEnumValue2.getSchema(), LinkedBuffer.allocate());
    Schema<TestUnknownEnumValue1> schema = TestUnknownEnumValue1.getSchema();
    TestUnknownEnumValue1 message = schema.newMessage();
    ProtobufIOUtil.mergeFrom(data, message, schema);
    Assertions.assertEquals("TestUnknownEnumValue1{field=UNRECOGNIZED(2)}", message.toString());
}
 
Example 6
Source File: MathObjectsTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testProtobuf() throws Exception
{
    Schema<Payment> schema = RuntimeSchema.getSchema(Payment.class);
    Payment p = filledPayment();

    byte[] data = ProtobufIOUtil.toByteArray(p, schema, buf());

    Payment p2 = new Payment();
    ProtobufIOUtil.mergeFrom(data, p2, schema);
    /*
     * System.err.println(p2.getId()); System.err.println(p2.getBd()); System.err.println(p2.getBi());
     * System.err.println(p2.getBdList()); System.err.println(p2.getBiList());
     */

    assertEquals(p, p2);

    List<Payment> list = new ArrayList<Payment>();
    list.add(p);
    list.add(p2);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ProtobufIOUtil.writeListTo(out, list, schema, buf());
    byte[] listData = out.toByteArray();

    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    List<Payment> parsedList = ProtobufIOUtil.parseListFrom(in, schema);

    assertEquals(list, parsedList);
}
 
Example 7
Source File: ProtostuffRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws Exception
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(Arrays.equals(protostuff, protostuffFromStream));

    T parsedMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema);
    SerializableObjects.assertEquals(message, parsedMessage);

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(Arrays.equals(protobufRoundTrip, protobufRoundTripFromStream));

    assertTrue(Arrays.equals(protobufRoundTrip, protobuf));
}
 
Example 8
Source File: BusProtoStuffMessageConverter.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private static byte[] writeClassHeader(final Class<?> clazz) {
    final EventType clazzEventType = EventType.from(clazz);
    if (clazzEventType == null) {
        LOG.error("There is no mapping to EventType for the given class {}", clazz);
        throw new MessageConversionException("Missing EventType for given class : " + clazz);
    }
    @SuppressWarnings("unchecked")
    final Schema<Object> schema = (Schema<Object>) RuntimeSchema
            .getSchema((Class<?>) EventType.class);
    final LinkedBuffer buffer = LinkedBuffer.allocate();
    return ProtobufIOUtil.toByteArray(clazzEventType, schema, buffer);
}
 
Example 9
Source File: BusProtoStuffMessageConverter.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private static byte[] writeContent(final Object payload) {
    final Class<?> serializeClass = payload.getClass();
    @SuppressWarnings("unchecked")
    final Schema<Object> schema = (Schema<Object>) RuntimeSchema.getSchema(serializeClass);
    final LinkedBuffer buffer = LinkedBuffer.allocate();
    return ProtobufIOUtil.toByteArray(payload, schema, buffer);
}
 
Example 10
Source File: JobsProtoUtil.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Generic method to convert Protostuff to Protobuf. Uses LegacyProtobufSerializer because deserializing with the
 * regular protobuf serializer does not handle repeated fields correctly.
 * @param protobufParser Parser for protobuf object
 * @param protostuff Protostuff object to convert
 * @param <M> Type of Protobuf
 * @param <T> Type of Protobuff
 * @return Converted object as Protobuf
 */
private static <M extends GeneratedMessageV3, T extends Message<T> & Schema<T>>
M toBuf(Parser<M> protobufParser, T protostuff) {
  try {
    LinkedBuffer buffer = LinkedBuffer.allocate();
    byte[] bytes = ProtobufIOUtil.toByteArray(protostuff, protostuff.cachedSchema(), buffer);
    // LegacyProtobufSerializer is necessary as it deals with stuff/buf grouping differences
    return LegacyProtobufSerializer.parseFrom(protobufParser, bytes);
  } catch (InvalidProtocolBufferException e) {
    throw new IllegalArgumentException("Cannot convert from protostuff to protobuf");
  }
}
 
Example 11
Source File: ProtostuffNullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws IOException
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(protostuff.length == protostuffFromStream.length);
    assertEquals(STRING.deser(protostuff),
            STRING.deser(protostuffFromStream));

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip,
            STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, STRING.deser(protobuf));
}
 
Example 12
Source File: ProtoMarshaller.java    From faster-framework-project with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream stream(Object value) {
    if (value == null) {
        return new ByteArrayInputStream(new byte[]{});
    }
    if (requestSchema == null) {
        requestSchema = RuntimeSchema.getSchema((Class<Object>) value.getClass());
    }
    return new ByteArrayInputStream(ProtobufIOUtil.toByteArray(value, requestSchema, buffer));
}
 
Example 13
Source File: QueryUtil.java    From datawave with Apache License 2.0 5 votes vote down vote up
public static <T extends Query> Mutation toMutation(T query, ColumnVisibility vis) {
    // Store by sid for backwards compatibility
    Mutation m = new Mutation(query.getOwner());
    try {
        @SuppressWarnings("unchecked")
        Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(query.getClass());
        byte[] bytes = ProtobufIOUtil.toByteArray(query, schema, BUFFER.get());
        m.put(query.getQueryName(), query.getId() + NULL_BYTE + query.getClass().getName(), vis, query.getExpirationDate().getTime(), new Value(bytes));
        return m;
    } finally {
        BUFFER.get().clear();
    }
}
 
Example 14
Source File: SerializationUtil.java    From springboot-learn with MIT License 5 votes vote down vote up
public static <T> byte[] serialize(T obj) {
    Class<T> aClass = (Class<T>) obj.getClass();
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
    try {
        Schema<T> schema = getSchema(aClass);
        return ProtobufIOUtil.toByteArray(obj, schema, buffer);
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    } finally {
        buffer.clear();
    }
}
 
Example 15
Source File: ProtobufNullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> byte[] toByteArray(T message, Schema<T> schema)
{
    return ProtobufIOUtil.toByteArray(message, schema, buf());
}
 
Example 16
Source File: ConnectionConf.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public byte[] toBytes() {
  return ProtobufIOUtil.toByteArray( (T) this, schema, LinkedBuffer.allocate() );
}
 
Example 17
Source File: ProtobufRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> byte[] toByteArray(T message, Schema<T> schema)
{
    return ProtobufIOUtil.toByteArray(message, schema, buf());
}
 
Example 18
Source File: ProtostuffNullArrayElementTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws IOException
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(protostuff.length == protostuffFromStream.length);
    assertEquals(STRING.deser(protostuff),
            STRING.deser(protostuffFromStream));

    T parsedMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema);
    SerializableObjects.assertEquals(message, parsedMessage);

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip,
            STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, STRING.deser(protobuf));
}
 
Example 19
Source File: ProtostuffRuntimeCollectionSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws Exception
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(protostuff.length == protostuffFromStream.length);
    assertEquals(STRING.deser(protostuff),
            STRING.deser(protostuffFromStream));

    T parsedMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema);
    SerializableObjects.assertEquals(message, parsedMessage);

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip,
            STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, STRING.deser(protobuf));
}
 
Example 20
Source File: ProtobufNullArrayElementTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws IOException
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(protostuff.length == protostuffFromStream.length);
    assertEquals(STRING.deser(protostuff),
            STRING.deser(protostuffFromStream));

    T parsedMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema);
    SerializableObjects.assertEquals(message, parsedMessage);

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip,
            STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, STRING.deser(protobuf));
}