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

2014-11-24 09:07:07 · 作者: · 浏览: 12
eByteDirect(getCompactType(map.keyType) << 4 | getCompactType(map.valueType));
}
}
public void writeListBegin(TList list)
throws TException {
writeCollectionBegin(list.elemType, list.size);
}
public void writeSetBegin(TSet set)
throws TException {
writeCollectionBegin(set.elemType, set.size);
}
public void writeBool(boolean b)
throws TException {
if (this.booleanField_ != null) {
writeFieldBeginInternal(this.booleanField_, (byte) (b 1 : 2));
this.booleanField_ = null;
} else {
writeByteDirect((byte) (b 1 : 2));
}
}
public void writeByte(byte b)
throws TException {
writeByteDirect(b);
}
public void writeI16(short i16)
throws TException {
writeVarint32(intToZigZag(i16));
}
public void writeI32(int i32)
throws TException {
writeVarint32(intToZigZag(i32));
}
public void writeI64(long i64)
throws TException {
writeVarint64(longToZigzag(i64));
}
public void writeDouble(double dub)
throws TException {
byte[] data = {0, 0, 0, 0, 0, 0, 0, 0};
fixedLongToBytes(Double.doubleToLongBits(dub), data, 0);
this.trans_.write(data);
}
public void writeString(String str) throws TException {
try {
byte[] bytes = str.getBytes("GBK");
writeBinary(bytes, 0, bytes.length);
} catch (UnsupportedEncodingException e) {
throw new TException("GBK not supported!");
}
}
public void writeBinary(ByteBuffer bin)
throws TException {
int length = bin.limit() - bin.position();
writeBinary(bin.array(), bin.position() + bin.arrayOffset(), length);
}
private void writeBinary(byte[] buf, int offset, int length) throws TException {
writeVarint32(length);
this.trans_.write(buf, offset, length);
}
public void writeMessageEnd() throws TException {
}
public void writeMapEnd() throws TException {
}
public void writeListEnd() throws TException {
}
public void writeSetEnd() throws TException {
}
public void writeFieldEnd() throws TException {
}
protected void writeCollectionBegin(byte elemType, int size) throws TException {
if (size <= 14) {
writeByteDirect(size << 4 | getCompactType(elemType));
} else {
writeByteDirect(0xF0 | getCompactType(elemType));
writeVarint32(size);
}
}
private void writeVarint32(int n)
throws TException {
int idx = 0;
while (true) {
if ((n & 0xFFFFFF80) == 0) {
this.i32buf[(idx++)] = (byte) n;
break;
}
this.i32buf[(idx++)] = (byte) (n & 0x7F | 0x80);
n >>>= 7;
}
this.trans_.write(this.i32buf, 0, idx);
}
private void writeVarint64(long n)
throws TException {
int idx = 0;
while (true) {
if ((n & 0xFFFFFF80) == 0L) {
this.varint64out[(idx++)] = (byte) (int) n;
break;
}
this.varin