Search in sources :

Example 46 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class Hessian2SerializationTest method test_intArray_withType.

@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 throws ArrayIndexOutOfBoundsException instead of IOException, let's live with this.
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Test(org.junit.Test)

Example 47 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class Hessian2SerializationTest method test_floatArray_withType.

@Test
public void test_floatArray_withType() 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(float[].class);
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
// NOTE: Hessian2 throws ArrayIndexOutOfBoundsException instead of IOException, let's live with this.
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Test(org.junit.Test)

Example 48 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class Hessian2SerializationTest method test_longArray_withType.

@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());
    try {
        deserialize.readObject(long[].class);
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
// NOTE: Hessian2 throws ArrayIndexOutOfBoundsException instead of IOException, let's live with this.
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Test(org.junit.Test)

Example 49 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class Hessian2SerializationTest method test_boolArray_withType.

// Hessian2
@Test
public void test_boolArray_withType() throws Exception {
    boolean[] data = new boolean[] { true, false, true };
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject(boolean[].class)));
    try {
        deserialize.readObject(boolean[].class);
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
// NOTE: Hessian2 throws ArrayIndexOutOfBoundsException instead of IOException, let's live with this.
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Test(org.junit.Test)

Example 50 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class TransportCodec method encode.

public void encode(Channel channel, ChannelBuffer buffer, Object message) throws IOException {
    OutputStream output = new ChannelBufferOutputStream(buffer);
    ObjectOutput objectOutput = getSerialization(channel).serialize(channel.getUrl(), output);
    encodeData(channel, objectOutput, message);
    objectOutput.flushBuffer();
    if (objectOutput instanceof Cleanable) {
        ((Cleanable) objectOutput).cleanup();
    }
}
Also used : ChannelBufferOutputStream(com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream) ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) OutputStream(java.io.OutputStream) ChannelBufferOutputStream(com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream) Cleanable(com.alibaba.dubbo.common.serialize.Cleanable)

Aggregations

ObjectOutput (com.alibaba.dubbo.common.serialize.ObjectOutput)106 ObjectInput (com.alibaba.dubbo.common.serialize.ObjectInput)92 ByteArrayInputStream (java.io.ByteArrayInputStream)92 Test (org.junit.Test)90 IOException (java.io.IOException)59 Person (com.alibaba.dubbo.common.model.Person)7 NotSerializableException (java.io.NotSerializableException)7 HashMap (java.util.HashMap)5 BizException (com.alibaba.dubbo.common.model.BizException)4 BizExceptionNoDefaultConstructor (com.alibaba.dubbo.common.model.BizExceptionNoDefaultConstructor)4 Serialization (com.alibaba.dubbo.common.serialize.Serialization)4 LinkedHashMap (java.util.LinkedHashMap)4 UnsafeByteArrayOutputStream (com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream)3 Cleanable (com.alibaba.dubbo.common.serialize.Cleanable)3 ChannelBufferOutputStream (com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream)3 ArrayList (java.util.ArrayList)3 URL (com.alibaba.dubbo.common.URL)2 MediaContent (com.alibaba.dubbo.common.model.media.MediaContent)2 BigPerson (com.alibaba.dubbo.common.model.person.BigPerson)2 RemotingException (com.alibaba.dubbo.remoting.RemotingException)2