Search in sources :

Example 1 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class DeprecatedExchangeCodec method decodeBody.

protected Object decodeBody(Channel channel, InputStream is, byte[] header) throws IOException {
    byte flag = header[2], proto = (byte) (flag & SERIALIZATION_MASK);
    // get request id.
    long id = Bytes.bytes2long(header, 4);
    if ((flag & FLAG_REQUEST) == 0) {
        // decode response.
        Response res = new Response(id);
        if ((flag & FLAG_EVENT) != 0) {
            res.setEvent(true);
        }
        // get status.
        byte status = header[3];
        res.setStatus(status);
        try {
            ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
            if (status == Response.OK) {
                Object data;
                if (res.isHeartbeat()) {
                    data = decodeHeartbeatData(channel, in);
                } else if (res.isEvent()) {
                    data = decodeEventData(channel, in);
                } else {
                    data = decodeResponseData(channel, in, getRequestData(id));
                }
                res.setResult(data);
            } else {
                res.setErrorMessage(in.readUTF());
            }
        } catch (Throwable t) {
            res.setStatus(Response.CLIENT_ERROR);
            res.setErrorMessage(StringUtils.toString(t));
        }
        return res;
    } else {
        // decode request.
        Request req = new Request(id);
        req.setVersion(Version.getProtocolVersion());
        req.setTwoWay((flag & FLAG_TWOWAY) != 0);
        if ((flag & FLAG_EVENT) != 0) {
            req.setEvent(true);
        }
        try {
            ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
            Object data;
            if (req.isHeartbeat()) {
                data = decodeHeartbeatData(channel, in);
            } else if (req.isEvent()) {
                data = decodeEventData(channel, in);
            } else {
                data = decodeRequestData(channel, in);
            }
            req.setData(data);
        } catch (Throwable t) {
            // bad request
            req.setBroken(true);
            req.setData(t);
        }
        return req;
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput)

Example 2 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class DubboCodec method decodeBody.

@Override
protected Object decodeBody(Channel channel, InputStream is, byte[] header) throws IOException {
    byte flag = header[2], proto = (byte) (flag & SERIALIZATION_MASK);
    // get request id.
    long id = Bytes.bytes2long(header, 4);
    if ((flag & FLAG_REQUEST) == 0) {
        // decode response.
        Response res = new Response(id);
        if ((flag & FLAG_EVENT) != 0) {
            res.setEvent(true);
        }
        // get status.
        byte status = header[3];
        res.setStatus(status);
        try {
            if (status == Response.OK) {
                Object data;
                if (res.isEvent()) {
                    byte[] eventPayload = CodecSupport.getPayload(is);
                    if (CodecSupport.isHeartBeat(eventPayload, proto)) {
                        // heart beat response data is always null;
                        data = null;
                    } else {
                        ObjectInput in = CodecSupport.deserialize(channel.getUrl(), new ByteArrayInputStream(eventPayload), proto);
                        data = decodeEventData(channel, in, eventPayload);
                    }
                } else {
                    DecodeableRpcResult result;
                    if (channel.getUrl().getParameter(DECODE_IN_IO_THREAD_KEY, DEFAULT_DECODE_IN_IO_THREAD)) {
                        result = new DecodeableRpcResult(channel, res, is, (Invocation) getRequestData(id), proto);
                        result.decode();
                    } else {
                        result = new DecodeableRpcResult(channel, res, new UnsafeByteArrayInputStream(readMessageData(is)), (Invocation) getRequestData(id), proto);
                    }
                    data = result;
                }
                res.setResult(data);
            } else {
                ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
                res.setErrorMessage(in.readUTF());
            }
        } catch (Throwable t) {
            if (log.isWarnEnabled()) {
                log.warn("Decode response failed: " + t.getMessage(), t);
            }
            res.setStatus(Response.CLIENT_ERROR);
            res.setErrorMessage(StringUtils.toString(t));
        }
        return res;
    } else {
        // decode request.
        Request req = new Request(id);
        req.setVersion(Version.getProtocolVersion());
        req.setTwoWay((flag & FLAG_TWOWAY) != 0);
        if ((flag & FLAG_EVENT) != 0) {
            req.setEvent(true);
        }
        try {
            Object data;
            if (req.isEvent()) {
                byte[] eventPayload = CodecSupport.getPayload(is);
                if (CodecSupport.isHeartBeat(eventPayload, proto)) {
                    // heart beat response data is always null;
                    data = null;
                } else {
                    ObjectInput in = CodecSupport.deserialize(channel.getUrl(), new ByteArrayInputStream(eventPayload), proto);
                    data = decodeEventData(channel, in, eventPayload);
                }
            } else {
                DecodeableRpcInvocation inv;
                if (channel.getUrl().getParameter(DECODE_IN_IO_THREAD_KEY, DEFAULT_DECODE_IN_IO_THREAD)) {
                    inv = new DecodeableRpcInvocation(channel, req, is, proto);
                    inv.decode();
                } else {
                    inv = new DecodeableRpcInvocation(channel, req, new UnsafeByteArrayInputStream(readMessageData(is)), proto);
                }
                data = inv;
            }
            req.setData(data);
        } catch (Throwable t) {
            if (log.isWarnEnabled()) {
                log.warn("Decode request failed: " + t.getMessage(), t);
            }
            // bad request
            req.setBroken(true);
            req.setData(t);
        }
        return req;
    }
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Request(org.apache.dubbo.remoting.exchange.Request) UnsafeByteArrayInputStream(org.apache.dubbo.common.io.UnsafeByteArrayInputStream) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsafeByteArrayInputStream(org.apache.dubbo.common.io.UnsafeByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput)

Example 3 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class AbstractProtobufSerializationTest method test_Long.

@Test
public void test_Long() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeLong(123L);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertEquals(123L, deserialize.readLong());
    try {
        deserialize.readLong();
        fail();
    } catch (Exception expected) {
        expected.printStackTrace();
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Test(org.junit.jupiter.api.Test)

Example 4 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class AbstractProtobufSerializationTest method test_BytesRange.

@Test
public void test_BytesRange() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeBytes("123中华人民共和国-新疆维吾尔自治区".getBytes(), 1, 9);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    byte[] expectedArray = new byte[9];
    System.arraycopy("123中华人民共和国-新疆维吾尔自治区".getBytes(), 1, expectedArray, 0, expectedArray.length);
    assertArrayEquals(expectedArray, deserialize.readBytes());
    try {
        deserialize.readBytes();
        fail();
    } catch (Exception expected) {
        expected.printStackTrace();
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Test(org.junit.jupiter.api.Test)

Example 5 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class AbstractProtobufSerializationTest method test_Byte_Multi.

@Test
public void test_Byte_Multi() throws Exception {
    byte[] array = new byte[100];
    random.nextBytes(array);
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    for (byte b : array) {
        objectOutput.writeByte(b);
    }
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    for (byte b : array) {
        assertEquals(b, deserialize.readByte());
    }
    try {
        deserialize.readByte();
        fail();
    } catch (Exception expected) {
        expected.printStackTrace();
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)84 ByteArrayInputStream (java.io.ByteArrayInputStream)76 Test (org.junit.jupiter.api.Test)74 ObjectOutput (org.apache.dubbo.common.serialize.ObjectOutput)73 IOException (java.io.IOException)42 AbstractSerializationPersonFailTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonFailTest)7 AbstractSerializationPersonOkTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonOkTest)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)3 Cleanable (org.apache.dubbo.common.serialize.Cleanable)3 BigPerson (org.apache.dubbo.common.serialize.model.person.BigPerson)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)2 URL (org.apache.dubbo.common.URL)2 Serialization (org.apache.dubbo.common.serialize.Serialization)2 BizException (org.apache.dubbo.common.serialize.model.BizException)2 BizExceptionNoDefaultConstructor (org.apache.dubbo.common.serialize.model.BizExceptionNoDefaultConstructor)2 Request (org.apache.dubbo.remoting.exchange.Request)2 Response (org.apache.dubbo.remoting.exchange.Response)2