]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | ||
20 | package org.apache.thrift; | |
21 | ||
22 | /** | |
23 | * Utility methods for use when encoding/decoding raw data as byte arrays. | |
24 | */ | |
25 | public class EncodingUtils { | |
26 | ||
27 | /** | |
28 | * Encode <code>integer</code> as a series of 4 bytes into <code>buf</code> | |
29 | * starting at position 0 within that buffer. | |
30 | * | |
31 | * @param integer | |
32 | * The integer to encode. | |
33 | * @param buf | |
34 | * The buffer to write to. | |
35 | */ | |
36 | public static final void encodeBigEndian(final int integer, final byte[] buf) { | |
37 | encodeBigEndian(integer, buf, 0); | |
38 | } | |
39 | ||
40 | /** | |
41 | * Encode <code>integer</code> as a series of 4 bytes into <code>buf</code> | |
42 | * starting at position <code>offset</code>. | |
43 | * | |
44 | * @param integer | |
45 | * The integer to encode. | |
46 | * @param buf | |
47 | * The buffer to write to. | |
48 | * @param offset | |
49 | * The offset within <code>buf</code> to start the encoding. | |
50 | */ | |
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)); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Decode a series of 4 bytes from <code>buf</code>, starting at position 0, | |
60 | * and interpret them as an integer. | |
61 | * | |
62 | * @param buf | |
63 | * The buffer to read from. | |
64 | * @return An integer, as read from the buffer. | |
65 | */ | |
66 | public static final int decodeBigEndian(final byte[] buf) { | |
67 | return decodeBigEndian(buf, 0); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Decode a series of 4 bytes from <code>buf</code>, start at | |
72 | * <code>offset</code>, and interpret them as an integer. | |
73 | * | |
74 | * @param buf | |
75 | * The buffer to read from. | |
76 | * @param offset | |
77 | * The offset with <code>buf</code> to start the decoding. | |
78 | * @return An integer, as read from the buffer. | |
79 | */ | |
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)); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Bitfield utilities. | |
87 | * Returns true if the bit at position is set in v. | |
88 | */ | |
89 | public static final boolean testBit(byte v, int position) { | |
90 | return testBit((int)v, position); | |
91 | } | |
92 | ||
93 | public static final boolean testBit(short v, int position) { | |
94 | return testBit((int)v, position); | |
95 | } | |
96 | ||
97 | public static final boolean testBit(int v, int position) { | |
98 | return (v & (1 << position)) != 0; | |
99 | } | |
100 | ||
101 | public static final boolean testBit(long v, int position) { | |
102 | return (v & (1L << position)) != 0L; | |
103 | } | |
104 | ||
105 | /** | |
106 | * Returns v, with the bit at position set to zero. | |
107 | */ | |
108 | public static final byte clearBit(byte v, int position) { | |
109 | return (byte)clearBit((int)v, position); | |
110 | } | |
111 | ||
112 | public static final short clearBit(short v, int position) { | |
113 | return (short)clearBit((int)v, position); | |
114 | } | |
115 | ||
116 | public static final int clearBit(int v, int position) { | |
117 | return v & ~(1 << position); | |
118 | } | |
119 | ||
120 | public static final long clearBit(long v, int position) { | |
121 | return v & ~(1L << position); | |
122 | } | |
123 | ||
124 | /** | |
125 | * Returns v, with the bit at position set to 1 or 0 depending on value. | |
126 | */ | |
127 | public static final byte setBit(byte v, int position, boolean value) { | |
128 | return (byte)setBit((int)v, position, value); | |
129 | } | |
130 | ||
131 | public static final short setBit(short v, int position, boolean value) { | |
132 | return (short)setBit((int)v, position, value); | |
133 | } | |
134 | ||
135 | public static final int setBit(int v, int position, boolean value) { | |
136 | if(value) | |
137 | return v | (1 << position); | |
138 | else | |
139 | return clearBit(v, position); | |
140 | } | |
141 | ||
142 | public static final long setBit(long v, int position, boolean value) { | |
143 | if(value) | |
144 | return v | (1L << position); | |
145 | else | |
146 | return clearBit(v, position); | |
147 | } | |
148 | } |