| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import java.util.UUID; |
| 9 |
|
| 10 |
/** |
| 11 |
* Manipulations with bytes and arrays. This class can be overridden in |
| 12 |
* multi-release JAR with more efficient implementation for a newer versions of |
| 13 |
* Java. |
| 14 |
*/ |
| 15 |
public final class Bits { |
| 16 |
|
| 17 |
/* |
| 18 |
* Signatures of methods should match with |
| 19 |
* h2/src/java9/src/org/h2/util/Bits.java and precompiled |
| 20 |
* h2/src/java9/precompiled/org/h2/util/Bits.class. |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* Compare the contents of two char arrays. If the content or length of the |
| 25 |
* first array is smaller than the second array, -1 is returned. If the content |
| 26 |
* or length of the second array is smaller than the first array, 1 is returned. |
| 27 |
* If the contents and lengths are the same, 0 is returned. |
| 28 |
* |
| 29 |
* @param data1 |
| 30 |
* the first char array (must not be null) |
| 31 |
* @param data2 |
| 32 |
* the second char array (must not be null) |
| 33 |
* @return the result of the comparison (-1, 1 or 0) |
| 34 |
*/ |
| 35 |
public static int compareNotNull(char[] data1, char[] data2) { |
| 36 |
if (data1 == data2) { |
| 37 |
return 0; |
| 38 |
} |
| 39 |
int len = Math.min(data1.length, data2.length); |
| 40 |
for (int i = 0; i < len; i++) { |
| 41 |
char b = data1[i]; |
| 42 |
char b2 = data2[i]; |
| 43 |
if (b != b2) { |
| 44 |
return b > b2 ? 1 : -1; |
| 45 |
} |
| 46 |
} |
| 47 |
return Integer.signum(data1.length - data2.length); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Compare the contents of two byte arrays. If the content or length of the |
| 52 |
* first array is smaller than the second array, -1 is returned. If the content |
| 53 |
* or length of the second array is smaller than the first array, 1 is returned. |
| 54 |
* If the contents and lengths are the same, 0 is returned. |
| 55 |
* |
| 56 |
* <p> |
| 57 |
* This method interprets bytes as signed. |
| 58 |
* </p> |
| 59 |
* |
| 60 |
* @param data1 |
| 61 |
* the first byte array (must not be null) |
| 62 |
* @param data2 |
| 63 |
* the second byte array (must not be null) |
| 64 |
* @return the result of the comparison (-1, 1 or 0) |
| 65 |
*/ |
| 66 |
public static int compareNotNullSigned(byte[] data1, byte[] data2) { |
| 67 |
if (data1 == data2) { |
| 68 |
return 0; |
| 69 |
} |
| 70 |
int len = Math.min(data1.length, data2.length); |
| 71 |
for (int i = 0; i < len; i++) { |
| 72 |
byte b = data1[i]; |
| 73 |
byte b2 = data2[i]; |
| 74 |
if (b != b2) { |
| 75 |
return b > b2 ? 1 : -1; |
| 76 |
} |
| 77 |
} |
| 78 |
return Integer.signum(data1.length - data2.length); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Compare the contents of two byte arrays. If the content or length of the |
| 83 |
* first array is smaller than the second array, -1 is returned. If the content |
| 84 |
* or length of the second array is smaller than the first array, 1 is returned. |
| 85 |
* If the contents and lengths are the same, 0 is returned. |
| 86 |
* |
| 87 |
* <p> |
| 88 |
* This method interprets bytes as unsigned. |
| 89 |
* </p> |
| 90 |
* |
| 91 |
* @param data1 |
| 92 |
* the first byte array (must not be null) |
| 93 |
* @param data2 |
| 94 |
* the second byte array (must not be null) |
| 95 |
* @return the result of the comparison (-1, 1 or 0) |
| 96 |
*/ |
| 97 |
public static int compareNotNullUnsigned(byte[] data1, byte[] data2) { |
| 98 |
if (data1 == data2) { |
| 99 |
return 0; |
| 100 |
} |
| 101 |
int len = Math.min(data1.length, data2.length); |
| 102 |
for (int i = 0; i < len; i++) { |
| 103 |
int b = data1[i] & 0xff; |
| 104 |
int b2 = data2[i] & 0xff; |
| 105 |
if (b != b2) { |
| 106 |
return b > b2 ? 1 : -1; |
| 107 |
} |
| 108 |
} |
| 109 |
return Integer.signum(data1.length - data2.length); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Reads a int value from the byte array at the given position in big-endian |
| 114 |
* order. |
| 115 |
* |
| 116 |
* @param buff |
| 117 |
* the byte array |
| 118 |
* @param pos |
| 119 |
* the position |
| 120 |
* @return the value |
| 121 |
*/ |
| 122 |
public static int readInt(byte[] buff, int pos) { |
| 123 |
return (buff[pos++] << 24) + ((buff[pos++] & 0xff) << 16) + ((buff[pos++] & 0xff) << 8) + (buff[pos] & 0xff); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Reads a int value from the byte array at the given position in |
| 128 |
* little-endian order. |
| 129 |
* |
| 130 |
* @param buff |
| 131 |
* the byte array |
| 132 |
* @param pos |
| 133 |
* the position |
| 134 |
* @return the value |
| 135 |
*/ |
| 136 |
public static int readIntLE(byte[] buff, int pos) { |
| 137 |
return (buff[pos++] & 0xff) + ((buff[pos++] & 0xff) << 8) + ((buff[pos++] & 0xff) << 16) + (buff[pos] << 24); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Reads a long value from the byte array at the given position in |
| 142 |
* big-endian order. |
| 143 |
* |
| 144 |
* @param buff |
| 145 |
* the byte array |
| 146 |
* @param pos |
| 147 |
* the position |
| 148 |
* @return the value |
| 149 |
*/ |
| 150 |
public static long readLong(byte[] buff, int pos) { |
| 151 |
return (((long) readInt(buff, pos)) << 32) + (readInt(buff, pos + 4) & 0xffff_ffffL); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Reads a long value from the byte array at the given position in |
| 156 |
* little-endian order. |
| 157 |
* |
| 158 |
* @param buff |
| 159 |
* the byte array |
| 160 |
* @param pos |
| 161 |
* the position |
| 162 |
* @return the value |
| 163 |
*/ |
| 164 |
public static long readLongLE(byte[] buff, int pos) { |
| 165 |
return (readIntLE(buff, pos) & 0xffff_ffffL) + (((long) readIntLE(buff, pos + 4)) << 32); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Reads a double value from the byte array at the given position in |
| 170 |
* big-endian order. |
| 171 |
* |
| 172 |
* @param buff |
| 173 |
* the byte array |
| 174 |
* @param pos |
| 175 |
* the position |
| 176 |
* @return the value |
| 177 |
*/ |
| 178 |
public static double readDouble(byte[] buff, int pos) { |
| 179 |
return Double.longBitsToDouble(readLong(buff, pos)); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Reads a double value from the byte array at the given position in |
| 184 |
* little-endian order. |
| 185 |
* |
| 186 |
* @param buff |
| 187 |
* the byte array |
| 188 |
* @param pos |
| 189 |
* the position |
| 190 |
* @return the value |
| 191 |
*/ |
| 192 |
public static double readDoubleLE(byte[] buff, int pos) { |
| 193 |
return Double.longBitsToDouble(readLongLE(buff, pos)); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Converts UUID value to byte array in big-endian order. |
| 198 |
* |
| 199 |
* @param msb |
| 200 |
* most significant part of UUID |
| 201 |
* @param lsb |
| 202 |
* least significant part of UUID |
| 203 |
* @return byte array representation |
| 204 |
*/ |
| 205 |
public static byte[] uuidToBytes(long msb, long lsb) { |
| 206 |
byte[] buff = new byte[16]; |
| 207 |
for (int i = 0; i < 8; i++) { |
| 208 |
buff[i] = (byte) ((msb >> (8 * (7 - i))) & 0xff); |
| 209 |
buff[8 + i] = (byte) ((lsb >> (8 * (7 - i))) & 0xff); |
| 210 |
} |
| 211 |
return buff; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Converts UUID value to byte array in big-endian order. |
| 216 |
* |
| 217 |
* @param uuid |
| 218 |
* UUID value |
| 219 |
* @return byte array representation |
| 220 |
*/ |
| 221 |
public static byte[] uuidToBytes(UUID uuid) { |
| 222 |
return uuidToBytes(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Writes a int value to the byte array at the given position in big-endian |
| 227 |
* order. |
| 228 |
* |
| 229 |
* @param buff |
| 230 |
* the byte array |
| 231 |
* @param pos |
| 232 |
* the position |
| 233 |
* @param x |
| 234 |
* the value to write |
| 235 |
*/ |
| 236 |
public static void writeInt(byte[] buff, int pos, int x) { |
| 237 |
buff[pos++] = (byte) (x >> 24); |
| 238 |
buff[pos++] = (byte) (x >> 16); |
| 239 |
buff[pos++] = (byte) (x >> 8); |
| 240 |
buff[pos] = (byte) x; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Writes a int value to the byte array at the given position in |
| 245 |
* little-endian order. |
| 246 |
* |
| 247 |
* @param buff |
| 248 |
* the byte array |
| 249 |
* @param pos |
| 250 |
* the position |
| 251 |
* @param x |
| 252 |
* the value to write |
| 253 |
*/ |
| 254 |
public static void writeIntLE(byte[] buff, int pos, int x) { |
| 255 |
buff[pos++] = (byte) x; |
| 256 |
buff[pos++] = (byte) (x >> 8); |
| 257 |
buff[pos++] = (byte) (x >> 16); |
| 258 |
buff[pos] = (byte) (x >> 24); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Writes a long value to the byte array at the given position in big-endian |
| 263 |
* order. |
| 264 |
* |
| 265 |
* @param buff |
| 266 |
* the byte array |
| 267 |
* @param pos |
| 268 |
* the position |
| 269 |
* @param x |
| 270 |
* the value to write |
| 271 |
*/ |
| 272 |
public static void writeLong(byte[] buff, int pos, long x) { |
| 273 |
writeInt(buff, pos, (int) (x >> 32)); |
| 274 |
writeInt(buff, pos + 4, (int) x); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Writes a long value to the byte array at the given position in |
| 279 |
* little-endian order. |
| 280 |
* |
| 281 |
* @param buff |
| 282 |
* the byte array |
| 283 |
* @param pos |
| 284 |
* the position |
| 285 |
* @param x |
| 286 |
* the value to write |
| 287 |
*/ |
| 288 |
public static void writeLongLE(byte[] buff, int pos, long x) { |
| 289 |
writeIntLE(buff, pos, (int) x); |
| 290 |
writeIntLE(buff, pos + 4, (int) (x >> 32)); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Writes a double value to the byte array at the given position in |
| 295 |
* big-endian order. |
| 296 |
* |
| 297 |
* @param buff |
| 298 |
* the byte array |
| 299 |
* @param pos |
| 300 |
* the position |
| 301 |
* @param x |
| 302 |
* the value to write |
| 303 |
*/ |
| 304 |
public static void writeDouble(byte[] buff, int pos, double x) { |
| 305 |
writeLong(buff, pos, Double.doubleToRawLongBits(x)); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Writes a double value to the byte array at the given position in |
| 310 |
* little-endian order. |
| 311 |
* |
| 312 |
* @param buff |
| 313 |
* the byte array |
| 314 |
* @param pos |
| 315 |
* the position |
| 316 |
* @param x |
| 317 |
* the value to write |
| 318 |
*/ |
| 319 |
public static void writeDoubleLE(byte[] buff, int pos, double x) { |
| 320 |
writeLongLE(buff, pos, Double.doubleToRawLongBits(x)); |
| 321 |
} |
| 322 |
|
| 323 |
private Bits() { |
| 324 |
} |
| 325 |
} |