Search in sources :

Example 16 with LinkedBuffer

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

the class TestHighwayServerConnection method createBuffer.

protected Buffer createBuffer(WrapSchema schema, Object value) throws Exception {
    Buffer headerBuffer;
    LinkedBuffer linkedBuffer = LinkedBuffer.allocate();
    ProtobufOutput output = new ProtobufOutput(linkedBuffer);
    schema.writeObject(output, value);
    try (BufferOutputStream os = new BufferOutputStream()) {
        LinkedBuffer.writeTo(os, linkedBuffer);
        headerBuffer = os.getBuffer();
    }
    return headerBuffer;
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) Buffer(io.vertx.core.buffer.Buffer) LinkedBuffer(io.protostuff.LinkedBuffer) BufferOutputStream(org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream) ProtobufOutput(io.protostuff.ProtobufOutput)

Example 17 with LinkedBuffer

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

the class HighwayOutputStream method write.

public void write(WrapSchema headerSchema, Object header, WrapSchema bodySchema, Object body) throws Exception {
    // 写protobuf数据
    LinkedBuffer linkedBuffer = LinkedBuffer.allocate();
    ProtobufOutput output = new ProtobufOutput(linkedBuffer);
    // 写header
    if (headerSchema != null) {
        headerSchema.writeObject(output, header, protobufFeature);
    }
    int headerSize = output.getSize();
    // void时bodySchema为null
    if (bodySchema != null) {
        bodySchema.writeObject(output, body, protobufFeature);
    }
    writeLength(output.getSize(), headerSize);
    LinkedBuffer.writeTo(this, linkedBuffer);
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) ProtobufOutput(io.protostuff.ProtobufOutput)

Example 18 with LinkedBuffer

use of io.protostuff.LinkedBuffer 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 19 with LinkedBuffer

use of io.protostuff.LinkedBuffer 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 20 with LinkedBuffer

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

the class Protostuff method serialize.

@Override
public byte[] serialize(Object model) throws IOException {
    LinkedBuffer linkedBuffer = LinkedBuffer.allocate();
    ProtobufOutput output = new ProtobufOutput(linkedBuffer);
    if (model != null) {
        rootSchema.writeTo(output, (Root) model);
    }
    return output.toByteArray();
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) ProtobufOutput(io.protostuff.ProtobufOutput)

Aggregations

LinkedBuffer (io.protostuff.LinkedBuffer)21 ProtobufOutput (io.protostuff.ProtobufOutput)19 Test (org.junit.Test)7 IOException (java.io.IOException)4 BufferOutputStream (io.servicecomb.foundation.vertx.stream.BufferOutputStream)3 RpcException (com.jim.framework.rpc.exception.RpcException)2 ByteArrayInput (io.protostuff.ByteArrayInput)2 Schema (io.protostuff.Schema)2 RuntimeSchema (io.protostuff.runtime.RuntimeSchema)2 Buffer (io.vertx.core.buffer.Buffer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Input (io.protostuff.Input)1 ModelProtostuff (io.protostuff.runtime.model.ModelProtostuff)1 MultiWrapper (io.servicecomb.common.javassist.MultiWrapper)1 LoginRequest (io.servicecomb.transport.highway.message.LoginRequest)1 RequestHeader (io.servicecomb.transport.highway.message.RequestHeader)1 List (java.util.List)1 MultiWrapper (org.apache.servicecomb.common.javassist.MultiWrapper)1 BufferOutputStream (org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream)1