TTransport

public abstract class TTransport implements Closeable {

  //当前连接是否已打开
public abstract boolean isOpen(); //是否还有数据需要读,当连接关闭时认为无数据可读
public boolean peek() {
return isOpen();
} //打开当前连接,可用于IO读写
public abstract void open()
throws TTransportException; //关闭当前连接
public abstract void close(); //向buf字节数组中写入数据,从off开始,最多读len长度的字节,最后返回实际向buf写入的字节数
public abstract int read(byte[] buf, int off, int len)
throws TTransportException; //确保向buf中从off开始写入,len长度的字节,这里通过循环调用上面的方法实现最后返回向buf写入的字节数
public int readAll(byte[] buf, int off, int len)
throws TTransportException {
int got = 0;
int ret = 0;
//没有读完继续下一次读取,直接读到的数据大于等于需要的len长度
while (got < len) {
ret = read(buf, off+got, len-got);
if (ret <= 0) {
throw new TTransportException(
"Cannot read. Remote side has closed. Tried to read "
+ len
+ " bytes, but only got "
+ got
+ " bytes. (This is often indicative of an internal error on the server side. Please check your server logs.)");
}
got += ret;
}
return got;
} //将buf中的数据全部发送出去
public void write(byte[] buf) throws TTransportException {
write(buf, 0, buf.length);
} //将buf的数据,从off开始,发送len长度的数据出去
public abstract void write(byte[] buf, int off, int len)
throws TTransportException; //下面四个方法,与ByteBuffer的原理类似
//获取到本地缓存的数据,没有缓存直接返回空
public byte[] getBuffer() {
return null;
} //返回本地缓存下一个读取位置,没有缓存返回0即可
public int getBufferPosition() {
return 0;
} //获取本地缓存中的字节数,没有缓存返回-1
public int getBytesRemainingInBuffer() {
return -1;
} //从本地缓存中消费n个字节
public void consumeBuffer(int len) {}
}
05-11 22:11