]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/EncodingUtils.java
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 package org
.apache
.thrift
;
23 * Utility methods for use when encoding/decoding raw data as byte arrays.
25 public class EncodingUtils
{
28 * Encode <code>integer</code> as a series of 4 bytes into <code>buf</code>
29 * starting at position 0 within that buffer.
32 * The integer to encode.
34 * The buffer to write to.
36 public static final void encodeBigEndian(final int integer
, final byte[] buf
) {
37 encodeBigEndian(integer
, buf
, 0);
41 * Encode <code>integer</code> as a series of 4 bytes into <code>buf</code>
42 * starting at position <code>offset</code>.
45 * The integer to encode.
47 * The buffer to write to.
49 * The offset within <code>buf</code> to start the encoding.
51 public static final void encodeBigEndian(final int integer
, final byte[] buf
, int offset
) {
52 buf
[offset
] = (byte) (0xff & (integer
>> 24));
53 buf
[offset
+ 1] = (byte) (0xff & (integer
>> 16));
54 buf
[offset
+ 2] = (byte) (0xff & (integer
>> 8));
55 buf
[offset
+ 3] = (byte) (0xff & (integer
));
59 * Decode a series of 4 bytes from <code>buf</code>, starting at position 0,
60 * and interpret them as an integer.
63 * The buffer to read from.
64 * @return An integer, as read from the buffer.
66 public static final int decodeBigEndian(final byte[] buf
) {
67 return decodeBigEndian(buf
, 0);
71 * Decode a series of 4 bytes from <code>buf</code>, start at
72 * <code>offset</code>, and interpret them as an integer.
75 * The buffer to read from.
77 * The offset with <code>buf</code> to start the decoding.
78 * @return An integer, as read from the buffer.
80 public static final int decodeBigEndian(final byte[] buf
, int offset
) {
81 return ((buf
[offset
] & 0xff) << 24) | ((buf
[offset
+ 1] & 0xff) << 16)
82 | ((buf
[offset
+ 2] & 0xff) << 8) | ((buf
[offset
+ 3] & 0xff));
87 * Returns true if the bit at position is set in v.
89 public static final boolean testBit(byte v
, int position
) {
90 return testBit((int)v
, position
);
93 public static final boolean testBit(short v
, int position
) {
94 return testBit((int)v
, position
);
97 public static final boolean testBit(int v
, int position
) {
98 return (v
& (1 << position
)) != 0;
101 public static final boolean testBit(long v
, int position
) {
102 return (v
& (1L << position
)) != 0L;
106 * Returns v, with the bit at position set to zero.
108 public static final byte clearBit(byte v
, int position
) {
109 return (byte)clearBit((int)v
, position
);
112 public static final short clearBit(short v
, int position
) {
113 return (short)clearBit((int)v
, position
);
116 public static final int clearBit(int v
, int position
) {
117 return v
& ~
(1 << position
);
120 public static final long clearBit(long v
, int position
) {
121 return v
& ~
(1L << position
);
125 * Returns v, with the bit at position set to 1 or 0 depending on value.
127 public static final byte setBit(byte v
, int position
, boolean value
) {
128 return (byte)setBit((int)v
, position
, value
);
131 public static final short setBit(short v
, int position
, boolean value
) {
132 return (short)setBit((int)v
, position
, value
);
135 public static final int setBit(int v
, int position
, boolean value
) {
137 return v
| (1 << position
);
139 return clearBit(v
, position
);
142 public static final long setBit(long v
, int position
, boolean value
) {
144 return v
| (1L << position
);
146 return clearBit(v
, position
);