blob: 378fbb1e6c9f700c67d5defed2e8566c1f2624d2 [file] [log] [blame]
Nils Diewaldf399a672013-11-18 17:55:22 +00001package de.ids_mannheim.korap.util;
2
3import java.util.*;
4
Nils Diewaldf399a672013-11-18 17:55:22 +00005/**
Nils Diewaldfe6a3652015-02-05 20:34:27 +00006 * A collection of byte and byte array related
7 * utility functions.
Nils Diewaldbb33da22015-03-04 16:24:25 +00008 *
Nils Diewaldfe6a3652015-02-05 20:34:27 +00009 * @author diewald
Nils Diewaldf399a672013-11-18 17:55:22 +000010 */
Nils Diewaldc383ed02015-02-26 21:35:22 +000011public class KrillByte {
Nils Diewaldf399a672013-11-18 17:55:22 +000012
13 /**
14 * Convert an integer to a byte array.
Nils Diewaldbb33da22015-03-04 16:24:25 +000015 *
16 * @param number
17 * The number to convert.
Nils Diewaldfe6a3652015-02-05 20:34:27 +000018 * @return The translated byte array.
Nils Diewaldf399a672013-11-18 17:55:22 +000019 */
Nils Diewaldfe6a3652015-02-05 20:34:27 +000020 // Based on
21 // http://www.tutorials.de/java/228129-konvertierung-von-integer-byte-array.html
Akron700c1eb2015-09-25 16:57:30 +020022 // CHECK: int2byte may be out of use
Nils Diewaldf399a672013-11-18 17:55:22 +000023 public static byte[] int2byte (int number) {
Nils Diewaldfe6a3652015-02-05 20:34:27 +000024 byte[] data = new byte[4];
25 for (int i = 0; i < 4; ++i) {
26 int shift = i << 3; // That's identical to i * 8
Nils Diewaldbb33da22015-03-04 16:24:25 +000027 data[3 - i] = (byte) ((number & (0xff << shift)) >>> shift);
Nils Diewaldfe6a3652015-02-05 20:34:27 +000028 };
29 return data;
Nils Diewaldf399a672013-11-18 17:55:22 +000030 };
31
Nils Diewaldfe6a3652015-02-05 20:34:27 +000032
Nils Diewaldf399a672013-11-18 17:55:22 +000033 /**
34 * Convert a byte array to an integer.
Nils Diewaldbb33da22015-03-04 16:24:25 +000035 *
36 * @param data
37 * The byte array to convert.
Nils Diewaldfe6a3652015-02-05 20:34:27 +000038 * @return The translated integer.
Nils Diewaldf399a672013-11-18 17:55:22 +000039 */
Nils Diewaldfe6a3652015-02-05 20:34:27 +000040 // Based on
41 // http://www.tutorials.de/java/228129-konvertierung-von-integer-byte-array.html
Nils Diewaldf399a672013-11-18 17:55:22 +000042 public static int byte2int (byte[] data) {
Nils Diewaldfe6a3652015-02-05 20:34:27 +000043 return byte2int(data, 0);
Nils Diewaldf399a672013-11-18 17:55:22 +000044 };
45
Nils Diewaldf399a672013-11-18 17:55:22 +000046
Nils Diewaldfe6a3652015-02-05 20:34:27 +000047 /**
48 * Convert a byte array to an integer.
Nils Diewaldbb33da22015-03-04 16:24:25 +000049 *
50 * @param data
51 * The byte array to convert.
52 * @param offset
53 * The byte offset (Not integer offset!).
Nils Diewaldfe6a3652015-02-05 20:34:27 +000054 * @return The translated integer.
55 */
Nils Diewald7a8e9252015-02-06 20:16:04 +000056 // Roughly based on
Nils Diewaldfe6a3652015-02-05 20:34:27 +000057 // http://www.tutorials.de/java/228129-konvertierung-von-integer-byte-array.html
58 public static int byte2int (byte[] data, int offset) {
Nils Diewald7a8e9252015-02-06 20:16:04 +000059 offset += 3;
60 int number = 0;
61 for (int i = 0; i < 4; ++i)
Nils Diewaldbb33da22015-03-04 16:24:25 +000062 number |= (data[offset - i] & 0xff) << (i << 3);
Nils Diewaldfe6a3652015-02-05 20:34:27 +000063 return number;
Nils Diewaldf399a672013-11-18 17:55:22 +000064 };
Akrona7b936d2016-03-04 13:40:54 +010065
66
67 /**
68 * Return the unsigned value of a byte.
69 *
70 * @param data
71 * The byte.
Akron6759b042016-04-28 01:25:00 +020072 */
Akrona7b936d2016-03-04 13:40:54 +010073 public static int unsignedByte (byte data) {
74 return (int) data & 0xFF;
75 };
Nils Diewaldf399a672013-11-18 17:55:22 +000076};