Skip to content

Commit 7a04294

Browse files
authored
Merge pull request #44 from hlyakhovich/to-short-array
Add toShortArray
2 parents 09c00a6 + f3a0c68 commit 7a04294

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,25 @@ public double[] toDoubleArray() {
20442044
return Util.Converter.toDoubleArray(internalArray(), byteOrder);
20452045
}
20462046

2047+
/**
2048+
* Converts the internal byte array to a short array, that is, every 2 bytes will be packed into a single short.
2049+
* <p>
2050+
* E.g. 2 bytes will be packed to a length 1 short array:
2051+
* <pre>
2052+
* [b1, b2] = [short1]
2053+
* </pre>
2054+
* <p>
2055+
* This conversion respects the internal byte order. Will only work if all bytes can be directly mapped to short,
2056+
* which means the byte array length must be dividable by 2 without rest.
2057+
*
2058+
* @return new short[] instance representing this byte array
2059+
* @throws IllegalArgumentException if internal byte length mod 2 != 0
2060+
*/
2061+
public short[] toShortArray() {
2062+
Util.Validation.checkModLength(length(), 2, "creating a short array");
2063+
return Util.Converter.toShortArray(internalArray(), byteOrder);
2064+
}
2065+
20472066
/**
20482067
* Decodes the internal byte array to UTF-8 char array.
20492068
* This implementation will not internally create a {@link String}.

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,34 @@ static double[] toDoubleArray(byte[] bytes, ByteOrder byteOrder) {
860860
return array;
861861
}
862862

863+
864+
/**
865+
* Converts the byte array to a short array. This will spread 2 bytes into a single short:
866+
*
867+
* <pre>
868+
* [b1, b2] = [short1]
869+
* </pre>
870+
*
871+
* <p>
872+
* <strong>Analysis</strong>
873+
* <ul>
874+
* <li>Time Complexity: <code>O(n)</code></li>
875+
* <li>Space Complexity: <code>O(n)</code></li>
876+
* <li>Alters Parameters: <code>false</code></li>
877+
* </ul>
878+
* </p>
879+
*
880+
* @param bytes to convert to short array, must be % 2 == 0 to work correctly
881+
* @param byteOrder of the byte array
882+
* @return short array
883+
*/
884+
static short[] toShortArray(byte[] bytes, ByteOrder byteOrder) {
885+
ShortBuffer buffer = ByteBuffer.wrap(bytes).order(byteOrder).asShortBuffer();
886+
short[] array = new short[buffer.remaining()];
887+
buffer.get(array);
888+
return array;
889+
}
890+
863891
/**
864892
* Convert UUID to a newly generated 16 byte long array representation. Puts the 8 byte most significant bits and
865893
* 8 byte least significant bits into an byte array.

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,38 @@ public void testToDoubleArrayLittleEndian() {
406406
1, 1, 1, 0, 0, 0, 1, 0,
407407
1, 0, 0, 0, 0, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN).toDoubleArray(), 0.01);
408408
}
409+
410+
@Test
411+
public void testToShortArrayEmpty() {
412+
assertArrayEquals(new short[0], Bytes.empty().toShortArray());
413+
}
414+
415+
416+
@Test(expected = IllegalArgumentException.class)
417+
public void testToShortArrayNotMod2Was5Byte() {
418+
Bytes.wrap(new byte[]{0, 0, 0, 0, 1}).toShortArray();
419+
}
420+
421+
@Test
422+
public void testToShortArray() {
423+
assertArrayEquals(new short[]{1}, Bytes.wrap(new byte[]{0, 1}).toShortArray());
424+
assertArrayEquals(new short[]{257}, Bytes.wrap(new byte[]{1, 1}).toShortArray());
425+
assertArrayEquals(new short[]{32_767}, Bytes.wrap(new byte[]{127, -1}).toShortArray());
426+
427+
assertArrayEquals(new short[]{1, 1}, Bytes.wrap(new byte[]{0, 1, 0, 1}).toShortArray());
428+
assertArrayEquals(new short[]{257, 1}, Bytes.wrap(new byte[]{1, 1, 0, 1}).toShortArray());
429+
assertArrayEquals(new short[]{257, 32_767, 1}, Bytes.wrap(new byte[]{1, 1, 127, -1, 0, 1}).toShortArray());
430+
}
431+
432+
@Test
433+
public void testToShortArrayLittleEndian() {
434+
assertArrayEquals(new short[]{1}, Bytes.wrap(new byte[]{1, 0}, ByteOrder.LITTLE_ENDIAN).toShortArray());
435+
assertArrayEquals(new short[]{257}, Bytes.wrap(new byte[]{1, 1}, ByteOrder.LITTLE_ENDIAN).toShortArray());
436+
assertArrayEquals(new short[]{32_767}, Bytes.wrap(new byte[]{-1, 127}, ByteOrder.LITTLE_ENDIAN).toShortArray());
437+
438+
assertArrayEquals(new short[]{1, 1}, Bytes.wrap(new byte[]{1, 0, 1, 0}, ByteOrder.LITTLE_ENDIAN).toShortArray());
439+
assertArrayEquals(new short[]{257, 1}, Bytes.wrap(new byte[]{1, 1, 1, 0}, ByteOrder.LITTLE_ENDIAN).toShortArray());
440+
assertArrayEquals(new short[]{257, 32_767, 1}, Bytes.wrap(new byte[]{1, 1, -1, 127, 1, 0}, ByteOrder.LITTLE_ENDIAN).toShortArray());
441+
}
442+
409443
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,20 @@ public void testToDoubleArray() {
193193
assertArrayEquals(new double[0], Util.Converter.toDoubleArray(new byte[0], ByteOrder.LITTLE_ENDIAN), 0.01);
194194
assertArrayEquals(new double[0], Util.Converter.toDoubleArray(new byte[0], ByteOrder.BIG_ENDIAN), 0.01);
195195
}
196+
197+
@Test
198+
public void testToShortArray() {
199+
assertArrayEquals(new short[]{1}, Util.Converter.toShortArray(new byte[]{0, 1}, ByteOrder.BIG_ENDIAN));
200+
assertArrayEquals(new short[]{257}, Util.Converter.toShortArray(new byte[]{1, 1}, ByteOrder.BIG_ENDIAN));
201+
assertArrayEquals(new short[]{32_767}, Util.Converter.toShortArray(new byte[]{127, -1}, ByteOrder.BIG_ENDIAN));
202+
assertArrayEquals(new short[]{257, 32_767, 1}, Util.Converter.toShortArray(new byte[]{1, 1, 127, -1, 0, 1}, ByteOrder.BIG_ENDIAN));
203+
204+
assertArrayEquals(new short[]{1}, Util.Converter.toShortArray(new byte[]{1, 0}, ByteOrder.LITTLE_ENDIAN));
205+
assertArrayEquals(new short[]{257}, Util.Converter.toShortArray(new byte[]{1, 1}, ByteOrder.LITTLE_ENDIAN));
206+
assertArrayEquals(new short[]{32_767}, Util.Converter.toShortArray(new byte[]{-1, 127}, ByteOrder.LITTLE_ENDIAN));
207+
assertArrayEquals(new short[]{257, 32_767, 1}, Util.Converter.toShortArray(new byte[]{1, 1, -1, 127, 1, 0}, ByteOrder.LITTLE_ENDIAN));
208+
209+
assertArrayEquals(new short[0], Util.Converter.toShortArray(new byte[0], ByteOrder.LITTLE_ENDIAN));
210+
assertArrayEquals(new short[0], Util.Converter.toShortArray(new byte[0], ByteOrder.BIG_ENDIAN));
211+
}
196212
}

0 commit comments

Comments
 (0)