Skip to content

Commit c4e0e1b

Browse files
committed
Add toLongArray() conversion
fixes #29
1 parent 8f1f21e commit c4e0e1b

File tree

6 files changed

+121
-47
lines changed

6 files changed

+121
-47
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* add `toCharArray()` method which decodes internal byte array to char[] #27
77
* add `encodeBase64()` supporting padding-less encoding
88
* add `toIntArray()` converter #28
9+
* add `toLongArray()` converter #29
910

1011
### Breaking
1112

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ Bytes.wrap(array).toInt();
507507
Bytes.wrap(array).toDouble();
508508
```
509509

510+
To primitive arrays
511+
512+
```java
513+
Bytes.wrap(array).toIntArray(); // of type int[]
514+
Bytes.wrap(array).toLongArray(); // of type long[]
515+
```
516+
510517
To other collections
511518

512519
```java
@@ -525,6 +532,7 @@ and others
525532

526533
```java
527534
Bytes.wrap(array).toUUID(); // convert 16 byte to UUID
535+
Bytes.wrap(array).toCharArray(StandardCharsets.UTF-8); // converts to encoded char array
528536
```
529537

530538
### Mutable and Read-Only

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,14 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.ByteArrayInputStream;
25-
import java.io.DataInput;
26-
import java.io.File;
27-
import java.io.InputStream;
28-
import java.io.Serializable;
24+
import java.io.*;
2925
import java.math.BigInteger;
30-
import java.nio.ByteBuffer;
31-
import java.nio.ByteOrder;
32-
import java.nio.CharBuffer;
33-
import java.nio.IntBuffer;
34-
import java.nio.ReadOnlyBufferException;
26+
import java.nio.*;
3527
import java.nio.charset.Charset;
3628
import java.nio.charset.StandardCharsets;
3729
import java.security.SecureRandom;
3830
import java.text.Normalizer;
39-
import java.util.Arrays;
40-
import java.util.BitSet;
41-
import java.util.Collection;
42-
import java.util.Iterator;
43-
import java.util.List;
44-
import java.util.Objects;
45-
import java.util.Random;
46-
import java.util.UUID;
31+
import java.util.*;
4732

4833
/**
4934
* Bytes is wrapper class for an byte-array that allows a lot of convenience operations on it:
@@ -1910,6 +1895,25 @@ public long toLong() {
19101895
return longAt(0);
19111896
}
19121897

1898+
/**
1899+
* Converts the internal byte array to an long array, that is, every 8 bytes will be packed into a single long.
1900+
* <p>
1901+
* E.g. 8 bytes will be packed to a length 1 long array:
1902+
* <pre>
1903+
* [b1, b2, b3, b4, b5, b6, b7, b8] = [int1]
1904+
* </pre>
1905+
* <p>
1906+
* This conversion respects the internal byte order. Will only work if all bytes can be directly mapped to long,
1907+
* which means the byte array length must be dividable by 8 without rest.
1908+
*
1909+
* @return new long[] instance representing this byte array
1910+
* @throws IllegalArgumentException if internal byte length mod 8 != 0
1911+
*/
1912+
public long[] toLongArray() {
1913+
Util.Validation.checkModLength(length(), 8, "creating an long array");
1914+
return Util.Converter.toLongArray(internalArray(), byteOrder);
1915+
}
1916+
19131917
/**
19141918
* If the underlying byte array is exactly 4 byte / 32 bit long, return the
19151919
* representation for a Java float value. The output is dependent on the set {@link #byteOrder()}.

src/main/java/at/favre/lib/bytes/Util.java

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,12 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.ByteArrayOutputStream;
25-
import java.io.DataInput;
26-
import java.io.IOException;
27-
import java.io.InputStream;
28-
import java.io.RandomAccessFile;
29-
import java.nio.ByteBuffer;
30-
import java.nio.ByteOrder;
31-
import java.nio.CharBuffer;
32-
import java.nio.IntBuffer;
24+
import java.io.*;
25+
import java.nio.*;
3326
import java.nio.charset.CharacterCodingException;
3427
import java.nio.charset.Charset;
3528
import java.nio.file.Files;
36-
import java.util.ArrayList;
37-
import java.util.Arrays;
38-
import java.util.Collection;
39-
import java.util.HashMap;
40-
import java.util.Iterator;
41-
import java.util.List;
42-
import java.util.Map;
43-
import java.util.NoSuchElementException;
44-
import java.util.Objects;
45-
import java.util.Random;
46-
import java.util.UUID;
29+
import java.util.*;
4730

4831
/**
4932
* Common Util methods to convert or modify byte arrays
@@ -458,22 +441,36 @@ static char[] byteToCharArray(byte[] bytes, Charset charset, ByteOrder byteOrder
458441
* Converts the byte array to an int array. This will spread 4 bytes into a single int:
459442
*
460443
* <pre>
461-
* [b1, b2, b3, b4] = int1
444+
* [b1, b2, b3, b4] = [int1]
462445
* </pre>
463446
*
464-
* @param bytes to convert to int array
447+
* @param bytes to convert to int array, must be % 4 == 0 to work correctly
465448
* @param byteOrder of the byte array
466449
* @return int array
467450
*/
468451
static int[] toIntArray(byte[] bytes, ByteOrder byteOrder) {
469452
IntBuffer buffer = ByteBuffer.wrap(bytes).order(byteOrder).asIntBuffer();
470-
if (buffer.hasArray()) {
471-
return buffer.array();
472-
} else {
473-
int[] array = new int[buffer.remaining()];
474-
buffer.get(array);
475-
return array;
476-
}
453+
int[] array = new int[buffer.remaining()];
454+
buffer.get(array);
455+
return array;
456+
}
457+
458+
/**
459+
* Converts the byte array to an long array. This will spread 8 bytes into a single long:
460+
*
461+
* <pre>
462+
* [b1, b2, b3, b4, b5, b6, b7, b8] = [long1]
463+
* </pre>
464+
*
465+
* @param bytes to convert to long array, must be % 8 == 0 to work correctly
466+
* @param byteOrder of the byte array
467+
* @return long array
468+
*/
469+
static long[] toLongArray(byte[] bytes, ByteOrder byteOrder) {
470+
LongBuffer buffer = ByteBuffer.wrap(bytes).order(byteOrder).asLongBuffer();
471+
long[] array = new long[buffer.remaining()];
472+
buffer.get(array);
473+
return array;
477474
}
478475

479476
/**

src/test/java/at/favre/lib/bytes/BytesToConvertOtherTypesTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,50 @@ public void testToIntArrayLittleEndian() {
276276
assertArrayEquals(new int[]{257, 1}, Bytes.wrap(new byte[]{1, 1, 0, 0, 1, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN).toIntArray());
277277
assertArrayEquals(new int[]{257, 65_793, 1}, Bytes.wrap(new byte[]{1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN).toIntArray());
278278
}
279+
280+
@Test
281+
public void testToLongArray() {
282+
assertArrayEquals(new long[]{1}, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 1}).toLongArray());
283+
assertArrayEquals(new long[]{257}, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 1, 1}).toLongArray());
284+
assertArrayEquals(new long[]{3329575617569751109L}, Bytes.wrap(new byte[]{46, 53, 7, 98, 34, 12, 0, 69}).toLongArray());
285+
assertArrayEquals(new long[]{-7124130559744646145L}, Bytes.wrap(new byte[]{(byte) 157, 34, 1, 0, 76, (byte) 234, 99, (byte) 255}).toLongArray());
286+
287+
assertArrayEquals(new long[]{1, 1}, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}).toLongArray());
288+
assertArrayEquals(new long[]{257, 1}, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1}).toLongArray());
289+
assertArrayEquals(new long[]{1099511628033L, 281474976776449L, 1}, Bytes.wrap(new byte[]{
290+
0, 0, 1, 0, 0, 0, 1, 1,
291+
0, 1, 0, 0, 0, 1, 1, 1,
292+
0, 0, 0, 0, 0, 0, 0, 1}).toLongArray());
293+
}
294+
295+
@Test(expected = IllegalArgumentException.class)
296+
public void testToLongArrayNotMod4Was9Byte() {
297+
Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 1}).toLongArray();
298+
}
299+
300+
@Test(expected = IllegalArgumentException.class)
301+
public void testToLongArrayNotMod4Only7Byte() {
302+
Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 1}).toLongArray();
303+
}
304+
305+
@Test
306+
public void testToLongEmptyArray() {
307+
assertArrayEquals(new long[0], Bytes.empty().toLongArray());
308+
}
309+
310+
311+
@Test
312+
public void testToLongArrayLittleEndian() {
313+
assertArrayEquals(new long[]{1}, Bytes.wrap(new byte[]{1, 0, 0, 0, 0, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN).toLongArray());
314+
assertArrayEquals(new long[]{257}, Bytes.wrap(new byte[]{1, 1, 0, 0, 0, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN).toLongArray());
315+
assertArrayEquals(new long[]{3329575617569751109L}, Bytes.wrap(new byte[]{69, 0, 12, 34, 98, 7, 53, 46}, ByteOrder.LITTLE_ENDIAN).toLongArray());
316+
assertArrayEquals(new long[]{-7124130559744646145L}, Bytes.wrap(new byte[]{(byte) 255, 99, (byte) 234, 76, 0, 1, 34, (byte) 157}, ByteOrder.LITTLE_ENDIAN).toLongArray());
317+
318+
assertArrayEquals(new long[]{1, 1}, Bytes.wrap(new byte[]{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN).toLongArray());
319+
assertArrayEquals(new long[]{257, 1}, Bytes.wrap(new byte[]{1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN).toLongArray());
320+
assertArrayEquals(new long[]{1099511628033L, 281474976776449L, 1}, Bytes.wrap(new byte[]{
321+
1, 1, 0, 0, 0, 1, 0, 0,
322+
1, 1, 1, 0, 0, 0, 1, 0,
323+
1, 0, 0, 0, 0, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN).toLongArray());
324+
}
279325
}

src/test/java/at/favre/lib/bytes/UtilConverterTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,22 @@ public void testToIntArray() {
137137
assertArrayEquals(new int[0], Util.Converter.toIntArray(new byte[0], ByteOrder.LITTLE_ENDIAN));
138138
assertArrayEquals(new int[0], Util.Converter.toIntArray(new byte[0], ByteOrder.BIG_ENDIAN));
139139
}
140+
141+
@Test
142+
public void testToLongArray() {
143+
assertArrayEquals(new long[]{1}, Util.Converter.toLongArray(new byte[]{0, 0, 0, 0, 0, 0, 0, 1}, ByteOrder.BIG_ENDIAN));
144+
assertArrayEquals(new long[]{257}, Util.Converter.toLongArray(new byte[]{0, 0, 0, 0, 0, 0, 1, 1}, ByteOrder.BIG_ENDIAN));
145+
146+
assertArrayEquals(new long[]{1}, Util.Converter.toLongArray(new byte[]{1, 0, 0, 0, 0, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN));
147+
assertArrayEquals(new long[]{257}, Util.Converter.toLongArray(new byte[]{1, 1, 0, 0, 0, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN));
148+
149+
assertArrayEquals(new long[]{-7124130559744646145L}, Util.Converter.toLongArray(new byte[]{(byte) 157, 34, 1, 0, 76, (byte) 234, 99, (byte) 255}, ByteOrder.BIG_ENDIAN));
150+
assertArrayEquals(new long[]{-7124130559744646145L}, Util.Converter.toLongArray(new byte[]{(byte) 255, 99, (byte) 234, 76, 0, 1, 34, (byte) 157}, ByteOrder.LITTLE_ENDIAN));
151+
152+
assertArrayEquals(new long[]{257, 1}, Util.Converter.toLongArray(new byte[]{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1}, ByteOrder.BIG_ENDIAN));
153+
assertArrayEquals(new long[]{257, 1}, Util.Converter.toLongArray(new byte[]{1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN));
154+
155+
assertArrayEquals(new long[0], Util.Converter.toLongArray(new byte[0], ByteOrder.LITTLE_ENDIAN));
156+
assertArrayEquals(new long[0], Util.Converter.toLongArray(new byte[0], ByteOrder.BIG_ENDIAN));
157+
}
140158
}

0 commit comments

Comments
 (0)