Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/at/favre/lib/bytes/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -1492,7 +1502,7 @@ public Bytes duplicate() {
/**
* Set the byte order or endianness of this instance. Default in Java is {@link ByteOrder#BIG_ENDIAN}.
* <p>
* 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
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/at/favre/lib/bytes/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>
* <strong>Analysis</strong>
* <ul>
* <li>Time Complexity: <code>O(n)</code></li>
* <li>Space Complexity: <code>O(n)</code></li>
* <li>Alters Parameters: <code>false</code></li>
* </ul>
* </p>
*
* @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.
Expand Down Expand Up @@ -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:
*
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/at/favre/lib/bytes/BytesConstructorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down