From c0f45adc2ca696c0317edab2cf3eed89adaf17bc Mon Sep 17 00:00:00 2001 From: Herman Lyakhovich <53296879+hlyakhovich@users.noreply.github.com> Date: Wed, 17 Mar 2021 19:29:22 +0100 Subject: [PATCH] Overload from() with short array --- src/main/java/at/favre/lib/bytes/Bytes.java | 12 +++++++- src/main/java/at/favre/lib/bytes/Util.java | 28 ++++++++++++++++++- .../lib/bytes/BytesConstructorTests.java | 13 +++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) 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 + *
O(n)O(n)false