Thrift 中以GBK传输中文字符和分词服务搭建(四)

2014-11-24 09:07:07 · 作者: · 浏览: 10
return new TList(type, size);
}
public TSet readSetBegin()
throws TException {
return new TSet(readListBegin());
}
public boolean readBool()
throws TException {
if (this.boolValue_ != null) {
boolean result = this.boolValue_.booleanValue();
this.boolValue_ = null;
return result;
}
return readByte() == 1;
}
public byte readByte()
throws TException {
byte b;
if (this.trans_.getBytesRemainingInBuffer() > 0) {
b = this.trans_.getBuffer()[this.trans_.getBufferPosition()];
this.trans_.consumeBuffer(1);
} else {
this.trans_.readAll(this.byteRawBuf, 0, 1);
b = this.byteRawBuf[0];
}
return b;
}
public short readI16()
throws TException {
return (short) zigzagToInt(readVarint32());
}
public int readI32()
throws TException {
return zigzagToInt(readVarint32());
}
public long readI64()
throws TException {
return zigzagToLong(readVarint64());
}
public double readDouble()
throws TException {
byte[] longBits = new byte[8];
this.trans_.readAll(longBits, 0, 8);
return Double.longBitsToDouble(bytesToLong(longBits));
}
public String readString()
throws TException {
int length = readVarint32();
if (length == 0) {
return "";
}
try {
if (this.trans_.getBytesRemainingInBuffer() >
= length) {
String str = new String(this.trans_.getBuffer(), this.trans_.getBufferPosition(), length, "GBK");
this.trans_.consumeBuffer(length);
return str;
}
return new String(readBinary(length), "GBK");
} catch (UnsupportedEncodingException e) {
}
throw new TException("GBK not supported!");
}
public ByteBuffer readBinary()
throws TException {
int length = readVarint32();
if (length == 0) return ByteBuffer.wrap(new byte[0]);
byte[] buf = new byte[length];
this.trans_.readAll(buf, 0, length);
return ByteBuffer.wrap(buf);
}
private byte[] readBinary(int length)
throws TException {
if (length == 0) return new byte[0];
byte[] buf = new byte[length];
this.trans_.readAll(buf, 0, length);
return buf;
}
public void readMessageEnd() throws TException {
}
public void readFieldEnd() throws TException {
}
public void readMapEnd() throws TException {
}
public void readListEnd() throws TException {
}
public void readSetEnd() throws TException {
}
private int readVarint32() throws TException {
int result = 0;
int shift = 0;
if (this.trans_.getBytesRemainingInBuffer() >= 5) {
byte[] buf = this.trans_.getBuffer();
int pos = this.trans_.getBufferPosition();
int off = 0;
while (true) {
byte b = buf[(pos + off)];
result |= (b & 0x7F) << shift;
if ((b & 0x80) != 12