diff --git a/src/main/java/at/favre/lib/bytes/Bytes.java b/src/main/java/at/favre/lib/bytes/Bytes.java
index 42541e6..3797292 100644
--- a/src/main/java/at/favre/lib/bytes/Bytes.java
+++ b/src/main/java/at/favre/lib/bytes/Bytes.java
@@ -296,6 +296,16 @@ public static Bytes from(short short2Byte) {
return wrap(ByteBuffer.allocate(2).putShort(short2Byte).array());
}
+ /**
+ * Creates a new instance from given 2 byte short array.
+ *
+ * @param shortArray to create from
+ * @return new instance
+ */
+ public static Bytes from(short... shortArray) {
+ return wrap(Util.Converter.toByteArray(Objects.requireNonNull(shortArray, "must provide at least a single short")));
+ }
+
/**
* Creates a new instance from given 4 byte integer.
*
@@ -1492,7 +1502,7 @@ public Bytes duplicate() {
/**
* Set the byte order or endianness of this instance. Default in Java is {@link ByteOrder#BIG_ENDIAN}.
*
- * This option is important for all encoding and conversation methods.
+ * This option is important for all encoding and conversion methods.
*
* @param byteOrder new byteOrder
* @return a new instance with the same underlying array and new order, or "this" if order is the same
diff --git a/src/main/java/at/favre/lib/bytes/Util.java b/src/main/java/at/favre/lib/bytes/Util.java
index da50ff0..710c4d3 100644
--- a/src/main/java/at/favre/lib/bytes/Util.java
+++ b/src/main/java/at/favre/lib/bytes/Util.java
@@ -567,6 +567,33 @@ static byte[] toPrimitiveArray(java.lang.Byte[] objectArray) {
return primitivesArray;
}
+ /**
+ * Creates a byte array from given short array.
+ * The resulting byte array will have length shortArray * 2.
+ *
+ *
+ * Analysis
+ *
+ * - Time Complexity:
O(n)
+ * - Space Complexity:
O(n)
+ * - Alters Parameters:
false
+ *
+ *
+ *
+ * @param shortArray to convert
+ * @return resulting byte array
+ */
+ static byte[] toByteArray(short[] shortArray) {
+ byte[] primitivesArray = new byte[shortArray.length * 2];
+ ByteBuffer buffer = ByteBuffer.allocate(2);
+ for (int i = 0; i < shortArray.length; i++) {
+ buffer.clear();
+ byte[] shortBytes = buffer.putShort(shortArray[i]).array();
+ System.arraycopy(shortBytes, 0, primitivesArray, (i * 2), shortBytes.length);
+ }
+ return primitivesArray;
+ }
+
/**
* Creates a byte array from given int array.
* The resulting byte array will have length intArray * 4.
@@ -860,7 +887,6 @@ static double[] toDoubleArray(byte[] bytes, ByteOrder byteOrder) {
return array;
}
-
/**
* Converts the byte array to a short array. This will spread 2 bytes into a single short:
*
diff --git a/src/test/java/at/favre/lib/bytes/BytesConstructorTests.java b/src/test/java/at/favre/lib/bytes/BytesConstructorTests.java
index 612dba3..5cf5058 100644
--- a/src/test/java/at/favre/lib/bytes/BytesConstructorTests.java
+++ b/src/test/java/at/favre/lib/bytes/BytesConstructorTests.java
@@ -179,6 +179,19 @@ public void fromShort() {
assertEquals(test, Bytes.from(test).toShort());
}
+ @Test(expected = NullPointerException.class)
+ public void fromShortArray_empty_shouldThrow() {
+ Bytes.from((short[]) null);
+ }
+
+ @Test
+ public void fromShortArray() {
+ assertArrayEquals(new byte[]{0, 1, 0, 2}, Bytes.from((short) 1, (short) 2).array());
+ 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());
+ 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());
+ assertArrayEquals(new byte[]{114, -123, 35, 53, 0, 0, 56, -70}, Bytes.from((short) 29317, (short) 9013, (short) 0, (short) 14522).array());
+ }
+
@Test
public void fromInt() {
int test = 722837193;