Skip to content

Commit c0f45ad

Browse files
committed
Overload from() with short array
1 parent 7a04294 commit c0f45ad

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ public static Bytes from(short short2Byte) {
296296
return wrap(ByteBuffer.allocate(2).putShort(short2Byte).array());
297297
}
298298

299+
/**
300+
* Creates a new instance from given 2 byte short array.
301+
*
302+
* @param shortArray to create from
303+
* @return new instance
304+
*/
305+
public static Bytes from(short... shortArray) {
306+
return wrap(Util.Converter.toByteArray(Objects.requireNonNull(shortArray, "must provide at least a single short")));
307+
}
308+
299309
/**
300310
* Creates a new instance from given 4 byte integer.
301311
*
@@ -1492,7 +1502,7 @@ public Bytes duplicate() {
14921502
/**
14931503
* Set the byte order or endianness of this instance. Default in Java is {@link ByteOrder#BIG_ENDIAN}.
14941504
* <p>
1495-
* This option is important for all encoding and conversation methods.
1505+
* This option is important for all encoding and conversion methods.
14961506
*
14971507
* @param byteOrder new byteOrder
14981508
* @return a new instance with the same underlying array and new order, or "this" if order is the same

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,33 @@ static byte[] toPrimitiveArray(java.lang.Byte[] objectArray) {
567567
return primitivesArray;
568568
}
569569

570+
/**
571+
* Creates a byte array from given short array.
572+
* The resulting byte array will have length shortArray * 2.
573+
*
574+
* <p>
575+
* <strong>Analysis</strong>
576+
* <ul>
577+
* <li>Time Complexity: <code>O(n)</code></li>
578+
* <li>Space Complexity: <code>O(n)</code></li>
579+
* <li>Alters Parameters: <code>false</code></li>
580+
* </ul>
581+
* </p>
582+
*
583+
* @param shortArray to convert
584+
* @return resulting byte array
585+
*/
586+
static byte[] toByteArray(short[] shortArray) {
587+
byte[] primitivesArray = new byte[shortArray.length * 2];
588+
ByteBuffer buffer = ByteBuffer.allocate(2);
589+
for (int i = 0; i < shortArray.length; i++) {
590+
buffer.clear();
591+
byte[] shortBytes = buffer.putShort(shortArray[i]).array();
592+
System.arraycopy(shortBytes, 0, primitivesArray, (i * 2), shortBytes.length);
593+
}
594+
return primitivesArray;
595+
}
596+
570597
/**
571598
* Creates a byte array from given int array.
572599
* The resulting byte array will have length intArray * 4.
@@ -860,7 +887,6 @@ static double[] toDoubleArray(byte[] bytes, ByteOrder byteOrder) {
860887
return array;
861888
}
862889

863-
864890
/**
865891
* Converts the byte array to a short array. This will spread 2 bytes into a single short:
866892
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ public void fromShort() {
179179
assertEquals(test, Bytes.from(test).toShort());
180180
}
181181

182+
@Test(expected = NullPointerException.class)
183+
public void fromShortArray_empty_shouldThrow() {
184+
Bytes.from((short[]) null);
185+
}
186+
187+
@Test
188+
public void fromShortArray() {
189+
assertArrayEquals(new byte[]{0, 1, 0, 2}, Bytes.from((short) 1, (short) 2).array());
190+
assertArrayEquals(Bytes.from(Bytes.from((short) 32767), Bytes.from((short) 6761), Bytes.from((short) -8517)).array(), Bytes.from((short) 32767, (short) 6761, (short) -8517).array());
191+
assertArrayEquals(Bytes.from(Bytes.from((short) 1678), Bytes.from((short) -223), Bytes.from((short) 11114)).array(), Bytes.from((short) 1678, (short) -223, (short) 11114).array());
192+
assertArrayEquals(new byte[]{114, -123, 35, 53, 0, 0, 56, -70}, Bytes.from((short) 29317, (short) 9013, (short) 0, (short) 14522).array());
193+
}
194+
182195
@Test
183196
public void fromInt() {
184197
int test = 722837193;

0 commit comments

Comments
 (0)