| 1 | package com.flanderra.commons.utils;
|
| 2 |
|
| 3 | import java.io.ByteArrayOutputStream;
|
| 4 | import java.io.IOException;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.BitSet;
|
| 7 |
|
| 8 | import org.apache.commons.logging.Log;
|
| 9 | import org.apache.commons.logging.LogFactory;
|
| 10 |
|
| 11 | /**
|
| 12 | * @deprecated
|
| 13 | */
|
| 14 | public class OldBitUtils {
|
| 15 |
|
| 16 | private static Log log = LogFactory.getLog(OldBitUtils.class);
|
| 17 |
|
| 18 | // ///////////////////////////////////////////
|
| 19 | // utility methods for basic types
|
| 20 | // ///////////////////////////////////////////
|
| 21 | /**
|
| 22 | * @deprecated
|
| 23 | */
|
| 24 | public static BitSet bitsUI8(long val) {
|
| 25 | Assert.inRange(val, BitUtils.MIN_UI8, BitUtils.MAX_UI8);
|
| 26 | BitSet bitset = longToBits(val, 8);
|
| 27 | return bitset;
|
| 28 | }
|
| 29 |
|
| 30 | /**
|
| 31 | * @deprecated
|
| 32 | */
|
| 33 | public static byte[] bytesUI8(long val) {
|
| 34 | return bitsToBytes(bitsUI8(val), 8);
|
| 35 | }
|
| 36 |
|
| 37 | /**
|
| 38 | * @deprecated
|
| 39 | */
|
| 40 | public static BitSet bitsUI16(long val) {
|
| 41 | Assert.inRange(val, BitUtils.MIN_UI16, BitUtils.MAX_UI16);
|
| 42 | return flipBytes(longToBits(val, 16), 2);
|
| 43 | }
|
| 44 |
|
| 45 | /**
|
| 46 | * @deprecated
|
| 47 | */
|
| 48 | public static byte[] bytesUI16(long val) {
|
| 49 | return bitsToBytes(bitsUI16(val), 16);
|
| 50 | }
|
| 51 |
|
| 52 | /**
|
| 53 | * @deprecated
|
| 54 | */
|
| 55 | public static BitSet bitsUI24(long val) {
|
| 56 | Assert.inRange(val, BitUtils.MIN_UI24, BitUtils.MAX_UI24);
|
| 57 | return flipBytes(longToBits(val, 24), 3);
|
| 58 | }
|
| 59 |
|
| 60 | /**
|
| 61 | * @deprecated
|
| 62 | */
|
| 63 | public static byte[] bytesUI24(long val) {
|
| 64 | return bitsToBytes(bitsUI24(val), 24);
|
| 65 | }
|
| 66 |
|
| 67 | /**
|
| 68 | * @deprecated
|
| 69 | */
|
| 70 | public static BitSet bitsUI32(long val) {
|
| 71 | Assert.inRange(val, BitUtils.MIN_UI32, BitUtils.MAX_UI32);
|
| 72 | return flipBytes(longToBits(val, 32), 4);
|
| 73 | }
|
| 74 |
|
| 75 | /**
|
| 76 | * @deprecated
|
| 77 | */
|
| 78 | public static byte[] bytesUI32(long val) {
|
| 79 | return bitsToBytes(bitsUI32(val), 32);
|
| 80 | }
|
| 81 |
|
| 82 | /**
|
| 83 | * @deprecated
|
| 84 | */
|
| 85 | public static BitSet bitsSI8(long val) {
|
| 86 | Assert.inRange(val, BitUtils.MIN_SI8, BitUtils.MAX_SI8);
|
| 87 | return longToBits(val, 8);
|
| 88 | }
|
| 89 |
|
| 90 | /**
|
| 91 | * @deprecated
|
| 92 | */
|
| 93 | public static byte[] bytesSI8(long val) {
|
| 94 | return bitsToBytes(bitsSI8(val), 8);
|
| 95 | }
|
| 96 |
|
| 97 | /**
|
| 98 | * @deprecated
|
| 99 | */
|
| 100 | public static BitSet bitsSI16(long val) {
|
| 101 | Assert.inRange(val, BitUtils.MIN_SI16, BitUtils.MAX_SI16);
|
| 102 | return flipBytes(longToBits(val, 16), 2);
|
| 103 | }
|
| 104 |
|
| 105 | /**
|
| 106 | * @deprecated
|
| 107 | */
|
| 108 | public static byte[] bytesSI16(long val) {
|
| 109 | return bitsToBytes(bitsSI16(val), 16);
|
| 110 | }
|
| 111 |
|
| 112 | /**
|
| 113 | * @deprecated
|
| 114 | */
|
| 115 | public static BitSet bitsSI32(long val) {
|
| 116 | Assert.inRange(val, BitUtils.MIN_SI32, BitUtils.MAX_SI32);
|
| 117 | return flipBytes(longToBits(val, 32), 4);
|
| 118 | }
|
| 119 |
|
| 120 | /**
|
| 121 | * @deprecated
|
| 122 | */
|
| 123 | public static byte[] bytesSI32(long val) {
|
| 124 | return bitsToBytes(bitsSI32(val), 32);
|
| 125 | }
|
| 126 |
|
| 127 | /**
|
| 128 | * @deprecated
|
| 129 | */
|
| 130 | public static byte[] bytesSTRING(String val) {
|
| 131 | try {
|
| 132 | if (val == null || val.equals("")) {
|
| 133 | return new byte[] { 0x00 };
|
| 134 | }
|
| 135 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
| 136 | baos.write(val.getBytes("UTF-8"));
|
| 137 | baos.write(0x00);
|
| 138 | return baos.toByteArray();
|
| 139 | } catch (IOException e) {
|
| 140 | log.error(e);
|
| 141 | throw new RuntimeException(e);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | /**
|
| 146 | * @deprecated
|
| 147 | */
|
| 148 | public static BitSet bitsFixed88(double val) {
|
| 149 | Assert.inRange(val, BitUtils.MIN_FIXED88, BitUtils.MAX_FIXED88);
|
| 150 | return bitsSI16((short) (val * 256.0));
|
| 151 | }
|
| 152 |
|
| 153 | /**
|
| 154 | * @deprecated
|
| 155 | */
|
| 156 | public static byte[] bytesFixed88(double val) {
|
| 157 | return bitsToBytes(bitsFixed88(val), 16);
|
| 158 | }
|
| 159 |
|
| 160 | /**
|
| 161 | * @deprecated
|
| 162 | */
|
| 163 | public static BitSet bitsFixed1616(double val) {
|
| 164 | Assert.inRange(val, BitUtils.MIN_FIXED1616, BitUtils.MAX_FIXED1616);
|
| 165 | BitSet result = new BitSet(32);
|
| 166 | result = concat(result, bitsSI32((int) (val * 65536.0)), 0, 32);
|
| 167 | return result;
|
| 168 | }
|
| 169 |
|
| 170 | /**
|
| 171 | * @deprecated
|
| 172 | */
|
| 173 | public static byte[] bytesFixed1616(double val) {
|
| 174 | return bitsToBytes(bitsFixed1616(val), 32);
|
| 175 | }
|
| 176 |
|
| 177 | /**
|
| 178 | * @deprecated
|
| 179 | */
|
| 180 | public static BitSet bitsFloat16(double val) {
|
| 181 | Assert.inRange(val, BitUtils.MIN_FLOAT16, BitUtils.MAX_FLOAT16);
|
| 182 | int bits32 = Float.floatToIntBits((float) val);
|
| 183 | int sign = Math.abs((bits32 & 0x80000000) >> 31);
|
| 184 | int exponent32 = (bits32 & 0x7f800000) >> 23;
|
| 185 | int mantissa32 = bits32 & 0x7fffff;
|
| 186 | int exponent16 = 0;
|
| 187 | if (exponent32 != 0) {
|
| 188 | if (exponent32 == 0xff) {
|
| 189 | exponent16 = 0x1f;
|
| 190 | } else {
|
| 191 | exponent16 = exponent32 - 127 + 15;
|
| 192 | }
|
| 193 | }
|
| 194 | int mantissa16 = 0;
|
| 195 | if (exponent16 < 0) {
|
| 196 | exponent16 = 0; // positive/negative zero
|
| 197 | } else if (exponent16 > 0x1f) {
|
| 198 | exponent16 = 0x1f; // infinity
|
| 199 | } else {
|
| 200 | mantissa16 = mantissa32 >> 13;
|
| 201 | }
|
| 202 | int bits16 = sign << 15;
|
| 203 | bits16 |= exponent16 << 10;
|
| 204 | bits16 |= mantissa16;
|
| 205 | return bitsUI16(bits16);
|
| 206 | }
|
| 207 |
|
| 208 | /**
|
| 209 | * @deprecated
|
| 210 | */
|
| 211 | public static byte[] bytesFloat16(double val) {
|
| 212 | return bitsToBytes(bitsFloat16(val), 16);
|
| 213 | }
|
| 214 |
|
| 215 | /**
|
| 216 | * @deprecated
|
| 217 | */
|
| 218 | public static BitSet bitsFloat32(double val) {
|
| 219 | Assert.inRange(val, BitUtils.MIN_FLOAT32, BitUtils.MAX_FLOAT32);
|
| 220 | return bitsSI32(Float.floatToIntBits((float) val));
|
| 221 | }
|
| 222 |
|
| 223 | /**
|
| 224 | * @deprecated
|
| 225 | */
|
| 226 | public static byte[] bytesFloat32(double val) {
|
| 227 | return bitsToBytes(bitsFloat32(val), 32);
|
| 228 | }
|
| 229 |
|
| 230 | /**
|
| 231 | * @deprecated
|
| 232 | */
|
| 233 | public static BitSet bitsFloat64(double val) {
|
| 234 | Assert.inRange(val, BitUtils.MIN_FLOAT64, BitUtils.MAX_FLOAT64);
|
| 235 | BitSet result;
|
| 236 | long longBits = Double.doubleToLongBits(val);
|
| 237 | long byte0 = (byte) (longBits >> 32);
|
| 238 | long byte1 = (byte) (longBits >> 40);
|
| 239 | long byte2 = (byte) (longBits >> 48);
|
| 240 | long byte3 = (byte) (longBits >> 56);
|
| 241 | long byte4 = (byte) (longBits);
|
| 242 | long byte5 = (byte) (longBits >> 8);
|
| 243 | long byte6 = (byte) (longBits >> 16);
|
| 244 | long byte7 = (byte) (longBits >> 24);
|
| 245 | result = bitsSI8(byte0);
|
| 246 | result = concat(result, bitsSI8(byte1), 8, 8);
|
| 247 | result = concat(result, bitsSI8(byte2), 16, 8);
|
| 248 | result = concat(result, bitsSI8(byte3), 24, 8);
|
| 249 | result = concat(result, bitsSI8(byte4), 32, 8);
|
| 250 | result = concat(result, bitsSI8(byte5), 40, 8);
|
| 251 | result = concat(result, bitsSI8(byte6), 48, 8);
|
| 252 | result = concat(result, bitsSI8(byte7), 56, 8);
|
| 253 | return result;
|
| 254 | }
|
| 255 |
|
| 256 | /**
|
| 257 | * @deprecated
|
| 258 | */
|
| 259 | public static byte[] bytesFloat64(double val) {
|
| 260 | return bitsToBytes(bitsFloat64(val), 64);
|
| 261 | }
|
| 262 |
|
| 263 | /**
|
| 264 | * @deprecated
|
| 265 | */
|
| 266 | public static BitSet bitsSBArray(long val, int bits) {
|
| 267 | Assert.inRange(val, bits, true);
|
| 268 | return longToBits(val, bits);
|
| 269 | }
|
| 270 |
|
| 271 | /**
|
| 272 | * @deprecated
|
| 273 | */
|
| 274 | public static byte[] bytesSBArray(long val, int bits) {
|
| 275 | return bitsToBytes(bitsSBArray(val, bits), bits);
|
| 276 | }
|
| 277 |
|
| 278 | /**
|
| 279 | * @deprecated
|
| 280 | */
|
| 281 | public static BitSet bitsUBArray(long val, int bits) {
|
| 282 | Assert.inRange(val, bits, false);
|
| 283 | return longToBits(val, bits);
|
| 284 | }
|
| 285 |
|
| 286 | /**
|
| 287 | * @deprecated
|
| 288 | */
|
| 289 | public static byte[] bytesUBArray(long val, int bits) {
|
| 290 | return bitsToBytes(bitsUBArray(val, bits), bits);
|
| 291 | }
|
| 292 |
|
| 293 | /**
|
| 294 | * @deprecated
|
| 295 | */
|
| 296 | public static BitSet bitsFBArray(double val, int bitNumber) {
|
| 297 | Assert.inRange(val, bitNumber);
|
| 298 | BitSet rawResult = bitsFixed1616(val);
|
| 299 | rawResult = flipBytes(rawResult, 4);
|
| 300 | BitSet result = new BitSet();
|
| 301 | result.set(0, rawResult.get(0));
|
| 302 | for (int i = 1; i < bitNumber; i++) {
|
| 303 | result.set(i, rawResult.get(i + (32 - bitNumber)));
|
| 304 | }
|
| 305 | return result;
|
| 306 | }
|
| 307 |
|
| 308 | /**
|
| 309 | * @deprecated
|
| 310 | */
|
| 311 | public static byte[] bytesFBArray(double val, int bitNumber) {
|
| 312 | return bitsToBytes(bitsFBArray(val, bitNumber), bitNumber);
|
| 313 | }
|
| 314 |
|
| 315 | /**
|
| 316 | * @deprecated
|
| 317 | */
|
| 318 | public static BitSet bitsEncodedU32(byte[] pos) {
|
| 319 | if (pos.length > 5) {
|
| 320 | throw new RuntimeException("trying to encode more then 5 bytes");
|
| 321 | }
|
| 322 | long result = pos[0];
|
| 323 | if ((result & 0x00000080) != 0) {
|
| 324 | return bitsUI32(result);
|
| 325 | }
|
| 326 | result = (result & 0x0000007f) | pos[1] << 7;
|
| 327 | if ((result & 0x00004000) != 0) {
|
| 328 | return bitsUI32(result);
|
| 329 | }
|
| 330 | result = (result & 0x00003fff) | pos[2] << 14;
|
| 331 | if ((result & 0x00200000) != 0) {
|
| 332 | return bitsUI32(result);
|
| 333 | }
|
| 334 | result = (result & 0x001fffff) | pos[3] << 21;
|
| 335 | if ((result & 0x10000000) != 0) {
|
| 336 | return bitsUI32(result);
|
| 337 | }
|
| 338 | result = (result & 0x0fffffff) | pos[4] << 28;
|
| 339 | return bitsUI32(result);
|
| 340 | }
|
| 341 |
|
| 342 | /**
|
| 343 | * @deprecated
|
| 344 | */
|
| 345 | public static byte[] bytesEncodedU32(byte[] pos) {
|
| 346 | return bitsToBytes(bitsEncodedU32(pos), 32);
|
| 347 | }
|
| 348 |
|
| 349 | // ///////////////////////////////////////////
|
| 350 | // Resusable utility methods
|
| 351 | // ///////////////////////////////////////////
|
| 352 | /**
|
| 353 | * @deprecated
|
| 354 | */
|
| 355 | public static BitSet longToBits(long val, int size) {
|
| 356 | String bits = Long.toBinaryString(val);
|
| 357 | if (bits.length() > size) {
|
| 358 | bits = bits.substring(bits.length() - size);
|
| 359 | }
|
| 360 | BitSet bitset = new BitSet(size);
|
| 361 | if (bits != null && bits.length() > 0) {
|
| 362 | int shift = size - bits.length();
|
| 363 | if (shift < 0) {
|
| 364 | throw new RuntimeException("type is larger then expected");
|
| 365 | }
|
| 366 | for (int i = 0; i < bits.length(); i++) {
|
| 367 | bitset.set(shift + i, (bits.charAt(i) == '1'));
|
| 368 | }
|
| 369 | }
|
| 370 | return bitset;
|
| 371 | }
|
| 372 |
|
| 373 | /**
|
| 374 | * @deprecated
|
| 375 | */
|
| 376 | public static long bitsToLong(BitSet bitSet, int numBits, boolean signed) {
|
| 377 | long result = 0l;
|
| 378 | if (!signed) {
|
| 379 | for (int i = 0; i < numBits; i++) {
|
| 380 | result |= ((long) (bitSet.get(i) ? 1 : 0) << (numBits - i - 1));
|
| 381 | }
|
| 382 | } else {
|
| 383 | for (int i = 1; i < numBits; i++) {
|
| 384 | result |= ((long) (bitSet.get(i) ? 1 : 0) << (numBits - i - 1));
|
| 385 | }
|
| 386 | if (bitSet.get(0)) {
|
| 387 | result -= Math.pow(2, numBits) / 2;
|
| 388 | }
|
| 389 | }
|
| 390 | return result;
|
| 391 | }
|
| 392 |
|
| 393 | /**
|
| 394 | * @deprecated
|
| 395 | */
|
| 396 | public static long bitsToLong(BitSet bitSet, int startPosition, int numBits, boolean signed) {
|
| 397 | long result = 0l;
|
| 398 | if (!signed) {
|
| 399 | for (int i = startPosition; i < (startPosition + numBits); i++) {
|
| 400 | result |= ((long) (bitSet.get(i) ? 1 : 0) << ((startPosition + numBits) - i - 1));
|
| 401 | }
|
| 402 | } else {
|
| 403 | for (int i = startPosition + 1; i < (startPosition + numBits); i++) {
|
| 404 | result |= ((long) (bitSet.get(i) ? 1 : 0) << ((startPosition + numBits) - i - 1));
|
| 405 | }
|
| 406 | if (bitSet.get(startPosition)) {
|
| 407 | result -= Math.pow(2, numBits) / 2;
|
| 408 | }
|
| 409 | }
|
| 410 | return result;
|
| 411 | }
|
| 412 |
|
| 413 | /**
|
| 414 | * @deprecated
|
| 415 | */
|
| 416 | static long bytesToLong(byte[] val, boolean signed) {
|
| 417 | long result = 0l;
|
| 418 | BitSet bitSet = bytesToBits(val);
|
| 419 | bitSet = flipBytes(bitSet, val.length);
|
| 420 | if (!signed) {
|
| 421 | for (int i = 0; i < val.length * 8; i++) {
|
| 422 | result |= ((long) (bitSet.get(i) ? 1 : 0) << (val.length * 8 - i - 1));
|
| 423 | }
|
| 424 | } else {
|
| 425 | for (int i = 1; i < val.length * 8; i++) {
|
| 426 | result |= ((long) (bitSet.get(i) ? 1 : 0) << (val.length * 8 - i - 1));
|
| 427 | }
|
| 428 | if (bitSet.get(0)) {
|
| 429 | result -= Math.pow(2, val.length * 8) / 2;
|
| 430 | }
|
| 431 |
|
| 432 | }
|
| 433 | return result;
|
| 434 | }
|
| 435 |
|
| 436 | /**
|
| 437 | * @deprecated
|
| 438 | */
|
| 439 | public static int calculateMaxBitCountUI(long[] data) {
|
| 440 | Arrays.sort(data);
|
| 441 | if (data[data.length - 1] == 0) {
|
| 442 | return 0;
|
| 443 | } else {
|
| 444 | return 32 - bitsToString(flipBytes(bitsUI32(data[data.length - 1]), 4), 32).indexOf('1') + 0;
|
| 445 | }
|
| 446 | }
|
| 447 |
|
| 448 | /**
|
| 449 | * @deprecated
|
| 450 | */
|
| 451 | public static int calculateMaxBitCountSI(long[] data) {
|
| 452 | for (int i = 0; i < data.length; i++) {
|
| 453 | if (data[i] < 0) {
|
| 454 | data[i] *= -1;
|
| 455 | }
|
| 456 | }
|
| 457 | Arrays.sort(data);
|
| 458 | if (data[data.length - 1] == 0) {
|
| 459 | return 1;
|
| 460 | } else {
|
| 461 | return 32 - bitsToString(flipBytes(bitsUI32(data[data.length - 1]), 4), 32).indexOf('1') + 1;
|
| 462 | }
|
| 463 | }
|
| 464 |
|
| 465 | /**
|
| 466 | * @deprecated
|
| 467 | */
|
| 468 | public static int calculateMaxBitCount(double[] data) {
|
| 469 | Arrays.sort(data);
|
| 470 | if (data[data.length - 1] == 0.0) {
|
| 471 | return 16;
|
| 472 | } else {
|
| 473 | int result = 32 - bitsToString(
|
| 474 | flipBytes(
|
| 475 | bitsFixed1616(data[data.length - 1]),
|
| 476 | 4), 32).
|
| 477 | indexOf('1') + 1;
|
| 478 | return (result > 15) ? result : 16;
|
| 479 | }
|
| 480 | }
|
| 481 |
|
| 482 | /**
|
| 483 | * @deprecated
|
| 484 | */
|
| 485 | public static byte[] bitsToBytes(BitSet bitSet, int bitNumber) {
|
| 486 | int byteNumber = ((bitNumber % 8) == 0) ? (bitNumber / 8) : (bitNumber / 8 + 1);
|
| 487 | byte[] result = new byte[byteNumber];
|
| 488 | for (int i = 0; i < byteNumber; i++) {
|
| 489 | int bin1 = (bitSet.get(i * 8)) ? 128 : 0;
|
| 490 | bin1 += (bitSet.get(i * 8 + 1)) ? 64 : 0;
|
| 491 | bin1 += (bitSet.get(i * 8 + 2)) ? 32 : 0;
|
| 492 | bin1 += (bitSet.get(i * 8 + 3)) ? 16 : 0;
|
| 493 |
|
| 494 | bin1 += (bitSet.get(i * 8 + 4)) ? 8 : 0;
|
| 495 | bin1 += (bitSet.get(i * 8 + 5)) ? 4 : 0;
|
| 496 | bin1 += (bitSet.get(i * 8 + 6)) ? 2 : 0;
|
| 497 | bin1 += (bitSet.get(i * 8 + 7)) ? 1 : 0;
|
| 498 | result[i] = (byte) bin1;
|
| 499 | }
|
| 500 | return result;
|
| 501 | }
|
| 502 |
|
| 503 | /**
|
| 504 | * @deprecated
|
| 505 | */
|
| 506 | public static BitSet bytesToBits(byte[] val) {
|
| 507 | BitSet result = new BitSet();
|
| 508 | for (int i = 0; i < val.length; i++) {
|
| 509 | byte b = val[i];
|
| 510 | result = concat(result, bitsSI8(b), i * 8, 8);
|
| 511 | }
|
| 512 | return result;
|
| 513 | }
|
| 514 |
|
| 515 | /**
|
| 516 | * @deprecated
|
| 517 | */
|
| 518 | public static BitSet concat(BitSet bs1, BitSet bs2, int size1, int size2) {
|
| 519 | BitSet result = new BitSet();
|
| 520 | for (int i = 0; i < size1; i++) {
|
| 521 | result.set(i, bs1.get(i));
|
| 522 | }
|
| 523 | for (int i = 0; i < size2; i++) {
|
| 524 | result.set(i + size1, bs2.get(i));
|
| 525 | }
|
| 526 | return result;
|
| 527 | }
|
| 528 |
|
| 529 | /**
|
| 530 | * @deprecated
|
| 531 | */
|
| 532 | public static BitSet flipBytes(BitSet val, int byteNumber) {
|
| 533 | BitSet result = new BitSet();
|
| 534 | for (int i = 0; i < byteNumber; i++) {
|
| 535 | for (int j = 0; j < 8; j++) {
|
| 536 | result.set(8 * i + j, val.get(8 * (byteNumber - 1 - i) + j));
|
| 537 | }
|
| 538 | }
|
| 539 | return result;
|
| 540 | }
|
| 541 |
|
| 542 | /**
|
| 543 | * @deprecated
|
| 544 | */
|
| 545 | public static String bitsToString(BitSet bitSet, int bitNumber) {
|
| 546 | StringBuffer sb = new StringBuffer("");
|
| 547 | for (int i = 0; i < bitNumber; i++) {
|
| 548 | sb.append((bitSet.get(i)) ? '1' : '0');
|
| 549 | }
|
| 550 | return sb.toString();
|
| 551 | }
|
| 552 |
|
| 553 | /**
|
| 554 | * @deprecated
|
| 555 | */
|
| 556 | public static String bin(BitSet bitSet, int bitNumber) {
|
| 557 | StringBuffer sb = new StringBuffer("");
|
| 558 | for (int i = 0; i < bitNumber; i++) {
|
| 559 | if (((i % 8) == 0) && (i != 0)) {
|
| 560 | sb.append(' ');
|
| 561 | }
|
| 562 | sb.append((bitSet.get(i)) ? '1' : '0');
|
| 563 | }
|
| 564 | return sb.toString();
|
| 565 | }
|
| 566 |
|
| 567 | /**
|
| 568 | * @deprecated
|
| 569 | */
|
| 570 | public static String bin(byte[] data) {
|
| 571 | return bin(bytesToBits(data), 8 * data.length);
|
| 572 | }
|
| 573 |
|
| 574 | /**
|
| 575 | * @deprecated
|
| 576 | */
|
| 577 | public static String hex(BitSet bitSet, int bitNumber) {
|
| 578 | StringBuffer sb = new StringBuffer("");
|
| 579 |
|
| 580 | int byteNumber = ((bitNumber % 8) == 0) ? (bitNumber / 8) : (bitNumber / 8 + 1);
|
| 581 | for (int i = 0; i < byteNumber; i++) {
|
| 582 | int bin1 = (bitSet.get(i * 8)) ? 8 : 0;
|
| 583 | bin1 += (bitSet.get(i * 8 + 1)) ? 4 : 0;
|
| 584 | bin1 += (bitSet.get(i * 8 + 2)) ? 2 : 0;
|
| 585 | bin1 += (bitSet.get(i * 8 + 3)) ? 1 : 0;
|
| 586 |
|
| 587 | int bin2 = (bitSet.get(i * 8 + 4)) ? 8 : 0;
|
| 588 | bin2 += (bitSet.get(i * 8 + 5)) ? 4 : 0;
|
| 589 | bin2 += (bitSet.get(i * 8 + 6)) ? 2 : 0;
|
| 590 | bin2 += (bitSet.get(i * 8 + 7)) ? 1 : 0;
|
| 591 | sb.append(BitUtils.hexChars[bin1]);
|
| 592 | sb.append(BitUtils.hexChars[bin2]);
|
| 593 | sb.append(' ');
|
| 594 | }
|
| 595 | return sb.toString();
|
| 596 | }
|
| 597 |
|
| 598 | /**
|
| 599 | * @deprecated
|
| 600 | */
|
| 601 | public static String hex(byte[] data) {
|
| 602 | int byteNumber = data.length;
|
| 603 | StringBuffer sb = new StringBuffer("");
|
| 604 | for (int i = 0; i < byteNumber; i++) {
|
| 605 | BitSet bitSet = bitsSI8(data[i]);
|
| 606 | int bin1 = (bitSet.get(0)) ? 8 : 0;
|
| 607 | bin1 += (bitSet.get(1)) ? 4 : 0;
|
| 608 | bin1 += (bitSet.get(2)) ? 2 : 0;
|
| 609 | bin1 += (bitSet.get(3)) ? 1 : 0;
|
| 610 |
|
| 611 | int bin2 = (bitSet.get(4)) ? 8 : 0;
|
| 612 | bin2 += (bitSet.get(5)) ? 4 : 0;
|
| 613 | bin2 += (bitSet.get(6)) ? 2 : 0;
|
| 614 | bin2 += (bitSet.get(7)) ? 1 : 0;
|
| 615 | sb.append(BitUtils.hexChars[bin1]);
|
| 616 | sb.append(BitUtils.hexChars[bin2]);
|
| 617 | sb.append(' ');
|
| 618 | }
|
| 619 | return sb.toString();
|
| 620 | }
|
| 621 |
|
| 622 | /**
|
| 623 | * @deprecated
|
| 624 | */
|
| 625 | public static byte[] hex(String data) {
|
| 626 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
| 627 | String[] strBytes = data.split(" ");
|
| 628 | for (int i = 0; i < strBytes.length; i++) {
|
| 629 | String strByte = strBytes[i];
|
| 630 | if (strByte.length() != 2) {
|
| 631 | continue;
|
| 632 | }
|
| 633 | byte oneByte = (byte) Long.parseLong(strByte, 16);
|
| 634 | baos.write(oneByte);
|
| 635 | }
|
| 636 | return baos.toByteArray();
|
| 637 | }
|
| 638 |
|
| 639 | /**
|
| 640 | * @deprecated
|
| 641 | */
|
| 642 | public static long parseUI8(byte[] val) {
|
| 643 | return bytesToLong(new byte[] { val[0] }, false);
|
| 644 | }
|
| 645 |
|
| 646 | /**
|
| 647 | * @deprecated
|
| 648 | */
|
| 649 | public static long parseUI16(byte[] val) {
|
| 650 | return bytesToLong(new byte[] { val[0], val[1] }, false);
|
| 651 | }
|
| 652 |
|
| 653 | /**
|
| 654 | * @deprecated
|
| 655 | */
|
| 656 | public static long parseUI24(byte[] val) {
|
| 657 | return bytesToLong(new byte[] { val[0], val[1], val[2] }, false);
|
| 658 | }
|
| 659 |
|
| 660 | /**
|
| 661 | * @deprecated
|
| 662 | */
|
| 663 | public static long parseUI32(byte[] val) {
|
| 664 | return bytesToLong(new byte[] { val[0], val[1], val[2], val[3] }, false);
|
| 665 | }
|
| 666 |
|
| 667 | /**
|
| 668 | * @deprecated
|
| 669 | */
|
| 670 | public static long parseSI8(byte[] val) {
|
| 671 | return bytesToLong(new byte[] { val[0] }, true);
|
| 672 | }
|
| 673 |
|
| 674 | /**
|
| 675 | * @deprecated
|
| 676 | */
|
| 677 | public static long parseSI16(byte[] val) {
|
| 678 | return bytesToLong(new byte[] { val[0], val[1] }, true);
|
| 679 | }
|
| 680 |
|
| 681 | /**
|
| 682 | * @deprecated
|
| 683 | */
|
| 684 | public static long parseSI32(byte[] val) {
|
| 685 | return bytesToLong(new byte[] { val[0], val[1], val[2], val[3] }, true);
|
| 686 | }
|
| 687 |
|
| 688 | /**
|
| 689 | * @deprecated
|
| 690 | */
|
| 691 | public static double parseFixed88(byte[] val) {
|
| 692 | return ((double) parseSI16(val)) / 256.0;
|
| 693 | }
|
| 694 |
|
| 695 | /**
|
| 696 | * @deprecated
|
| 697 | */
|
| 698 | public static double parseFixed1616(byte[] val) {
|
| 699 | return ((double) parseSI32(val)) / 65536.0;
|
| 700 | }
|
| 701 |
|
| 702 | /**
|
| 703 | * @deprecated
|
| 704 | */
|
| 705 | public static double parseFloat16(byte[] val) {
|
| 706 | BitSet bits16 = bytesToBits(val);
|
| 707 | bits16 = flipBytes(bits16, 2);
|
| 708 | BitSet bits32 = new BitSet();
|
| 709 | boolean sign = false;
|
| 710 | int exponent16 = 0;
|
| 711 | int exponent32 = 0;
|
| 712 | long mantissa16 = 0;
|
| 713 | long mantissa32 = 0;
|
| 714 | int intbits32 = 0;
|
| 715 | float result;
|
| 716 | sign = bits16.get(0);
|
| 717 | for (int i = 1; i < 6; i++) {
|
| 718 | exponent16 |= ((long) (bits16.get(i) ? 1 : 0) << (6 - i - 1));
|
| 719 | }
|
| 720 |
|
| 721 | for (int i = 6; i < 16; i++) {
|
| 722 | mantissa16 |= ((long) (bits16.get(i) ? 1 : 0) << (16 - i - 1));
|
| 723 | }
|
| 724 |
|
| 725 | exponent32 = exponent16 + 127 - 15;
|
| 726 | mantissa32 = mantissa16 << 13;
|
| 727 | bits32.set(0, sign);
|
| 728 | intbits32 |= sign ? 1 : 0 << 31;
|
| 729 | intbits32 |= exponent32 << 23;
|
| 730 | intbits32 |= mantissa32;
|
| 731 | result = Float.intBitsToFloat(intbits32);
|
| 732 | return result;
|
| 733 | }
|
| 734 |
|
| 735 | /**
|
| 736 | * @deprecated
|
| 737 | */
|
| 738 | public static double parseFloat32(byte[] val) {
|
| 739 | return Float.intBitsToFloat((int) parseSI32(val));
|
| 740 | }
|
| 741 |
|
| 742 | /**
|
| 743 | * @deprecated
|
| 744 | */
|
| 745 | public static double parseFloat64(byte[] val) {
|
| 746 | BitSet result = bytesToBits(new byte[] { val[3], val[2], val[1], val[0], val[7], val[6], val[5], val[4] });
|
| 747 | return Double.longBitsToDouble(bitsToLong(result, 64, true));
|
| 748 | }
|
| 749 |
|
| 750 | /**
|
| 751 | * @deprecated
|
| 752 | */
|
| 753 | public static long parseSBArray(BitSet val, int bits) {
|
| 754 | return bitsToLong(val, bits, true);
|
| 755 | }
|
| 756 |
|
| 757 | /**
|
| 758 | * @deprecated
|
| 759 | */
|
| 760 | public static long parseSBArray(byte[] val) {
|
| 761 | return bytesToLong(val, true);
|
| 762 | }
|
| 763 |
|
| 764 | /**
|
| 765 | * @deprecated
|
| 766 | */
|
| 767 | public static long parseUBArray(BitSet val, int bits) {
|
| 768 | return bitsToLong(val, bits, false);
|
| 769 | }
|
| 770 |
|
| 771 | /**
|
| 772 | * @deprecated
|
| 773 | */
|
| 774 | public static long parseUBArray(byte[] val) {
|
| 775 | return bytesToLong(val, false);
|
| 776 | }
|
| 777 |
|
| 778 | /**
|
| 779 | * @deprecated
|
| 780 | */
|
| 781 | public static double parseFBArray(BitSet val, int bitNumber) {
|
| 782 | double result = 0l;
|
| 783 | if (bitNumber < 32) {
|
| 784 | BitSet bits = concat(new BitSet(), val, 32 - bitNumber, 32);
|
| 785 | result = parseFixed1616(bitsToBytes(flipBytes(bits, 4), 32));
|
| 786 | } else if (bitNumber > 32) {
|
| 787 | BitSet bits = sub(val, 0, 32);
|
| 788 | result = parseFixed1616(bitsToBytes(flipBytes(bits, 4), 32));
|
| 789 | } else {
|
| 790 | result = parseFixed1616(bitsToBytes(flipBytes(val, 4), 32));
|
| 791 | }
|
| 792 | return result;
|
| 793 | }
|
| 794 |
|
| 795 | /**
|
| 796 | * @deprecated
|
| 797 | */
|
| 798 | public static BitSet sub(BitSet val, int start, int end) {
|
| 799 | BitSet result = new BitSet();
|
| 800 | for (int i = start; i <= end; i++) {
|
| 801 | result.set(i - start, val.get(i));
|
| 802 | }
|
| 803 | return result;
|
| 804 | }
|
| 805 |
|
| 806 | /**
|
| 807 | * @deprecated
|
| 808 | */
|
| 809 | public static BitSet stringToBits(String bits, int length) {
|
| 810 | BitSet bitset = new BitSet(length);
|
| 811 | if (bits != null && bits.length() > 0) {
|
| 812 | int shift = length - bits.length();
|
| 813 | if (shift < 0) {
|
| 814 | throw new RuntimeException("type is larger then expected");
|
| 815 | }
|
| 816 | for (int i = 0; i < bits.length(); i++) {
|
| 817 | bitset.set(shift + i, (bits.charAt(i) == '1'));
|
| 818 | }
|
| 819 | }
|
| 820 | return bitset;
|
| 821 | }
|
| 822 |
|
| 823 | }
|