@@ -23,6 +23,10 @@ public enum ByteUtils {
23
23
24
24
public static final VarHandle BIG_ENDIAN_LONG = MethodHandles .byteArrayViewVarHandle (long [].class , ByteOrder .BIG_ENDIAN );
25
25
26
+ private static final VarHandle BIG_ENDIAN_SHORT = MethodHandles .byteArrayViewVarHandle (short [].class , ByteOrder .BIG_ENDIAN );
27
+
28
+ private static final VarHandle BIG_ENDIAN_INT = MethodHandles .byteArrayViewVarHandle (int [].class , ByteOrder .BIG_ENDIAN );
29
+
26
30
/** Zig-zag decode. */
27
31
public static long zigZagDecode (long n ) {
28
32
return ((n >>> 1 ) ^ -(n & 1 ));
@@ -33,54 +37,150 @@ public static long zigZagEncode(long n) {
33
37
return (n >> 63 ) ^ (n << 1 );
34
38
}
35
39
36
- /** Write a long in little-endian format. */
37
- public static void writeLongLE (long l , byte [] arr , int offset ) {
38
- LITTLE_ENDIAN_LONG .set (arr , offset , l );
40
+ /**
41
+ * Converts a long to a byte array in little endian format.
42
+ *
43
+ * @param val The long to convert to a byte array
44
+ * @param arr The byte array to write the long value in little endian layout
45
+ * @param offset The offset where in the array to write to
46
+ */
47
+ public static void writeLongLE (long val , byte [] arr , int offset ) {
48
+ LITTLE_ENDIAN_LONG .set (arr , offset , val );
39
49
}
40
50
41
- /** Write a long in little-endian format. */
51
+ /**
52
+ * Converts a byte array written in little endian format to a long.
53
+ *
54
+ * @param arr The byte array to read from in little endian layout
55
+ * @param offset The offset where in the array to read from
56
+ */
42
57
public static long readLongLE (byte [] arr , int offset ) {
43
58
return (long ) LITTLE_ENDIAN_LONG .get (arr , offset );
44
59
}
45
60
46
- /** Write a long in big-endian format. */
47
- public static void writeLongBE (long l , byte [] arr , int offset ) {
48
- BIG_ENDIAN_LONG .set (arr , offset , l );
61
+ /**
62
+ * Converts a long to a byte array in big endian format.
63
+ *
64
+ * @param val The long to convert to a byte array
65
+ * @param arr The byte array to write the long value in big endian layout
66
+ * @param offset The offset where in the array to write to
67
+ */
68
+ public static void writeLongBE (long val , byte [] arr , int offset ) {
69
+ BIG_ENDIAN_LONG .set (arr , offset , val );
49
70
}
50
71
51
- /** Write a long in big-endian format. */
72
+ /**
73
+ * Converts a byte array written in big endian format to a long.
74
+ *
75
+ * @param arr The byte array to read from in big endian layout
76
+ * @param offset The offset where in the array to read from
77
+ */
52
78
public static long readLongBE (byte [] arr , int offset ) {
53
79
return (long ) BIG_ENDIAN_LONG .get (arr , offset );
54
80
}
55
81
56
- /** Write an int in little-endian format. */
57
- public static void writeIntLE (int l , byte [] arr , int offset ) {
58
- LITTLE_ENDIAN_INT .set (arr , offset , l );
82
+ /**
83
+ * Converts an int to a byte array in little endian format.
84
+ *
85
+ * @param val The int to convert to a byte array
86
+ * @param arr The byte array to write the int value in little endian layout
87
+ * @param offset The offset where in the array to write to
88
+ */
89
+ public static void writeIntLE (int val , byte [] arr , int offset ) {
90
+ LITTLE_ENDIAN_INT .set (arr , offset , val );
59
91
}
60
92
61
- /** Read an int in little-endian format. */
93
+ /**
94
+ * Converts a byte array written in little endian format to an int.
95
+ *
96
+ * @param arr The byte array to read from in little endian layout
97
+ * @param offset The offset where in the array to read from
98
+ */
62
99
public static int readIntLE (byte [] arr , int offset ) {
63
100
return (int ) LITTLE_ENDIAN_INT .get (arr , offset );
64
101
}
65
102
66
- /** Write a double in little-endian format. */
67
- public static void writeDoubleLE (double d , byte [] arr , int offset ) {
68
- writeLongLE (Double .doubleToRawLongBits (d ), arr , offset );
103
+ /**
104
+ * Converts a double to a byte array in little endian format.
105
+ *
106
+ * @param val The double to convert to a byte array
107
+ * @param arr The byte array to write the double value in little endian layout
108
+ * @param offset The offset where in the array to write to
109
+ */
110
+ public static void writeDoubleLE (double val , byte [] arr , int offset ) {
111
+ writeLongLE (Double .doubleToRawLongBits (val ), arr , offset );
69
112
}
70
113
71
- /** Read a double in little-endian format. */
114
+ /**
115
+ * Converts a byte array written in little endian format to a double.
116
+ *
117
+ * @param arr The byte array to read from in little endian layout
118
+ * @param offset The offset where in the array to read from
119
+ */
72
120
public static double readDoubleLE (byte [] arr , int offset ) {
73
121
return Double .longBitsToDouble (readLongLE (arr , offset ));
74
122
}
75
123
76
- /** Write a float in little-endian format. */
77
- public static void writeFloatLE (float d , byte [] arr , int offset ) {
78
- writeIntLE (Float .floatToRawIntBits (d ), arr , offset );
124
+ /**
125
+ * Converts a float to a byte array in little endian format.
126
+ *
127
+ * @param val The float to convert to a byte array
128
+ * @param arr The byte array to write the float value in little endian layout
129
+ * @param offset The offset where in the array to write to
130
+ */
131
+ public static void writeFloatLE (float val , byte [] arr , int offset ) {
132
+ writeIntLE (Float .floatToRawIntBits (val ), arr , offset );
79
133
}
80
134
81
- /** Read a float in little-endian format. */
135
+ /**
136
+ * Converts a byte array written in little endian format to a float.
137
+ *
138
+ * @param arr The byte array to read from in little endian layout
139
+ * @param offset The offset where in the array to read from
140
+ */
82
141
public static float readFloatLE (byte [] arr , int offset ) {
83
142
return Float .intBitsToFloat (readIntLE (arr , offset ));
84
143
}
85
144
145
+ /**
146
+ * Converts an int to a byte array in big endian format.
147
+ *
148
+ * @param val The int to convert to a byte array
149
+ * @param arr The byte array to write the int value in big endian layout
150
+ * @param offset The offset where in the array to write to
151
+ */
152
+ public static void writeIntBE (int val , byte [] arr , int offset ) {
153
+ BIG_ENDIAN_INT .set (arr , offset , val );
154
+ }
155
+
156
+ /**
157
+ * Converts a byte array written in big endian format to an int.
158
+ *
159
+ * @param arr The byte array to read from in big endian layout
160
+ * @param offset The offset where in the array to read from
161
+ */
162
+ public static int readIntBE (byte [] arr , int offset ) {
163
+ return (int ) BIG_ENDIAN_INT .get (arr , offset );
164
+ }
165
+
166
+ /**
167
+ * Converts a short to a byte array in big endian format.
168
+ *
169
+ * @param val The short to convert to a byte array
170
+ * @param arr The byte array to write the short value in big endian layout
171
+ * @param offset The offset where in the array to write to
172
+ */
173
+ public static void writeShortBE (short val , byte [] arr , int offset ) {
174
+ BIG_ENDIAN_SHORT .set (arr , offset , val );
175
+ }
176
+
177
+ /**
178
+ * Converts a byte array written in big endian format to a short.
179
+ *
180
+ * @param arr The byte array to read from in big endian layout
181
+ * @param offset The offset where in the array to read from
182
+ */
183
+ public static short readShortBE (byte [] arr , int offset ) {
184
+ return (short ) BIG_ENDIAN_SHORT .get (arr , offset );
185
+ }
86
186
}
0 commit comments