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