Example usage for org.apache.thrift.transport TIOStreamTransport read

List of usage examples for org.apache.thrift.transport TIOStreamTransport read

Introduction

In this page you can find the example usage for org.apache.thrift.transport TIOStreamTransport read.

Prototype

public int read(byte[] buf, int off, int len) throws TTransportException 

Source Link

Document

Reads from the underlying input stream if not null.

Usage

From source file:com.alibaba.dubbo.rpc.protocol.thrift.ThriftCodec.java

License:Open Source License

public Object decode(Channel channel, InputStream input) throws IOException {

    int available = input.available();

    if (available < MESSAGE_SHORTEST_LENGTH) {

        return Codec.NEED_MORE_INPUT;

    } else {//from   w  w w . java 2 s .co  m

        TIOStreamTransport transport = new TIOStreamTransport(input);

        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 NEED_MORE_INPUT;
        }

        return decode(protocol);

    }

}

From source file:org.apache.dubbo.rpc.protocol.thrift.ThriftCodec.java

License:Apache License

@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 {//from w  w  w . ja v a2 s. c o m

        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);

    }

}