Search in sources :

Example 81 with ObjectInput

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

the class Hessian2PersonOkTest method test_doubleArray_withType.

@Test
public void test_doubleArray_withType() throws Exception {
    double[] data = new double[] { 37D, -3.14D, 123456.7D };
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertArrayEquals(data, (double[]) deserialize.readObject(double[].class), 0.0001);
    try {
        deserialize.readObject(double[].class);
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
// NOTE: Hessian2 throws ArrayIndexOutOfBoundsException instead of IOException, let's live with this.
}
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) AbstractSerializationPersonOkTest(org.apache.dubbo.common.serialize.base.AbstractSerializationPersonOkTest)

Example 82 with ObjectInput

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

the class SerializationTest method testObjectInput.

@Test
public void testObjectInput() throws IOException {
    ObjectInput objectInput = mySerialization.deserialize(null, mock(InputStream.class));
    assertThat(objectInput, Matchers.<ObjectInput>instanceOf(MyObjectInput.class));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Test(org.junit.jupiter.api.Test)

Example 83 with ObjectInput

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

the class DecodeableRpcInvocation method decode.

@Override
public Object decode(Channel channel, InputStream input) throws IOException {
    ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType).deserialize(channel.getUrl(), input);
    this.put(SERIALIZATION_ID_KEY, serializationType);
    String dubboVersion = in.readUTF();
    request.setVersion(dubboVersion);
    setAttachment(DUBBO_VERSION_KEY, dubboVersion);
    String path = in.readUTF();
    setAttachment(PATH_KEY, path);
    String version = in.readUTF();
    setAttachment(VERSION_KEY, version);
    setMethodName(in.readUTF());
    String desc = in.readUTF();
    setParameterTypesDesc(desc);
    try {
        if (ConfigurationUtils.getSystemConfiguration().getBoolean(SERIALIZATION_SECURITY_CHECK_KEY, false)) {
            CodecSupport.checkSerialization(path, version, serializationType);
        }
        Object[] args = DubboCodec.EMPTY_OBJECT_ARRAY;
        Class<?>[] pts = DubboCodec.EMPTY_CLASS_ARRAY;
        if (desc.length() > 0) {
            // if (RpcUtils.isGenericCall(path, getMethodName()) || RpcUtils.isEcho(path, getMethodName())) {
            // pts = ReflectUtils.desc2classArray(desc);
            // } else {
            ServiceRepository repository = ApplicationModel.getServiceRepository();
            ServiceDescriptor serviceDescriptor = repository.lookupService(path);
            if (serviceDescriptor != null) {
                MethodDescriptor methodDescriptor = serviceDescriptor.getMethod(getMethodName(), desc);
                if (methodDescriptor != null) {
                    pts = methodDescriptor.getParameterClasses();
                    this.setReturnTypes(methodDescriptor.getReturnTypes());
                }
            }
            if (pts == DubboCodec.EMPTY_CLASS_ARRAY) {
                if (!RpcUtils.isGenericCall(desc, getMethodName()) && !RpcUtils.isEcho(desc, getMethodName())) {
                    throw new IllegalArgumentException("Service not found:" + path + ", " + getMethodName());
                }
                pts = ReflectUtils.desc2classArray(desc);
            }
            // }
            args = new Object[pts.length];
            for (int i = 0; i < args.length; i++) {
                try {
                    args[i] = in.readObject(pts[i]);
                } catch (Exception e) {
                    if (log.isWarnEnabled()) {
                        log.warn("Decode argument failed: " + e.getMessage(), e);
                    }
                }
            }
        }
        setParameterTypes(pts);
        Map<String, Object> map = in.readAttachments();
        if (map != null && map.size() > 0) {
            Map<String, Object> attachment = getObjectAttachments();
            if (attachment == null) {
                attachment = new HashMap<>();
            }
            attachment.putAll(map);
            setObjectAttachments(attachment);
        }
        // decode argument ,may be callback
        for (int i = 0; i < args.length; i++) {
            args[i] = decodeInvocationArgument(channel, this, pts, i, args[i]);
        }
        setArguments(args);
        String targetServiceName = buildKey(getAttachment(PATH_KEY), getAttachment(GROUP_KEY), getAttachment(VERSION_KEY));
        setTargetServiceUniqueName(targetServiceName);
    } catch (ClassNotFoundException e) {
        throw new IOException(StringUtils.toString("Read invocation data failed.", e));
    } finally {
        if (in instanceof Cleanable) {
            ((Cleanable) in).cleanup();
        }
    }
    return this;
}
Also used : IOException(java.io.IOException) ServiceRepository(org.apache.dubbo.rpc.model.ServiceRepository) MethodDescriptor(org.apache.dubbo.rpc.model.MethodDescriptor) IOException(java.io.IOException) ServiceDescriptor(org.apache.dubbo.rpc.model.ServiceDescriptor) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Cleanable(org.apache.dubbo.common.serialize.Cleanable)

Example 84 with ObjectInput

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

the class DecodeableRpcResult method decode.

@Override
public Object decode(Channel channel, InputStream input) throws IOException {
    if (log.isDebugEnabled()) {
        Thread thread = Thread.currentThread();
        log.debug("Decoding in thread -- [" + thread.getName() + "#" + thread.getId() + "]");
    }
    ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType).deserialize(channel.getUrl(), input);
    byte flag = in.readByte();
    switch(flag) {
        case DubboCodec.RESPONSE_NULL_VALUE:
            break;
        case DubboCodec.RESPONSE_VALUE:
            handleValue(in);
            break;
        case DubboCodec.RESPONSE_WITH_EXCEPTION:
            handleException(in);
            break;
        case DubboCodec.RESPONSE_NULL_VALUE_WITH_ATTACHMENTS:
            handleAttachment(in);
            break;
        case DubboCodec.RESPONSE_VALUE_WITH_ATTACHMENTS:
            handleValue(in);
            handleAttachment(in);
            break;
        case DubboCodec.RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS:
            handleException(in);
            handleAttachment(in);
            break;
        default:
            throw new IOException("Unknown result flag, expect '0' '1' '2' '3' '4' '5', but received: " + flag);
    }
    if (in instanceof Cleanable) {
        ((Cleanable) in).cleanup();
    }
    return this;
}
Also used : ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) Cleanable(org.apache.dubbo.common.serialize.Cleanable)

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