Java Code Examples for com.alibaba.dubbo.common.serialize.ObjectOutput#flushBuffer()

The following examples show how to use com.alibaba.dubbo.common.serialize.ObjectOutput#flushBuffer() . 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 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(int[].class));

    try {
        deserialize.readObject(int[].class);
        fail();
    } catch (IOException expected) {
    }
}
 
Example 2
Source File: AbstractSerializationTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test(timeout=3000)
public void test_LoopReference() throws Exception {
    Map<String, Object> map= new HashMap<String, Object>();
    map.put("k1", "v1");
    map.put("self", map);
    

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

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
            byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    @SuppressWarnings("unchecked")
    Map<String, Object> output = (Map<String, Object>) deserialize.readObject();
    
    assertEquals("v1", output.get("k1"));
    assertSame(output, output.get("self"));
}
 
Example 3
Source File: Hessian2SerializationTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_StringArray_withType() throws Exception {
    
    String[] data = new String[] { "1", "b" };
    

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

    try {
        deserialize.readObject(String[].class);
        fail();
    } 
    catch (ArrayIndexOutOfBoundsException e) {} 
    // NOTE: Hessian2抛出了ArrayIndexOutOfBoundsException 而不是 IOException!!
    // 容忍这个问题!!
}
 
Example 4
Source File: Hessian2SerializationTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_StringArray_withType() throws Exception {

    String[] data = new String[]{"1", "b"};


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

    try {
        deserialize.readObject(String[].class);
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
    // NOTE: Hessian2 throws ArrayIndexOutOfBoundsException instead of IOException, let's live with this.
}
 
Example 5
Source File: AbstractSerializationTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Bytes() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeBytes("123中华人民共和国".getBytes());
    objectOutput.flushBuffer();

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

    assertArrayEquals("123中华人民共和国".getBytes(), deserialize.readBytes());

    try {
        deserialize.readBytes();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 6
Source File: AbstractSerializationTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_charArray_withType() throws Exception {
    char[] data = new char[] { 'a', '中', '无' };
    
    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, (char[]) deserialize.readObject(char[].class));
    
    try {
        deserialize.readObject(char[].class);
        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_Float() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeFloat(1.28F);
    objectOutput.flushBuffer();

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

    assertTrue(1.28F == deserialize.readFloat());

    try {
        deserialize.readFloat();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 8
Source File: AbstractSerializationTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
<T> void assertObjectArray(T[] data, Class<T[]> clazz) 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);

    assertArrayEquals(data, clazz.cast(deserialize.readObject()));

    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 9
Source File: AbstractSerializationTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void test_charArray() throws Exception {
    char[] data = new char[] { 'a', '中', '无' };
    
    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, (char[]) 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_StringArray_withType() throws Exception {
    
    String[] data = new String[] { "1", "b" };
    

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

    try {
        deserialize.readObject(String[].class);
        fail();
    } 
    catch (ArrayIndexOutOfBoundsException e) {} 
    // NOTE: Hessian2抛出了ArrayIndexOutOfBoundsException 而不是 IOException!!
    // 容忍这个问题!!
}
 
Example 11
Source File: AbstractSerializationTest.java    From dubbo3 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 12
Source File: Hessian2SerializationTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@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.
}
 
Example 13
Source File: Hessian2SerializationTest.java    From dubbox 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 14
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_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(float[].class), 0.0001F);

    try {
        deserialize.readObject(float[].class);
        fail();
    } catch (IOException expected) {
    }
}
 
Example 15
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 16
Source File: AbstractSerializationTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Byte() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeByte((byte) 123);
    objectOutput.flushBuffer();

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

    assertEquals((byte) 123, deserialize.readByte());

    try {
        deserialize.readByte();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 17
Source File: AbstractSerializationTest.java    From dubbo-2.6.5 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(int[].class));

    try {
        deserialize.readObject(int[].class);
        fail();
    } catch (IOException expected) {
    }
}
 
Example 18
Source File: AbstractSerializationTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_UtfString() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeUTF("123中华人民共和国");
    objectOutput.flushBuffer();

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

    assertEquals("123中华人民共和国", deserialize.readUTF());

    try {
        deserialize.readUTF();
        fail();
    } catch (IOException expected) {
    }
}
 
Example 19
Source File: DeprecatedExchangeCodec.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
protected void encodeRequest(Channel channel, OutputStream os, Request req) throws IOException {
    Serialization serialization = CodecSupport.getSerialization(channel.getUrl());
    // header.
    byte[] header = new byte[HEADER_LENGTH];
    // set magic number.
    Bytes.short2bytes(MAGIC, header);

    // set request and serialization flag.
    header[2] = (byte) (FLAG_REQUEST | serialization.getContentTypeId());

    if (req.isTwoWay()) header[2] |= FLAG_TWOWAY;
    if (req.isEvent()) header[2] |= FLAG_EVENT;

    // set request id.
    Bytes.long2bytes(req.getId(), header, 4);

    // encode request data.
    UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
    ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
    if (req.isEvent()) {
        encodeEventData(channel, out, req.getData());
    } else {
        encodeRequestData(channel, out, req.getData());
    }
    out.flushBuffer();
    bos.flush();
    bos.close();
    byte[] data = bos.toByteArray();
    checkPayload(channel, data.length);
    Bytes.int2bytes(data.length, header, 12);

    // write
    os.write(header); // write header.
    os.write(data); // write data.
}
 
Example 20
Source File: ExchangeCodec.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request req) throws IOException {
//        从url中serialization这个key值获取序列化方式,默认值是hessian2
        Serialization serialization = getSerialization(channel);
        // header.
        byte[] header = new byte[HEADER_LENGTH];
        // set magic number.
        Bytes.short2bytes(MAGIC, header);

        // set request and serialization flag.
        header[2] = (byte) (FLAG_REQUEST | serialization.getContentTypeId());

//        是否需要返回
        if (req.isTwoWay()) header[2] |= FLAG_TWOWAY;
        if (req.isEvent()) header[2] |= FLAG_EVENT;

        // set request id.
        Bytes.long2bytes(req.getId(), header, 4);

        // encode request data.
        int savedWriteIndex = buffer.writerIndex();
        buffer.writerIndex(savedWriteIndex + HEADER_LENGTH);
        ChannelBufferOutputStream bos = new ChannelBufferOutputStream(buffer);
//        序列化请求数据
        ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
        if (req.isEvent()) {
            encodeEventData(channel, out, req.getData());
        } else {
            encodeRequestData(channel, out, req.getData(), req.getVersion());
        }
        out.flushBuffer();
        if (out instanceof Cleanable) {
            ((Cleanable) out).cleanup();
        }
        bos.flush();
        bos.close();
        int len = bos.writtenBytes();
//        检查服务端支持的网络通讯数据大小,默认8m
        checkPayload(channel, len);
        Bytes.int2bytes(len, header, 12);

        // write
        buffer.writerIndex(savedWriteIndex);
        buffer.writeBytes(header); // write header.
        buffer.writerIndex(savedWriteIndex + HEADER_LENGTH + len);
    }