Java Code Examples for com.alibaba.dubbo.remoting.buffer.ChannelBuffer#readableBytes()

The following examples show how to use com.alibaba.dubbo.remoting.buffer.ChannelBuffer#readableBytes() . 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: TelnetCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
protected void testEecode_assertEquals(Object request, byte[] ret, boolean isServerside) throws IOException {
    //init channel
    Channel channel = isServerside ? getServerSideChannel(url) : getCliendSideChannel(url);

    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024);

    codec.encode(channel, buffer, request);
    byte[] data = new byte[buffer.readableBytes()];
    buffer.readBytes(data);

    Assert.assertEquals(ret.length, data.length);
    for (int i = 0; i < ret.length; i++) {
        if (ret[i] != data[i]) {
            Assert.fail();
        }
    }
}
 
Example 2
Source File: CodecAdapter.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    byte[] bytes = new byte[buffer.readableBytes()];
    int savedReaderIndex = buffer.readerIndex();
    buffer.readBytes(bytes);
    UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(bytes);
    Object result = codec.decode(channel, is);
    buffer.readerIndex(savedReaderIndex + is.position());
    return result == Codec.NEED_MORE_INPUT ? DecodeResult.NEED_MORE_INPUT : result;
}
 
Example 3
Source File: NettyBackedChannelBuffer.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(int index, ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    setBytes(index, src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example 4
Source File: NettyBackedChannelBuffer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(int index, ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    setBytes(index, src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example 5
Source File: NettyBackedChannelBuffer.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBytes(ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    writeBytes(src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example 6
Source File: NettyBackedChannelBuffer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBytes(ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    writeBytes(src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example 7
Source File: CodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    byte[] bytes = new byte[buffer.readableBytes()];
    int savedReaderIndex = buffer.readerIndex();
    buffer.readBytes(bytes);
    UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(bytes);
    Object result = codec.decode(channel, is);
    buffer.readerIndex(savedReaderIndex + is.position());
    return result == Codec.NEED_MORE_INPUT ? DecodeResult.NEED_MORE_INPUT : result;
}
 
Example 8
Source File: NettyBackedChannelBuffer.java    From dubbo-remoting-netty4 with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(int index, ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    setBytes(index, src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example 9
Source File: NettyBackedChannelBuffer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(int index, ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    setBytes(index, src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example 10
Source File: NettyBackedChannelBuffer.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public void writeBytes(ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    writeBytes(src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example 11
Source File: TelnetCodec.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    int readable = buffer.readableBytes();
    byte[] message = new byte[readable];
    buffer.readBytes(message);
    return decode(channel, buffer, readable, message);
}
 
Example 12
Source File: ExchangeCodec.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    int readable = buffer.readableBytes();
    byte[] header = new byte[Math.min(readable, HEADER_LENGTH)];
    buffer.readBytes(header);
    return decode(channel, buffer, readable, header);
}
 
Example 13
Source File: ExchangeCodec.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    int readable = buffer.readableBytes();
    byte[] header = new byte[Math.min(readable, HEADER_LENGTH)];
    buffer.readBytes(header);
    return decode(channel, buffer, readable, header);
}
 
Example 14
Source File: TelnetCodec.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    int readable = buffer.readableBytes();
    byte[] message = new byte[readable];
    buffer.readBytes(message);
    return decode(channel, buffer, readable, message);
}
 
Example 15
Source File: ThriftCodec.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Override
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {

    int available = buffer.readableBytes();

    if (available < MESSAGE_SHORTEST_LENGTH) {

        return DecodeResult.NEED_MORE_INPUT;

    } else {

        TIOStreamTransport transport = new TIOStreamTransport(new ChannelBufferInputStream(buffer));

        TBinaryProtocol protocol = new TBinaryProtocol(transport);

        String serviceName = channel.getUrl().getParameter(Constants.INTERFACE_KEY);
        return decode(serviceName, protocol);

    }

}
 
Example 16
Source File: ThriftCodec.java    From dubbox with Apache License 2.0 2 votes vote down vote up
public Object decode( Channel channel, ChannelBuffer buffer ) throws IOException {

        int available = buffer.readableBytes();

        if ( available < MESSAGE_SHORTEST_LENGTH ) {

            return DecodeResult.NEED_MORE_INPUT;

        } else {

            TIOStreamTransport transport = new TIOStreamTransport( new ChannelBufferInputStream(buffer));

            TBinaryProtocol protocol = new TBinaryProtocol( transport );

            short magic;
            int messageLength;

            try{
//                protocol.readI32(); // skip the first message length
                byte[] bytes = new byte[4];
                transport.read( bytes, 0, 4 );
                magic = protocol.readI16();
                messageLength = protocol.readI32();

            } catch ( TException e ) {
                throw new IOException( e.getMessage(), e );
            }

            if ( MAGIC != magic ) {
                throw new IOException(
                        new StringBuilder( 32 )
                                .append( "Unknown magic code " )
                                .append( magic )
                                .toString() );
            }

            if ( available < messageLength ) { return  DecodeResult.NEED_MORE_INPUT; }

            return decode( protocol );

        }

    }
 
Example 17
Source File: ThriftCodec.java    From dubbo-2.6.5 with Apache License 2.0 2 votes vote down vote up
@Override
    public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {

        int available = buffer.readableBytes();

        if (available < MESSAGE_SHORTEST_LENGTH) {

            return DecodeResult.NEED_MORE_INPUT;

        } else {

            TIOStreamTransport transport = new TIOStreamTransport(new ChannelBufferInputStream(buffer));

            TBinaryProtocol protocol = new TBinaryProtocol(transport);

            short magic;
            int messageLength;

            try {
//                protocol.readI32(); // skip the first message length
                byte[] bytes = new byte[4];
                transport.read(bytes, 0, 4);
                magic = protocol.readI16();
                messageLength = protocol.readI32();

            } catch (TException e) {
                throw new IOException(e.getMessage(), e);
            }

            if (MAGIC != magic) {
                throw new IOException("Unknown magic code " + magic);
            }

            if (available < messageLength) {
                return DecodeResult.NEED_MORE_INPUT;
            }

            return decode(protocol);

        }

    }
 
Example 18
Source File: ThriftCodec.java    From dubbox with Apache License 2.0 2 votes vote down vote up
public Object decode( Channel channel, ChannelBuffer buffer ) throws IOException {

        int available = buffer.readableBytes();

        if ( available < MESSAGE_SHORTEST_LENGTH ) {

            return DecodeResult.NEED_MORE_INPUT;

        } else {

            TIOStreamTransport transport = new TIOStreamTransport( new ChannelBufferInputStream(buffer));

            TBinaryProtocol protocol = new TBinaryProtocol( transport );

            short magic;
            int messageLength;

            try{
//                protocol.readI32(); // skip the first message length
                byte[] bytes = new byte[4];
                transport.read( bytes, 0, 4 );
                magic = protocol.readI16();
                messageLength = protocol.readI32();

            } catch ( TException e ) {
                throw new IOException( e.getMessage(), e );
            }

            if ( MAGIC != magic ) {
                throw new IOException(
                        new StringBuilder( 32 )
                                .append( "Unknown magic code " )
                                .append( magic )
                                .toString() );
            }

            if ( available < messageLength ) { return  DecodeResult.NEED_MORE_INPUT; }

            return decode( protocol );

        }

    }
 
Example 19
Source File: ThriftCodec.java    From dubbox-hystrix with Apache License 2.0 2 votes vote down vote up
public Object decode( Channel channel, ChannelBuffer buffer ) throws IOException {

        int available = buffer.readableBytes();

        if ( available < MESSAGE_SHORTEST_LENGTH ) {

            return DecodeResult.NEED_MORE_INPUT;

        } else {

            TIOStreamTransport transport = new TIOStreamTransport( new ChannelBufferInputStream(buffer));

            TBinaryProtocol protocol = new TBinaryProtocol( transport );

            short magic;
            int messageLength;

            try{
//                protocol.readI32(); // skip the first message length
                byte[] bytes = new byte[4];
                transport.read( bytes, 0, 4 );
                magic = protocol.readI16();
                messageLength = protocol.readI32();

            } catch ( TException e ) {
                throw new IOException( e.getMessage(), e );
            }

            if ( MAGIC != magic ) {
                throw new IOException(
                        new StringBuilder( 32 )
                                .append( "Unknown magic code " )
                                .append( magic )
                                .toString() );
            }

            if ( available < messageLength ) { return  DecodeResult.NEED_MORE_INPUT; }

            return decode( protocol );

        }

    }
 
Example 20
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode( channel, output, request );

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream( bytes );

    TTransport transport = new TIOStreamTransport( bis );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    // frame
    byte[] length = new byte[4];
    transport.read( length, 0, 4 );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    // magic
    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals( messageLength + 4, bytes.length );

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    // service name
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    // dubbo request id
    Assert.assertEquals( request.getId(), protocol.readI64() );

    // test message header length
    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read( protocol );

    protocol.readMessageEnd();

    Assert.assertEquals( "echoString", message.name );

    Assert.assertEquals( TMessageType.CALL, message.type );

    Assert.assertEquals( "Hello, World!", args.getArg() );

}