@@ -23,6 +23,10 @@ public enum ByteUtils {
2323
2424 public static final VarHandle BIG_ENDIAN_LONG = MethodHandles .byteArrayViewVarHandle (long [].class , ByteOrder .BIG_ENDIAN );
2525
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+
2630 /** Zig-zag decode. */
2731 public static long zigZagDecode (long n ) {
2832 return ((n >>> 1 ) ^ -(n & 1 ));
@@ -33,54 +37,150 @@ public static long zigZagEncode(long n) {
3337 return (n >> 63 ) ^ (n << 1 );
3438 }
3539
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 );
3949 }
4050
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+ */
4257 public static long readLongLE (byte [] arr , int offset ) {
4358 return (long ) LITTLE_ENDIAN_LONG .get (arr , offset );
4459 }
4560
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 );
4970 }
5071
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+ */
5278 public static long readLongBE (byte [] arr , int offset ) {
5379 return (long ) BIG_ENDIAN_LONG .get (arr , offset );
5480 }
5581
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 );
5991 }
6092
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+ */
6299 public static int readIntLE (byte [] arr , int offset ) {
63100 return (int ) LITTLE_ENDIAN_INT .get (arr , offset );
64101 }
65102
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 );
69112 }
70113
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+ */
72120 public static double readDoubleLE (byte [] arr , int offset ) {
73121 return Double .longBitsToDouble (readLongLE (arr , offset ));
74122 }
75123
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 );
79133 }
80134
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+ */
82141 public static float readFloatLE (byte [] arr , int offset ) {
83142 return Float .intBitsToFloat (readIntLE (arr , offset ));
84143 }
85144
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+ }
86186}
0 commit comments