com.alibaba.dubbo.remoting.Codec2 Java Examples

The following examples show how to use com.alibaba.dubbo.remoting.Codec2. 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: DubboCountCodec.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    int save = buffer.readerIndex();
    MultiMessage result = MultiMessage.create();
    do {
        Object obj = codec.decode(channel, buffer);
        if (Codec2.DecodeResult.NEED_MORE_INPUT == obj) {
            buffer.readerIndex(save);
            break;
        } else {
            result.addMessage(obj);
            logMessageLength(obj, buffer.readerIndex() - save);
            save = buffer.readerIndex();
        }
    } while (true);
    if (result.isEmpty()) {
        return Codec2.DecodeResult.NEED_MORE_INPUT;
    }
    if (result.size() == 1) {
        return result.get(0);
    }
    return result;
}
 
Example #2
Source File: TelnetCodecTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private void testDecode_PersonWithEnterByte(byte[] enterbytes ,boolean isNeedmore) throws IOException{
    //init channel
    Channel channel = getServerSideChannel(url);
    //init request string
    Person request = new Person();
    byte[] newbuf = join(objectToByte(request), enterbytes);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(newbuf);

    //decode
    Object obj = codec.decode(channel, buffer);
    if (isNeedmore){
        Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT , obj);
    }else {
        Assert.assertTrue("return must string ", obj instanceof String);
    }
}
 
Example #3
Source File: TelnetCodecTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testDecode_UPorDOWN_WithError() throws IOException{
    url = url.addParameter(AbstractMockChannel.ERROR_WHEN_SEND, Boolean.TRUE.toString());
    
    //init channel
    AbstractMockChannel channel = getServerSideChannel(url);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, null);
    
    String request1 = "aaa\n"; 
    Object expected1 = "aaa";
    //init history 
    testDecode_assertEquals(channel, request1, expected1, null);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1);
    
    url = url.removeParameter(AbstractMockChannel.ERROR_WHEN_SEND);
}
 
Example #4
Source File: DubboCountCodec.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    int save = buffer.readerIndex();
    MultiMessage result = MultiMessage.create();
    do {
        Object obj = codec.decode(channel, buffer);
        if (Codec2.DecodeResult.NEED_MORE_INPUT == obj) {
            buffer.readerIndex(save);
            break;
        } else {
            result.addMessage(obj);
            logMessageLength(obj, buffer.readerIndex() - save);
            save = buffer.readerIndex();
        }
    } while (true);
    if (result.isEmpty()) {
        return Codec2.DecodeResult.NEED_MORE_INPUT;
    }
    if (result.size() == 1) {
        return result.get(0);
    }
    return result;
}
 
Example #5
Source File: TelnetCodecTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
private void testDecode_PersonWithEnterByte(byte[] enterbytes ,boolean isNeedmore) throws IOException{
    //init channel
    Channel channel = getServerSideChannel(url);
    //init request string
    Person request = new Person();
    byte[] newbuf = join(objectToByte(request), enterbytes);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(newbuf);

    //decode
    Object obj = codec.decode(channel, buffer);
    if (isNeedmore){
        Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT , obj);
    }else {
        Assert.assertTrue("return must string ", obj instanceof String);
    }
}
 
Example #6
Source File: DubboCountCodec.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
    public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
        int save = buffer.readerIndex();
        MultiMessage result = MultiMessage.create();
        do {
//            com.alibaba.dubbo.remoting.transport.netty4.NettyCodecAdapter.InternalDecoder.decode()
            Object obj = codec.decode(channel, buffer);
            if (Codec2.DecodeResult.NEED_MORE_INPUT == obj) {
                buffer.readerIndex(save);
                break;
            } else {
                result.addMessage(obj);
                logMessageLength(obj, buffer.readerIndex() - save);
                save = buffer.readerIndex();
            }
        } while (true);
        if (result.isEmpty()) {
            return Codec2.DecodeResult.NEED_MORE_INPUT;
        }
        if (result.size() == 1) {
            return result.get(0);
        }
        return result;
    }
 
Example #7
Source File: DubboCountCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    int save = buffer.readerIndex();
    MultiMessage result = MultiMessage.create();
    do {
        Object obj = codec.decode(channel, buffer);
        if (Codec2.DecodeResult.NEED_MORE_INPUT == obj) {
            buffer.readerIndex(save);
            break;
        } else {
            result.addMessage(obj);
            logMessageLength(obj, buffer.readerIndex() - save);
            save = buffer.readerIndex();
        }
    } while (true);
    if (result.isEmpty()) {
        return Codec2.DecodeResult.NEED_MORE_INPUT;
    }
    if (result.size() == 1) {
        return result.get(0);
    }
    return result;
}
 
Example #8
Source File: TelnetCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testDecode_UPorDOWN_WithError() throws IOException {
    url = url.addParameter(AbstractMockChannel.ERROR_WHEN_SEND, Boolean.TRUE.toString());

    //init channel
    AbstractMockChannel channel = getServerSideChannel(url);

    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, null);

    String request1 = "aaa\n";
    Object expected1 = "aaa";
    //init history
    testDecode_assertEquals(channel, request1, expected1, null);

    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1);

    url = url.removeParameter(AbstractMockChannel.ERROR_WHEN_SEND);
}
 
Example #9
Source File: TelnetCodecTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
private void testDecode_PersonWithEnterByte(byte[] enterbytes ,boolean isNeedmore) throws IOException{
    //init channel
    Channel channel = getServerSideChannel(url);
    //init request string
    Person request = new Person();
    byte[] newbuf = join(objectToByte(request), enterbytes);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(newbuf);

    //decode
    Object obj = codec.decode(channel, buffer);
    if (isNeedmore){
        Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT , obj);
    }else {
        Assert.assertTrue("return must string ", obj instanceof String);
    }
}
 
Example #10
Source File: TelnetCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
private void testDecode_PersonWithEnterByte(byte[] enterbytes, boolean isNeedmore) throws IOException {
    //init channel
    Channel channel = getServerSideChannel(url);
    //init request string
    Person request = new Person();
    byte[] newbuf = join(objectToByte(request), enterbytes);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(newbuf);

    //decode
    Object obj = codec.decode(channel, buffer);
    if (isNeedmore) {
        Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT, obj);
    } else {
        Assert.assertTrue("return must string ", obj instanceof String);
    }
}
 
Example #11
Source File: TelnetCodecTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testDecode_UPorDOWN_WithError() throws IOException{
    url = url.addParameter(AbstractMockChannel.ERROR_WHEN_SEND, Boolean.TRUE.toString());
    
    //init channel
    AbstractMockChannel channel = getServerSideChannel(url);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, null);
    
    String request1 = "aaa\n"; 
    Object expected1 = "aaa";
    //init history 
    testDecode_assertEquals(channel, request1, expected1, null);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1);
    
    url = url.removeParameter(AbstractMockChannel.ERROR_WHEN_SEND);
}
 
Example #12
Source File: TelnetCodecTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private void testDecode_PersonWithEnterByte(byte[] enterbytes ,boolean isNeedmore) throws IOException{
    //init channel
    Channel channel = getServerSideChannel(url);
    //init request string
    Person request = new Person();
    byte[] newbuf = join(objectToByte(request), enterbytes);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(newbuf);

    //decode
    Object obj = codec.decode(channel, buffer);
    if (isNeedmore){
        Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT , obj);
    }else {
        Assert.assertTrue("return must string ", obj instanceof String);
    }
}
 
Example #13
Source File: TelnetCodecTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testDecode_UPorDOWN_WithError() throws IOException{
    url = url.addParameter(AbstractMockChannel.ERROR_WHEN_SEND, Boolean.TRUE.toString());
    
    //init channel
    AbstractMockChannel channel = getServerSideChannel(url);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, null);
    
    String request1 = "aaa\n"; 
    Object expected1 = "aaa";
    //init history 
    testDecode_assertEquals(channel, request1, expected1, null);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1);
    
    url = url.removeParameter(AbstractMockChannel.ERROR_WHEN_SEND);
}
 
Example #14
Source File: TelnetCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Test()
public void testDecode_History_UP() throws IOException {
    //init channel
    AbstractMockChannel channel = getServerSideChannel(url);

    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, null);

    String request1 = "aaa\n";
    Object expected1 = "aaa";
    //init history
    testDecode_assertEquals(channel, request1, expected1, null);

    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1);
}
 
Example #15
Source File: NettyCodecAdapter.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public NettyCodecAdapter(Codec2 codec, URL url, com.alibaba.dubbo.remoting.ChannelHandler handler) {
    this.codec = codec;
    this.url = url;
    this.handler = handler;
    int b = url.getPositiveParameter(Constants.BUFFER_KEY, Constants.DEFAULT_BUFFER_SIZE);
    this.bufferSize = b >= Constants.MIN_BUFFER_SIZE && b <= Constants.MAX_BUFFER_SIZE ? b : Constants.DEFAULT_BUFFER_SIZE;
}
 
Example #16
Source File: TelnetCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecode_Backspace() throws IOException {
    //32 8 first add space and then add backspace.
    testDecode_assertEquals(new byte[]{'\b'}, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[]{32, 8}));

    // test chinese
    byte[] chineseBytes = "中".getBytes();
    byte[] request = join(chineseBytes, new byte[]{'\b'});
    testDecode_assertEquals(request, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[]{32, 32, 8, 8}));
    //There may be some problem handling chinese (negative number recognition). Ignoring this problem, the backspace key is only meaningfully input in a real telnet program.
    testDecode_assertEquals(new byte[]{'a', 'x', -1, 'x', '\b'}, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[]{32, 32, 8, 8}));
}
 
Example #17
Source File: MinaCodecAdapter.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
public MinaCodecAdapter(Codec2 codec, URL url, ChannelHandler handler) {
    this.codec = codec;
    this.url = url;
    this.handler = handler;
    int b = url.getPositiveParameter(Constants.BUFFER_KEY, Constants.DEFAULT_BUFFER_SIZE);
    this.bufferSize = b >= Constants.MIN_BUFFER_SIZE && b <= Constants.MAX_BUFFER_SIZE ? b : Constants.DEFAULT_BUFFER_SIZE;
}
 
Example #18
Source File: AbstractEndpoint.java    From dubbox with Apache License 2.0 5 votes vote down vote up
protected static Codec2 getChannelCodec(URL url) {
    String codecName = url.getParameter(Constants.CODEC_KEY, "telnet");
    if (ExtensionLoader.getExtensionLoader(Codec2.class).hasExtension(codecName)) {
        return ExtensionLoader.getExtensionLoader(Codec2.class).getExtension(codecName);
    } else {
        return new CodecAdapter(ExtensionLoader.getExtensionLoader(Codec.class)
                                           .getExtension(codecName));
    }
}
 
Example #19
Source File: MinaCodecAdapter.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public MinaCodecAdapter(Codec2 codec, URL url, ChannelHandler handler) {
    this.codec = codec;
    this.url = url;
    this.handler = handler;
    int b = url.getPositiveParameter(Constants.BUFFER_KEY, Constants.DEFAULT_BUFFER_SIZE);
    this.bufferSize = b >= Constants.MIN_BUFFER_SIZE && b <= Constants.MAX_BUFFER_SIZE ? b : Constants.DEFAULT_BUFFER_SIZE;
}
 
Example #20
Source File: TelnetCodecTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecode_Backspace() throws IOException{
    //32 8 先加空格在补退格.
    testDecode_assertEquals(new byte[]{'\b'}, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[] {32, 8}));
    
    //测试中文
    byte[] chineseBytes = "中".getBytes();
    byte[] request = join(chineseBytes, new byte[]{'\b'});
    testDecode_assertEquals(request, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[] {32, 32, 8, 8}));
    //中文会带来此问题 (-数判断) 忽略此问题,退格键只有在真的telnet程序中才输入有意义.
    testDecode_assertEquals(new byte[]{'a', 'x', -1, 'x', '\b'}, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[] {32, 32, 8, 8}));
}
 
Example #21
Source File: TelnetCodecTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecode_Backspace() throws IOException{
    //32 8 先加空格在补退格.
    testDecode_assertEquals(new byte[]{'\b'}, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[] {32, 8}));
    
    //测试中文
    byte[] chineseBytes = "中".getBytes();
    byte[] request = join(chineseBytes, new byte[]{'\b'});
    testDecode_assertEquals(request, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[] {32, 32, 8, 8}));
    //中文会带来此问题 (-数判断) 忽略此问题,退格键只有在真的telnet程序中才输入有意义.
    testDecode_assertEquals(new byte[]{'a', 'x', -1, 'x', '\b'}, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[] {32, 32, 8, 8}));
}
 
Example #22
Source File: TelnetCodecTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test()
public void testDecode_History_UP() throws IOException{
    //init channel
    AbstractMockChannel channel = getServerSideChannel(url);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, null);
    
    String request1 = "aaa\n"; 
    Object expected1 = "aaa";
    //init history 
    testDecode_assertEquals(channel, request1, expected1, null);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1);
}
 
Example #23
Source File: MinaCodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public MinaCodecAdapter(Codec2 codec, URL url, ChannelHandler handler) {
    this.codec = codec;
    this.url = url;
    this.handler = handler;
    int b = url.getPositiveParameter(Constants.BUFFER_KEY, Constants.DEFAULT_BUFFER_SIZE);
    this.bufferSize = b >= Constants.MIN_BUFFER_SIZE && b <= Constants.MAX_BUFFER_SIZE ? b : Constants.DEFAULT_BUFFER_SIZE;
}
 
Example #24
Source File: TelnetCodecTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test()
public void testDecode_History_UP() throws IOException{
    //init channel
    AbstractMockChannel channel = getServerSideChannel(url);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, null);
    
    String request1 = "aaa\n"; 
    Object expected1 = "aaa";
    //init history 
    testDecode_assertEquals(channel, request1, expected1, null);
    
    testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1);
}
 
Example #25
Source File: TelnetCodecTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecode_Backspace() throws IOException{
    //32 8 先加空格在补退格.
    testDecode_assertEquals(new byte[]{'\b'}, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[] {32, 8}));
    
    //测试中文
    byte[] chineseBytes = "中".getBytes();
    byte[] request = join(chineseBytes, new byte[]{'\b'});
    testDecode_assertEquals(request, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[] {32, 32, 8, 8}));
    //中文会带来此问题 (-数判断) 忽略此问题,退格键只有在真的telnet程序中才输入有意义.
    testDecode_assertEquals(new byte[]{'a', 'x', -1, 'x', '\b'}, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[] {32, 32, 8, 8}));
}
 
Example #26
Source File: GrizzlyCodecAdapter.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public GrizzlyCodecAdapter(Codec2 codec, URL url, ChannelHandler handler) {
    this.codec = codec;
    this.url = url;
    this.handler = handler;
    int b = url.getPositiveParameter(Constants.BUFFER_KEY, Constants.DEFAULT_BUFFER_SIZE);
    this.bufferSize = b >= Constants.MIN_BUFFER_SIZE && b <= Constants.MAX_BUFFER_SIZE ? b : Constants.DEFAULT_BUFFER_SIZE;
}
 
Example #27
Source File: GrizzlyCodecAdapter.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
public GrizzlyCodecAdapter(Codec2 codec, URL url, ChannelHandler handler) {
    this.codec = codec;
    this.url = url;
    this.handler = handler;
    int b = url.getPositiveParameter(Constants.BUFFER_KEY, Constants.DEFAULT_BUFFER_SIZE);
    this.bufferSize = b >= Constants.MIN_BUFFER_SIZE && b <= Constants.MAX_BUFFER_SIZE ? b : Constants.DEFAULT_BUFFER_SIZE;
}
 
Example #28
Source File: NettyCodecAdapter.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public NettyCodecAdapter(Codec2 codec, URL url, com.alibaba.dubbo.remoting.ChannelHandler handler) {
    this.codec = codec;
    this.url = url;
    this.handler = handler;
    int b = url.getPositiveParameter(Constants.BUFFER_KEY, Constants.DEFAULT_BUFFER_SIZE);
    this.bufferSize = b >= Constants.MIN_BUFFER_SIZE && b <= Constants.MAX_BUFFER_SIZE ? b : Constants.DEFAULT_BUFFER_SIZE;
}
 
Example #29
Source File: AbstractEndpoint.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
protected static Codec2 getChannelCodec(URL url) {
    String codecName = url.getParameter(Constants.CODEC_KEY, "telnet");
    if (ExtensionLoader.getExtensionLoader(Codec2.class).hasExtension(codecName)) {
        return ExtensionLoader.getExtensionLoader(Codec2.class).getExtension(codecName);
    } else {
        return new CodecAdapter(ExtensionLoader.getExtensionLoader(Codec.class)
                                           .getExtension(codecName));
    }
}
 
Example #30
Source File: AbstractEndpoint.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
protected static Codec2 getChannelCodec(URL url) {
    String codecName = url.getParameter(Constants.CODEC_KEY, "telnet");
    if (ExtensionLoader.getExtensionLoader(Codec2.class).hasExtension(codecName)) {
        return ExtensionLoader.getExtensionLoader(Codec2.class).getExtension(codecName);
    } else {
        return new CodecAdapter(ExtensionLoader.getExtensionLoader(Codec.class)
                                           .getExtension(codecName));
    }
}