io.protostuff.Pipe Java Examples

The following examples show how to use io.protostuff.Pipe. 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: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testPojoWithClassFields() throws Exception
{
    Schema<PojoWithClassFields> schema = RuntimeSchema
            .getSchema(PojoWithClassFields.class);
    Pipe.Schema<PojoWithClassFields> pipeSchema = ((RuntimeSchema<PojoWithClassFields>) schema).getPipeSchema();

    PojoWithClassFields p = new PojoWithClassFields().fill();

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

    PojoWithClassFields pFromByteArray = new PojoWithClassFields();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    PojoWithClassFields pFromStream = new PojoWithClassFields();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example #2
Source File: AbstractRuntimeMapTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testPojoKPolymorphicV() throws Exception
{
    Schema<HasMapPojoKPolymorphicV> schema = RuntimeSchema
            .getSchema(HasMapPojoKPolymorphicV.class);
    Pipe.Schema<HasMapPojoKPolymorphicV> pipeSchema = ((RuntimeSchema<HasMapPojoKPolymorphicV>) schema)
            .getPipeSchema();

    HasMapPojoKPolymorphicV p = new HasMapPojoKPolymorphicV().fill();

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

    HasMapPojoKPolymorphicV pFromByteArray = new HasMapPojoKPolymorphicV();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    HasMapPojoKPolymorphicV pFromStream = new HasMapPojoKPolymorphicV();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromByteArray);

    roundTrip(p, schema, pipeSchema);
}
 
Example #3
Source File: ObjectSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
static void transferArray(Pipe pipe, Input input, Output output, int number,
        Pipe.Schema<?> pipeSchema, boolean mapped, IdStrategy strategy) throws IOException
{
    strategy.transferArrayId(input, output, number, mapped);

    if (input.readFieldNumber(pipeSchema.wrappedSchema) != ID_ARRAY_LEN)
        throw new ProtostuffException("Corrupt input.");

    output.writeUInt32(ID_ARRAY_LEN, input.readUInt32(), false);

    if (input.readFieldNumber(pipeSchema.wrappedSchema) != ID_ARRAY_DIMENSION)
        throw new ProtostuffException("Corrupt input.");

    output.writeUInt32(ID_ARRAY_DIMENSION, input.readUInt32(), false);

    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(strategy.ARRAY_PIPE_SCHEMA, pipeSchema);
    }

    Pipe.transferDirect(strategy.ARRAY_PIPE_SCHEMA, pipe, input, output);
}
 
Example #4
Source File: AbstractRuntimeMapTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testInlineKEnumV() throws Exception
{
    Schema<HasMapInlineKEnumV> schema = RuntimeSchema
            .getSchema(HasMapInlineKEnumV.class);
    Pipe.Schema<HasMapInlineKEnumV> pipeSchema = ((RuntimeSchema<HasMapInlineKEnumV>) schema).getPipeSchema();

    HasMapInlineKEnumV p = new HasMapInlineKEnumV().fill();

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

    HasMapInlineKEnumV pFromByteArray = new HasMapInlineKEnumV();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    HasMapInlineKEnumV pFromStream = new HasMapInlineKEnumV();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromByteArray);

    roundTrip(p, schema, pipeSchema);
}
 
Example #5
Source File: AbstractRuntimeMapTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testInlineKInlineV() throws Exception
{
    Schema<HasMapInlineKInlineV> schema = RuntimeSchema
            .getSchema(HasMapInlineKInlineV.class);
    Pipe.Schema<HasMapInlineKInlineV> pipeSchema = ((RuntimeSchema<HasMapInlineKInlineV>) schema).getPipeSchema();

    HasMapInlineKInlineV p = new HasMapInlineKInlineV().fill();

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

    HasMapInlineKInlineV pFromByteArray = new HasMapInlineKInlineV();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    HasMapInlineKInlineV pFromStream = new HasMapInlineKInlineV();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromByteArray);

    roundTrip(p, schema, pipeSchema);
}
 
Example #6
Source File: AbstractRuntimeMapTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testEnumKInlineV() throws Exception
{
    Schema<HasMapEnumKInlineV> schema = RuntimeSchema
            .getSchema(HasMapEnumKInlineV.class);
    Pipe.Schema<HasMapEnumKInlineV> pipeSchema = ((RuntimeSchema<HasMapEnumKInlineV>) schema).getPipeSchema();

    HasMapEnumKInlineV p = new HasMapEnumKInlineV().fill();

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

    HasMapEnumKInlineV pFromByteArray = new HasMapEnumKInlineV();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    HasMapEnumKInlineV pFromStream = new HasMapEnumKInlineV();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromByteArray);

    roundTrip(p, schema, pipeSchema);
}
 
Example #7
Source File: DerivativeSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
public void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    final int first = input.readFieldNumber(DerivativeSchema.this);
    if (first != ID_POJO)
        throw new ProtostuffException("order not preserved.");

    final Pipe.Schema<Object> pipeSchema = strategy.transferPojoId(
            input, output, ID_POJO).getPipeSchema();

    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(pipeSchema, this);
    }

    Pipe.transferDirect(pipeSchema, pipe, input, output);
}
 
Example #8
Source File: AbstractRuntimeMapTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testPojoKEnumV() throws Exception
{
    Schema<HasMapPojoKEnumV> schema = RuntimeSchema
            .getSchema(HasMapPojoKEnumV.class);
    Pipe.Schema<HasMapPojoKEnumV> pipeSchema = ((RuntimeSchema<HasMapPojoKEnumV>) schema).getPipeSchema();

    HasMapPojoKEnumV p = new HasMapPojoKEnumV().fill();

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

    HasMapPojoKEnumV pFromByteArray = new HasMapPojoKEnumV();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    HasMapPojoKEnumV pFromStream = new HasMapPojoKEnumV();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromByteArray);

    roundTrip(p, schema, pipeSchema);
}
 
Example #9
Source File: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testPojoWithThrowableArray() throws Exception
{
    Schema<PojoWithThrowableArray> schema = RuntimeSchema
            .getSchema(PojoWithThrowableArray.class);
    Pipe.Schema<PojoWithThrowableArray> pipeSchema = ((RuntimeSchema<PojoWithThrowableArray>) schema)
            .getPipeSchema();

    PojoWithThrowableArray p = new PojoWithThrowableArray().fill();

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

    PojoWithThrowableArray pFromByteArray = new PojoWithThrowableArray();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    PojoWithThrowableArray pFromStream = new PojoWithThrowableArray();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example #10
Source File: AbstractRuntimeMapTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testInlineKPolymorphicV() throws Exception
{
    Schema<HasMapInlineKPolymorphicV> schema = RuntimeSchema
            .getSchema(HasMapInlineKPolymorphicV.class);
    Pipe.Schema<HasMapInlineKPolymorphicV> pipeSchema = ((RuntimeSchema<HasMapInlineKPolymorphicV>) schema)
            .getPipeSchema();

    HasMapInlineKPolymorphicV p = new HasMapInlineKPolymorphicV().fill();

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

    HasMapInlineKPolymorphicV pFromByteArray = new HasMapInlineKPolymorphicV();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    HasMapInlineKPolymorphicV pFromStream = new HasMapInlineKPolymorphicV();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromByteArray);

    roundTrip(p, schema, pipeSchema);
}
 
Example #11
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
public Pipe.Schema<T> getPipeSchema()
{
    Pipe.Schema<T> pipeSchema = this.pipeSchema;
    if (pipeSchema == null)
    {
        synchronized (this)
        {
            if ((pipeSchema = this.pipeSchema) == null)
            {
                this.pipeSchema = pipeSchema = RuntimeSchema.resolvePipeSchema(
                        getSchema(), typeClass, true);
            }
        }
    }
    return pipeSchema;
}
 
Example #12
Source File: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testPojoWithArray2D() throws Exception
{
    Schema<PojoWithArray2D> schema = RuntimeSchema
            .getSchema(PojoWithArray2D.class);
    Pipe.Schema<PojoWithArray2D> pipeSchema = ((RuntimeSchema<PojoWithArray2D>) schema).getPipeSchema();

    PojoWithArray2D p = new PojoWithArray2D().fill();

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

    PojoWithArray2D pFromByteArray = new PojoWithArray2D();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    PojoWithArray2D pFromStream = new PojoWithArray2D();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example #13
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Pojo ids start at 1.
 */
@Override
public <T> Registry registerPojo(Schema<T> schema, Pipe.Schema<T> pipeSchema,
        int id)
{
    if (id >= strategy.pojoIdStart)
        throw new IllegalArgumentException("pojo ids must be lesser than " + strategy.pojoIdStart);
    else if (strategy.pojos.get(id) != null)
    {
        throw new IllegalArgumentException("Duplicate id registration: " + id +
                " (" + schema.typeClass().getName() + ")");
    }

    if (strategy.pojoMapping.containsKey(schema.typeClass()))
        throw new IllegalArgumentException("Duplicate registration for: " + schema.typeClass());

    Registered<T> wrapper = new Registered<T>(id, schema, pipeSchema, strategy);
    strategy.pojos.set(id, wrapper);

    strategy.pojoMapping.put(schema.typeClass(), wrapper);

    return this;
}
 
Example #14
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public Pipe.Schema<T> getPipeSchema() {
	Pipe.Schema<T> pipeSchema = this.pipeSchema;
	if (pipeSchema == null) {
		synchronized (this) {
			if ((pipeSchema = this.pipeSchema) == null) {
				this.pipeSchema = pipeSchema = RuntimeSchema.resolvePipeSchema(getSchema(), typeClass, true);
			}
		}
	}
	return pipeSchema;
}
 
Example #15
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    if (ID_ARRAY_LEN != input
            .readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");

    final int len = input.readInt32();
    // write it back
    output.writeInt32(ID_ARRAY_LEN, len, false);

    for (int i = 0, nullCount = 0; i < len;)
    {
        switch (input.readFieldNumber(pipeSchema.wrappedSchema))
        {
            case ID_ARRAY_DATA:
                i++;
                output.writeObject(ID_ARRAY_DATA, pipe, hs.getPipeSchema(),
                        true);
                break;
            case ID_ARRAY_NULLCOUNT:
                nullCount = input.readUInt32();
                i += nullCount;
                output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
                break;
            default:
                throw new ProtostuffException("Corrupt input.");
        }
    }

    if (0 != input.readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");
}
 
Example #16
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public Pipe.Schema<T> getPipeSchema() {
	Pipe.Schema<T> pipeSchema = this.pipeSchema;
	if (pipeSchema == null) {
		synchronized (this) {
			if ((pipeSchema = this.pipeSchema) == null) {
				this.pipeSchema = pipeSchema = RuntimeSchema.resolvePipeSchema(schema, schema.typeClass(),
						true);
			}
		}
	}
	return pipeSchema;
}
 
Example #17
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public Pipe.Schema<T> getPipeSchema() {
	Pipe.Schema<T> pipeSchema = this.pipeSchema;
	if (pipeSchema == null) {
		synchronized (this) {
			if ((pipeSchema = this.pipeSchema) == null) {
				this.pipeSchema = pipeSchema = RuntimeSchema.resolvePipeSchema(getSchema(), typeClass, true);
			}
		}
	}

	return pipeSchema;
}
 
Example #18
Source File: PolymorphicPojoCollectionSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe,
        Input input, Output output, IdStrategy strategy) throws IOException
{
    final int number = input.readFieldNumber(pipeSchema.wrappedSchema);
    if (number != ID_POJO)
        PolymorphicCollectionSchema.transferObject(pipeSchema, pipe, input, output, strategy, number);
    else
        transferObject(pipeSchema, pipe, input, output, strategy, number);
}
 
Example #19
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    if (ID_ARRAY_LEN != input
            .readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");

    final int len = input.readInt32();
    // write it back
    output.writeInt32(ID_ARRAY_LEN, len, false);

    for (int i = 0, nullCount = 0; i < len;)
    {
        switch (input.readFieldNumber(pipeSchema.wrappedSchema))
        {
            case ID_ARRAY_DATA:
                i++;
                EnumIO.transfer(pipe, input, output, ID_ARRAY_DATA, true, eio.strategy);
                break;
            case ID_ARRAY_NULLCOUNT:
                nullCount = input.readUInt32();
                i += nullCount;
                output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
                break;
            default:
                throw new ProtostuffException("Corrupt input.");
        }
    }

    if (0 != input.readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");
}
 
Example #20
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 #21
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    transferObject(this, pipe, input, output, strategy,
            RuntimeFieldFactory.BOOL);
}
 
Example #22
Source File: ProtostuffRuntimeMapTest.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 #23
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
Registered(int id, Schema<T> schema, Pipe.Schema<T> pipeSchema, 
        IdStrategy strategy)
{
    super(strategy);
    this.id = id;

    this.schema = schema;
    this.pipeSchema = pipeSchema;
}
 
Example #24
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    transferObject(this, pipe, input, output, strategy,
            RuntimeFieldFactory.SHORT);
}
 
Example #25
Source File: ClassSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe,
        Input input, Output output, IdStrategy strategy) throws IOException
{
    final int number = input.readFieldNumber(pipeSchema.wrappedSchema);
    switch (number)
    {
        case ID_CLASS:
            ObjectSchema.transferClass(pipe, input, output, number, pipeSchema,
                    false, false, strategy);
            break;

        case ID_CLASS_MAPPED:
            ObjectSchema.transferClass(pipe, input, output, number, pipeSchema,
                    true, false, strategy);
            break;

        case ID_CLASS_ARRAY:
            ObjectSchema.transferClass(pipe, input, output, number, pipeSchema,
                    false, true, strategy);
            break;

        case ID_CLASS_ARRAY_MAPPED:
            ObjectSchema.transferClass(pipe, input, output, number, pipeSchema,
                    true, true, strategy);
            break;

        default:
            throw new ProtostuffException("Corrupt input.");
    }

    if (0 != input.readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");
}
 
Example #26
Source File: NullArrayElementTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testArrayInList() throws IOException
{
    Schema<PojoWithArrayInList> schema =
            getSchema(PojoWithArrayInList.class);

    Pipe.Schema<PojoWithArrayInList> pipeSchema =
            ((RuntimeSchema<PojoWithArrayInList>) schema).getPipeSchema();

    PojoWithArrayInList p = new PojoWithArrayInList();
    p.boolArrayList.add(new Boolean[] { true, false });
    p.charArrayList.add(new Character[] { 'a', 'b' });
    p.shortArrayList.add(new Short[] { 1, 2 });
    p.intArrayList.add(new Integer[] { 1, 2 });
    p.longArrayList.add(new Long[] { 1l, 2l });
    p.floatArrayList.add(new Float[] { 1.1f, 2.2f });
    p.doubleArrayList.add(new Double[] { 1.1d, 2.2d });

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

    PojoWithArrayInList pFromByteArray = schema.newMessage();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    PojoWithArrayInList pFromStream = schema.newMessage();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example #27
Source File: NullArrayElementTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testNullLast() throws IOException
{
    Schema<PojoWithNonPrimitiveArrays> schema =
            getSchema(PojoWithNonPrimitiveArrays.class);

    Pipe.Schema<PojoWithNonPrimitiveArrays> pipeSchema =
            ((RuntimeSchema<PojoWithNonPrimitiveArrays>) schema).getPipeSchema();

    PojoWithNonPrimitiveArrays p = new PojoWithNonPrimitiveArrays(
            new Boolean[] { true, false, null },
            new Character[] { 'a', 'b', null },
            new Short[] { 1, 2, null },
            new Integer[] { 1, 2, null },
            new Long[] { 1l, 2l, null },
            new Float[] { 1.1f, 2.2f, null },
            new Double[] { 1.1d, 2.2d, null },
            new String[] { "a", "b", null },
            new ByteString[] { ByteString.copyFromUtf8("a"), ByteString.copyFromUtf8("b"), null },
            new byte[][] { new byte[] { 'a' }, new byte[] { 'b' }, null },
            new BigDecimal[] { new BigDecimal(1.1d), new BigDecimal(2.2d), null },
            new BigInteger[] { new BigInteger("1"), new BigInteger("2"), null },
            new Date[] { new Date(), new Date(), null },
            new Size[] { Size.MEDIUM, Size.LARGE, null },
            new SomePojo[] { new SomePojo("a"), new SomePojo("b"), null }
            );

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

    PojoWithNonPrimitiveArrays pFromByteArray = schema.newMessage();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    PojoWithNonPrimitiveArrays pFromStream = schema.newMessage();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example #28
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    transferObject(this, pipe, input, output, strategy,
            RuntimeFieldFactory.BYTES);
}
 
Example #29
Source File: ObjectSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void transferClass(Pipe pipe, Input input, Output output, int number,
        Pipe.Schema<?> pipeSchema, boolean mapped, boolean array,
        IdStrategy strategy) throws IOException
{
    strategy.transferClassId(input, output, number, mapped, array);

    if (array)
    {
        if (input.readFieldNumber(pipeSchema.wrappedSchema) != ID_ARRAY_DIMENSION)
            throw new ProtostuffException("Corrupt input.");

        output.writeUInt32(ID_ARRAY_DIMENSION, input.readUInt32(), false);
    }
}
 
Example #30
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));
}