blob: 19d05e4770ef4355ba001b3a82c7ca0142a74a0f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.thrift.protocol;
import java.util.BitSet;
import org.apache.thrift.TException;
import org.apache.thrift.scheme.IScheme;
import org.apache.thrift.scheme.TupleScheme;
import org.apache.thrift.transport.TTransport;
public final class TTupleProtocol extends TCompactProtocol {
public static class Factory implements TProtocolFactory {
public Factory() {}
@Override
public TProtocol getProtocol(TTransport trans) {
return new TTupleProtocol(trans);
}
}
public TTupleProtocol(TTransport transport) {
super(transport);
}
@Override
public Class<? extends IScheme> getScheme() {
return TupleScheme.class;
}
public void writeBitSet(BitSet bs, int vectorWidth) throws TException {
byte[] bytes = toByteArray(bs, vectorWidth);
for (byte b : bytes) {
writeByte(b);
}
}
public BitSet readBitSet(int i) throws TException {
int length = (int) Math.ceil(i / 8.0);
byte[] bytes = new byte[length];
for (int j = 0; j < length; j++) {
bytes[j] = readByte();
}
BitSet bs = fromByteArray(bytes);
return bs;
}
/** Returns a bitset containing the values in bytes. The byte-ordering must be big-endian. */
public static BitSet fromByteArray(byte[] bytes) {
BitSet bits = new BitSet();
for (int i = 0; i < bytes.length * 8; i++) {
if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0) {
bits.set(i);
}
}
return bits;
}
/**
* Returns a byte array of at least length 1. The most significant bit in the result is guaranteed
* not to be a 1 (since BitSet does not support sign extension). The byte-ordering of the result
* is big-endian which means the most significant bit is in element 0. The bit at index 0 of the
* bit set is assumed to be the least significant bit.
*
* @param bits bit set
* @param vectorWidth width of the vector
* @return a byte array of at least length 1
*/
public static byte[] toByteArray(BitSet bits, int vectorWidth) {
byte[] bytes = new byte[(int) Math.ceil(vectorWidth / 8.0)];
for (int i = 0; i < bits.length(); i++) {
if (bits.get(i)) {
bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);
}
}
return bytes;
}
public TMap readMapBegin(byte keyType, byte valTyep) throws TException {
int size = super.readI32();
TMap map = new TMap(keyType, valTyep, size);
checkReadBytesAvailable(map);
return map;
}
public TList readListBegin(byte type) throws TException {
int size = super.readI32();
TList list = new TList(type, size);
checkReadBytesAvailable(list);
return list;
}
public TSet readSetBegin(byte type) throws TException {
return new TSet(readListBegin(type));
}
@Override
public void readMapEnd() throws TException {}
@Override
public void readListEnd() throws TException {}
@Override
public void readSetEnd() throws TException {}
}
|