Java Code Examples for com.alibaba.dubbo.common.serialize.ObjectInput#readObject()

The following examples show how to use com.alibaba.dubbo.common.serialize.ObjectInput#readObject() . 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: AbstractSerializationTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_longArray_withType() throws Exception {
    long[] data = new long[] { 234, 0, -1};
    
    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, (long[]) deserialize.readObject(long[].class));
    
    try {
        deserialize.readObject(long[].class);
        fail();
    } catch (IOException expected) {
    }
}
 
Example 2
Source File: AbstractSerializationTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_longArray() throws Exception {
    long[] data = new long[]{234, 0, -1};

    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, (long[]) deserialize.readObject());

    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 3
Source File: AbstractSerializationTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
<T> void assertObject(T data) throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
            byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);

    assertEquals(data, (T) deserialize.readObject());

    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 4
Source File: Hessian2SerializationTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void test_intArray_withType() throws Exception {
    int[] data = new int[] { 234, 0, -1};
    
    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, (int[]) deserialize.readObject());

    try {
        deserialize.readObject(int[].class);
        fail();
    } 
    catch (ArrayIndexOutOfBoundsException e) {} 
    // NOTE: Hessian2抛出了ArrayIndexOutOfBoundsException 而不是 IOException!!
    // 容忍这个问题!!
}
 
Example 5
Source File: Hessian2SerializationTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_shortArray_withType() throws Exception {
    short[] data = new short[] { 37, 39, 12 };
    
    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, (short[]) deserialize.readObject(short[].class));
    
    try {
        deserialize.readObject(short[].class);
        fail();
    } 
    catch (ArrayIndexOutOfBoundsException e) {} 
    // NOTE: Hessian2抛出了ArrayIndexOutOfBoundsException 而不是 IOException!!
    // 容忍这个问题!!
}
 
Example 6
Source File: AbstractSerializationTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
<T> void assertObject(T data) throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
            byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);

    assertEquals(data, (T) deserialize.readObject());

    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 7
Source File: AbstractSerializationTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_MediaContent_badStream() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(mediaContent);
    objectOutput.flushBuffer();

    byte[] byteArray = byteArrayOutputStream.toByteArray();
    for (int i = 0; i < byteArray.length; i++) {
        if(i%3 == 0) {
            byteArray[i] = (byte)~byteArray[i];
        }
    }
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    
    try {
        ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
        @SuppressWarnings("unused") // local variable, convenient for debug
        Object read = deserialize.readObject();
        fail();
    } catch (IOException expected) {
        System.out.println(expected);
    }
}
 
Example 8
Source File: AbstractSerializationTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_floatArray() throws Exception {
    float[] data = new float[]{37F, -3.14F, 123456.7F};

    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, (float[]) deserialize.readObject(), 0.0001F);

    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 9
Source File: AbstractSerializationTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_longArray() throws Exception {
    long[] data = new long[]{234, 0, -1};

    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, (long[]) deserialize.readObject());

    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 10
Source File: Hessian2SerializationTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_intArray_withType() throws Exception {
    int[] data = new int[] { 234, 0, -1};
    
    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, (int[]) deserialize.readObject());

    try {
        deserialize.readObject(int[].class);
        fail();
    } 
    catch (ArrayIndexOutOfBoundsException e) {} 
    // NOTE: Hessian2抛出了ArrayIndexOutOfBoundsException 而不是 IOException!!
    // 容忍这个问题!!
}
 
Example 11
Source File: AbstractSerializationTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_MediaContent_badStream() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(mediaContent);
    objectOutput.flushBuffer();

    byte[] byteArray = byteArrayOutputStream.toByteArray();
    for (int i = 0; i < byteArray.length; i++) {
        if (i % 3 == 0) {
            byteArray[i] = (byte) ~byteArray[i];
        }
    }
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);

    try {
        ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
        @SuppressWarnings("unused") // local variable, convenient for debug
                Object read = deserialize.readObject();
        fail();
    } catch (IOException expected) {
        System.out.println(expected);
    }
}
 
Example 12
Source File: DeprecatedExchangeCodec.java    From dubbox with Apache License 2.0 5 votes vote down vote up
protected Object decodeResponseData(ObjectInput in) throws IOException {
    try {
        return in.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException(StringUtils.toString("Read object failed.", e));
    }
}
 
Example 13
Source File: AbstractSerializationTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Test
public void test_BizException_WithType() throws Exception {
    BizException e = new BizException("Hello");

    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(e);
    objectOutput.flushBuffer();

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
            byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);

    Object read = deserialize.readObject(BizException.class);
    assertEquals("Hello", ((BizException) read).getMessage());
}
 
Example 14
Source File: ExchangeCodec.java    From dubbox with Apache License 2.0 5 votes vote down vote up
protected Object decodeRequestData(ObjectInput in) throws IOException {
    try {
        return in.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException(StringUtils.toString("Read object failed.", e));
    }
}
 
Example 15
Source File: ExchangeCodec.java    From dubbox with Apache License 2.0 5 votes vote down vote up
protected Object decodeEventData(Channel channel, ObjectInput in) throws IOException {
    try {
        return in.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException(StringUtils.toString("Read object failed.", e));
    }
}
 
Example 16
Source File: DeprecatedExchangeCodec.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
protected Object decodeRequestData(ObjectInput in) throws IOException {
    try {
        return in.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException(StringUtils.toString("Read object failed.", e));
    }
}
 
Example 17
Source File: AbstractSerializationTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test
public void test_BizExceptionNoDefaultConstructor() throws Exception {
    BizExceptionNoDefaultConstructor e = new BizExceptionNoDefaultConstructor("Hello");
    
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(e);
    objectOutput.flushBuffer();
    
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
            byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    
    Object read = deserialize.readObject();
    assertEquals("Hello", ((BizExceptionNoDefaultConstructor) read).getMessage());
}
 
Example 18
Source File: AbstractSerializationTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Test
public void test_LinkedHashMap() throws Exception {
    LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();
    data.put("1", "a");
    data.put("2", "b");

    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
            byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);

    Object read = deserialize.readObject();
    assertTrue(read instanceof LinkedHashMap);
    @SuppressWarnings("unchecked")
    String key1 = ((LinkedHashMap<String, String>)read).entrySet().iterator().next().getKey();
    assertEquals("1", key1);
    
    assertEquals(data, read);

    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 19
Source File: AbstractSerializationTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void test_LinkedHashMap() throws Exception {
    LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();
    data.put("1", "a");
    data.put("2", "b");

    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
            byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);

    Object read = deserialize.readObject();
    assertTrue(read instanceof LinkedHashMap);
    @SuppressWarnings("unchecked")
    String key1 = ((LinkedHashMap<String, String>)read).entrySet().iterator().next().getKey();
    assertEquals("1", key1);
    
    assertEquals(data, read);

    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 20
Source File: TransportCodec.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
protected Object decodeData(ObjectInput input) throws IOException {
    try {
        return input.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException("ClassNotFoundException: " + StringUtils.toString(e));
    }
}