/* (non-Javadoc)
  * @see org.directmemory.utils.Serializer#deserialize(byte[], java.lang.Class)
  */
 @SuppressWarnings("unchecked")
 public Object deserialize(byte[] source, @SuppressWarnings("rawtypes") Class clazz)
     throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
   final Object object = clazz.newInstance();
   @SuppressWarnings("rawtypes")
   final Schema schema = RuntimeSchema.getSchema(clazz);
   ProtostuffIOUtil.mergeFrom(source, object, schema);
   return object;
 }
  /**
   * HasHasBar wraps an object without a schema. That object will have to be serialized via the
   * default java serialization and it will be delimited.
   *
   * <p>HasBar wraps a message {@link Bar}.
   */
  public void testJavaSerializable() throws Exception {
    Schema<HasHasBar> schema = RuntimeSchema.getSchema(HasHasBar.class);

    HasHasBar hhbCompare = new HasHasBar("hhb", new HasBar(12345, "hb", SerializableObjects.bar));
    HasHasBar dhhb = new HasHasBar();

    int expectedSize = ComputedSizeOutput.getSize(hhbCompare, schema);

    byte[] deferred = toByteArray(hhbCompare, schema);
    assertTrue(deferred.length == expectedSize);
    ProtostuffIOUtil.mergeFrom(deferred, dhhb, schema);
    assertEquals(hhbCompare, dhhb);
  }
  /* (non-Javadoc)
   * @see org.directmemory.utils.Serializer#serialize(java.lang.Object, java.lang.Class)
   */
  @SuppressWarnings("unchecked")
  public byte[] serialize(Object obj, @SuppressWarnings("rawtypes") Class clazz)
      throws IOException {
    @SuppressWarnings("rawtypes")
    Schema schema = RuntimeSchema.getSchema(clazz);
    final LinkedBuffer buffer = LinkedBuffer.allocate(serBufferSize);
    byte[] protostuff = null;

    try {
      protostuff = ProtostuffIOUtil.toByteArray(obj, schema, buffer);
    } finally {
      buffer.clear();
    }
    return protostuff;
  }
  public void testFoo() throws Exception {
    Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class);

    Foo fooCompare = foo;
    Foo dfoo = new Foo();

    byte[] deferred = toByteArray(fooCompare, schema);

    // ComputedSizeOutput is format compatible with protobuf
    // E.g collections are not serialized ... only its members/elements are.
    if (!RuntimeEnv.COLLECTION_SCHEMA_ON_REPEATED_FIELDS)
      assertTrue(deferred.length == ComputedSizeOutput.getSize(fooCompare, schema));

    ProtostuffIOUtil.mergeFrom(deferred, dfoo, schema);
    SerializableObjects.assertEquals(fooCompare, dfoo);
  }
  public void testPojoWithArrayAndSet() throws Exception {
    PojoWithArrayAndSet pojoCompare = filledPojoWithArrayAndSet();

    Schema<PojoWithArrayAndSet> schema = RuntimeSchema.getSchema(PojoWithArrayAndSet.class);

    PojoWithArrayAndSet dpojo = new PojoWithArrayAndSet();

    int expectedSize = ComputedSizeOutput.getSize(pojoCompare, schema, true);

    byte[] deferred = toByteArray(pojoCompare, schema);
    assertTrue(deferred.length == expectedSize);
    ProtostuffIOUtil.mergeFrom(deferred, dpojo, schema);
    assertEquals(pojoCompare, dpojo);
    // System.err.println(dpojo.getSomeEnumAsSet());
    // System.err.println(dpojo.getSomeFloatAsSet());
  }
  public void testBaz() throws Exception {
    Schema<Baz> schema = RuntimeSchema.getSchema(Baz.class);

    for (Baz bazCompare : new Baz[] {baz, negativeBaz}) {
      Baz dbaz = new Baz();

      int expectedSize = ComputedSizeOutput.getSize(bazCompare, schema);

      byte[] deferred = toByteArray(bazCompare, schema);
      assertTrue(deferred.length == expectedSize);
      ProtostuffIOUtil.mergeFrom(deferred, dbaz, schema);
      SerializableObjects.assertEquals(bazCompare, dbaz);
      // System.err.println(dbaz.getId());
      // System.err.println(dbaz.getName());
      // System.err.println(dbaz.getTimestamp());
    }
  }
  public void testBar() throws Exception {
    Schema<Bar> schema = RuntimeSchema.getSchema(Bar.class);

    for (Bar barCompare : new Bar[] {bar, negativeBar}) {
      Bar dbar = new Bar();

      int expectedSize = ComputedSizeOutput.getSize(barCompare, schema);

      byte[] deferred = toByteArray(barCompare, schema);
      assertTrue(deferred.length == expectedSize);
      ProtostuffIOUtil.mergeFrom(deferred, dbar, schema);
      SerializableObjects.assertEquals(barCompare, dbar);
      // System.err.println(dbar.getSomeInt());
      // System.err.println(dbar.getSomeLong());
      // System.err.println(dbar.getSomeFloat());
      // System.err.println(dbar.getSomeDouble());
      // System.err.println(dbar.getSomeBytes());
      // System.err.println(dbar.getSomeString());
      // System.err.println(dbar.getSomeEnum());
      // System.err.println(dbar.getSomeBoolean());
    }
  }
 public <T> byte[] toByteArray(T message, Schema<T> schema) {
   return ProtostuffIOUtil.toByteArray(message, schema, buf());
 }
Beispiel #9
0
 @Override
 public <T> T deserializeInternal(final byte[] bytes, final T result, final Schema<T> schema) {
   ProtostuffIOUtil.mergeFrom(bytes, result, schema);
   return result;
 }
Beispiel #10
0
 @Override
 public <T> byte[] serializeInternal(
     final T source, final Schema<T> schema, final LinkedBuffer buffer) {
   return ProtostuffIOUtil.toByteArray(source, schema, buffer);
 }