Search in sources :

Example 1 with ByteArrayInput

use of io.protostuff.ByteArrayInput in project incubator-servicecomb-java-chassis by apache.

the class TestProtobufSchemaUtils method object.

@Test
public void object() throws Exception {
    WrapSchema schema = ProtobufSchemaUtils.getOrCreateSchema(Object.class);
    LinkedBuffer linkedBuf = LinkedBuffer.allocate();
    ProtobufOutput output = new ProtobufOutput(linkedBuf);
    schema.writeObject(output, 1);
    Input input = new ByteArrayInput(output.toByteArray(), false);
    Object result = schema.readObject(input);
    Assert.assertEquals(1, result);
    Assert.assertThat(result, Matchers.instanceOf(Integer.class));
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) ByteArrayInput(io.protostuff.ByteArrayInput) Input(io.protostuff.Input) ByteArrayInput(io.protostuff.ByteArrayInput) ProtobufOutput(io.protostuff.ProtobufOutput) Test(org.junit.Test)

Example 2 with ByteArrayInput

use of io.protostuff.ByteArrayInput in project incubator-servicecomb-java-chassis by apache.

the class TestProtobufCompatibleUtils method testProtostuff.

protected byte[] testProtostuff(Map<String, String> map, Map<String, User> userMap) throws IOException {
    ProtobufCompatibleUtils.init();
    RuntimeSchema<ModelProtostuff> schema = RuntimeSchema.createFrom(ModelProtostuff.class);
    ModelProtostuff model = new ModelProtostuff();
    model.setContext(map);
    model.setUserMap(userMap);
    model.getList().add("l1");
    model.getList().add("l2");
    LinkedBuffer linkedBuffer = LinkedBuffer.allocate();
    ProtobufOutput output = new ProtobufOutput(linkedBuffer);
    ProtobufFeature protobufFeature = new ProtobufFeature();
    protobufFeature.setUseProtobufMapCodec(true);
    ProtobufFeatureUtils.setProtobufFeature(protobufFeature);
    schema.writeTo(output, model);
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    LinkedBuffer.writeTo(s, linkedBuffer);
    byte[] bytes = s.toByteArray();
    ModelProtostuff newModel = new ModelProtostuff();
    ByteArrayInput bai = new ByteArrayInput(bytes, false);
    schema.mergeFrom(bai, newModel);
    ProtobufFeatureUtils.removeProtobufFeature();
    Assert.assertEquals("v1", newModel.getContext().get("k1"));
    Assert.assertEquals("v2", newModel.getContext().get("k2"));
    Assert.assertEquals("n1", newModel.getUserMap().get("u1").getName());
    Assert.assertEquals("n2", newModel.getUserMap().get("u2").getName());
    Assert.assertEquals("l1", newModel.getList().get(0));
    Assert.assertEquals("l2", newModel.getList().get(1));
    return bytes;
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) ByteArrayInput(io.protostuff.ByteArrayInput) ModelProtostuff(io.protostuff.runtime.model.ModelProtostuff) ProtobufOutput(io.protostuff.ProtobufOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with ByteArrayInput

use of io.protostuff.ByteArrayInput in project incubator-servicecomb-java-chassis by apache.

the class RuntimeMapFieldProtobuf method mergeFrom.

@SuppressWarnings("unchecked")
@Override
protected void mergeFrom(Input input, T message) throws IOException {
    if (!ProtobufFeatureUtils.isUseProtobufMapCodec()) {
        runtimeMapField.mergeFrom(input, message);
        return;
    }
    Map<Object, Object> value = null;
    try {
        value = (Map<Object, Object>) field.get(message);
        if (value == null) {
            value = schema.newMessage();
            field.set(message, value);
        }
    } catch (Exception e) {
        throw new ProtostuffException("Failed to get or set map field " + field.getDeclaringClass().getName() + ":" + field.getName(), e);
    }
    MapWrapper<Object, Object> mapWrapper = MapSchemaUtils.createMapWrapper(value);
    if (ByteArrayInput.class.isInstance(input)) {
        ((ByteArrayInput) input).readRawVarint32();
    } else if (ByteBufferInput.class.isInstance(input)) {
        ((ByteBufferInput) input).readRawVarint32();
    } else {
        throw new Error("not handler " + input.getClass().getName());
    }
    int keyNumber = input.readFieldNumber(schema);
    if (keyNumber != 1) {
        throw new ProtostuffException("The map was incorrectly serialized, expect key number 1, but be " + keyNumber);
    }
    Object key = kFrom(input, null);
    int valueNumber = input.readFieldNumber(schema);
    if (valueNumber != 2) {
        throw new ProtostuffException("The map was incorrectly serialized, expect value number 2, but be " + valueNumber);
    }
    vPutFrom(input, mapWrapper, key);
}
Also used : ByteArrayInput(io.protostuff.ByteArrayInput) ByteBufferInput(io.protostuff.ByteBufferInput) ProtostuffException(io.protostuff.ProtostuffException) IOException(java.io.IOException) ProtostuffException(io.protostuff.ProtostuffException)

Example 4 with ByteArrayInput

use of io.protostuff.ByteArrayInput in project java-chassis by ServiceComb.

the class Protostuff method deserialize.

@Override
public Object deserialize(byte[] bytes) throws IOException {
    Input input = new ByteArrayInput(bytes, false);
    Root result = new Root();
    rootSchema.mergeFrom(input, result);
    return result;
}
Also used : ByteArrayInput(io.protostuff.ByteArrayInput) Input(io.protostuff.Input) ByteArrayInput(io.protostuff.ByteArrayInput) Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root)

Aggregations

ByteArrayInput (io.protostuff.ByteArrayInput)4 Input (io.protostuff.Input)2 LinkedBuffer (io.protostuff.LinkedBuffer)2 ProtobufOutput (io.protostuff.ProtobufOutput)2 ByteBufferInput (io.protostuff.ByteBufferInput)1 ProtostuffException (io.protostuff.ProtostuffException)1 ModelProtostuff (io.protostuff.runtime.model.ModelProtostuff)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Root (org.apache.servicecomb.foundation.protobuf.internal.model.Root)1 Test (org.junit.Test)1