Skip to content

Commit c956722

Browse files
committed
Add from() constructor for float[] and double[]
1 parent 9fa415b commit c956722

File tree

5 files changed

+150
-3
lines changed

5 files changed

+150
-3
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Releases
22

3+
## v1.4.0
4+
5+
* add `from()` constructor from `float[]`
6+
* add `from()` constructor from `double[]`
7+
38
## v1.3.0
49
* improve hex encoder performance by factor 5
510

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ Creating byte arrays from **primitive integer** types and arrays:
137137
Bytes.from(8); //00000000 00000000 00000000 00001000
138138
Bytes.from(1897621543227L);
139139
Bytes.from(1634, 88903, 77263);
140+
Bytes.from(0.7336f, -87263.0f);
141+
Bytes.from(0.8160183296, 3984639846.0);
140142
```
141143

142144
Initializing **empty arrays** of arbitrary length:

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static Bytes empty() {
108108
* @return new instance
109109
*/
110110
public static Bytes wrap(Bytes bytes) {
111-
return wrap(bytes.internalArray(), bytes.byteOrder);
111+
return wrap(Objects.requireNonNull(bytes, "passed Byte instance must not be null").internalArray(), bytes.byteOrder);
112112
}
113113

114114
/**
@@ -346,6 +346,16 @@ public static Bytes from(float float4byte) {
346346
return wrap(ByteBuffer.allocate(4).putFloat(float4byte).array());
347347
}
348348

349+
/**
350+
* Creates a new instance from given float array.
351+
*
352+
* @param floatArray to create from
353+
* @return new instance
354+
*/
355+
public static Bytes from(float... floatArray) {
356+
return wrap(Util.Converter.toByteArray(Objects.requireNonNull(floatArray, "must provide at least a single float")));
357+
}
358+
349359
/**
350360
* Creates a new instance from given 8 byte floating point number (double).
351361
*
@@ -356,6 +366,16 @@ public static Bytes from(double double8Byte) {
356366
return wrap(ByteBuffer.allocate(8).putDouble(double8Byte).array());
357367
}
358368

369+
/**
370+
* Creates a new instance from given double array.
371+
*
372+
* @param doubleArray to create from
373+
* @return new instance
374+
*/
375+
public static Bytes from(double... doubleArray) {
376+
return wrap(Util.Converter.toByteArray(Objects.requireNonNull(doubleArray, "must provide at least a single double")));
377+
}
378+
359379
/**
360380
* Creates a new instance from given {@link ByteBuffer}.
361381
* Will use the same backing byte array and honour the buffer's byte order.
@@ -2043,7 +2063,7 @@ public char[] toCharArray(Charset charset) {
20432063
* Pairs of {@code byte} elements are compared as if by invoking
20442064
* {@link Byte#compare(byte, byte)}.
20452065
* <p>
2046-
* Uses {@link ByteBuffer#compareTo(Object)} internally.
2066+
* Uses {@link ByteBuffer#compareTo(ByteBuffer)} internally.
20472067
*
20482068
* @return A negative integer, zero, or a positive integer as this buffer
20492069
* is less than, equal to, or greater than the given buffer

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,33 @@ static byte[] toByteArray(int[] intArray) {
560560
return primitivesArray;
561561
}
562562

563+
/**
564+
* Creates a byte array from given float array.
565+
* The resulting byte array will have length floatArray * 4.
566+
*
567+
* <p>
568+
* <strong>Analysis</strong>
569+
* <ul>
570+
* <li>Time Complexity: <code>O(n)</code></li>
571+
* <li>Space Complexity: <code>O(n)</code></li>
572+
* <li>Alters Parameters: <code>false</code></li>
573+
* </ul>
574+
* </p>
575+
*
576+
* @param floatArray to convert
577+
* @return resulting byte array
578+
*/
579+
static byte[] toByteArray(float[] floatArray) {
580+
byte[] primitivesArray = new byte[floatArray.length * 4];
581+
ByteBuffer buffer = ByteBuffer.allocate(4);
582+
for (int i = 0; i < floatArray.length; i++) {
583+
buffer.clear();
584+
byte[] floatBytes = buffer.putFloat(floatArray[i]).array();
585+
System.arraycopy(floatBytes, 0, primitivesArray, (i * 4), floatBytes.length);
586+
}
587+
return primitivesArray;
588+
}
589+
563590
/**
564591
* Creates a byte array from given long array.
565592
* The resulting byte array will have length longArray * 8
@@ -587,6 +614,33 @@ static byte[] toByteArray(long[] longArray) {
587614
return primitivesArray;
588615
}
589616

617+
/**
618+
* Creates a byte array from given double array.
619+
* The resulting byte array will have length doubleArray * 8.
620+
*
621+
* <p>
622+
* <strong>Analysis</strong>
623+
* <ul>
624+
* <li>Time Complexity: <code>O(n)</code></li>
625+
* <li>Space Complexity: <code>O(n)</code></li>
626+
* <li>Alters Parameters: <code>false</code></li>
627+
* </ul>
628+
* </p>
629+
*
630+
* @param doubleArray to convert
631+
* @return resulting byte array
632+
*/
633+
static byte[] toByteArray(double[] doubleArray) {
634+
byte[] primitivesArray = new byte[doubleArray.length * 8];
635+
ByteBuffer buffer = ByteBuffer.allocate(8);
636+
for (int i = 0; i < doubleArray.length; i++) {
637+
buffer.clear();
638+
byte[] doubleBytes = buffer.putDouble(doubleArray[i]).array();
639+
System.arraycopy(doubleBytes, 0, primitivesArray, (i * 8), doubleBytes.length);
640+
}
641+
return primitivesArray;
642+
}
643+
590644
/**
591645
* Converts a char array to a byte array with given charset and range
592646
*

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,19 @@ public void wrapTest() {
6767
assertEquals(0, bNullSafe1.length());
6868
}
6969

70+
@Test(expected = NullPointerException.class)
71+
public void wrapByteNull_shouldThrow() {
72+
Bytes.wrap((Bytes) null);
73+
}
74+
7075
@Test(expected = NullPointerException.class)
7176
public void wrapTestNullExpected() {
7277
Bytes.wrap((byte[]) null);
7378
}
7479

7580
@Test
7681
public void wrapTestNullSafe() {
77-
Bytes.wrapNullSafe(null);
82+
assertSame(Bytes.empty(), Bytes.wrapNullSafe(null));
7883
}
7984

8085
@Test
@@ -183,6 +188,11 @@ public void fromInt() {
183188
assertEquals(test, Bytes.from(test).toInt());
184189
}
185190

191+
@Test(expected = NullPointerException.class)
192+
public void fromIntArray_empty_shouldThrow() {
193+
Bytes.from((int[]) null);
194+
}
195+
186196
@Test
187197
public void fromIntArray() {
188198
assertArrayEquals(new byte[]{0, 0, 0, 1, 0, 0, 0, 2}, Bytes.from(1, 2).array());
@@ -207,6 +217,11 @@ public void fromLong() {
207217
assertEquals(test, Bytes.from(test).toLong());
208218
}
209219

220+
@Test(expected = NullPointerException.class)
221+
public void fromLongArray_empty_shouldThrow() {
222+
Bytes.from((long[]) null);
223+
}
224+
210225
@Test
211226
public void fromLongArray() {
212227
assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2}, Bytes.from(new long[]{1, 2}).array());
@@ -224,6 +239,22 @@ public void fromFloat() {
224239
assertEquals(test, Bytes.from(test).toFloat(), 0.01);
225240
}
226241

242+
@Test(expected = NullPointerException.class)
243+
public void fromFloatArray_empty_shouldThrow() {
244+
Bytes.from((float[]) null);
245+
}
246+
247+
@Test
248+
public void fromFloatArray() {
249+
assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 0}, Bytes.from(0f, 0f).array());
250+
assertArrayEquals(new byte[]{0, 0, 0, 0, 63, -128, 0, 0}, Bytes.from(0f, 1f).array());
251+
assertArrayEquals(new byte[]{0, 0, 0, 0, -65, -128, 0, 0}, Bytes.from(0f, -1f).array());
252+
assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Bytes.from(0f, 0f, 0f).array());
253+
assertArrayEquals(new byte[]{66, -105, 0, 0, 71, 119, 46, 31}, Bytes.from(75.5f, 63278.123f).array());
254+
assertArrayEquals(Bytes.from(Bytes.from(78239.934978f), Bytes.from(-82736.65178f), Bytes.from(0.12879316287461f)).array(),
255+
Bytes.from(78239.934978f, -82736.65178f, 0.12879316287461f).array());
256+
}
257+
227258
@Test
228259
public void fromDouble() {
229260
double test = 3423423.8923423974123;
@@ -233,6 +264,21 @@ public void fromDouble() {
233264
assertEquals(test, Bytes.from(test).toDouble(), 0.01);
234265
}
235266

267+
@Test(expected = NullPointerException.class)
268+
public void fromDoubleArray_empty_shouldThrow() {
269+
Bytes.from((double[]) null);
270+
}
271+
272+
@Test
273+
public void fromDoubleArray() {
274+
assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Bytes.from(0.0, 0.0).array());
275+
assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 63, -16, 0, 0, 0, 0, 0, 0}, Bytes.from(0.0, 1.0).array());
276+
assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, -65, -16, 0, 0, 0, 0, 0, 0}, Bytes.from(0.0, -1.0).array());
277+
assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Bytes.from(0, 0, 0).array());
278+
assertArrayEquals(Bytes.from(Bytes.from(78239.934978), Bytes.from(-82736.65178), Bytes.from(0.12879316287461)).array(),
279+
Bytes.from(78239.934978, -82736.65178, 0.12879316287461).array());
280+
}
281+
236282
@Test
237283
public void fromByteBuffer() {
238284
checkByteBuffer(example_bytes_empty);
@@ -325,6 +371,11 @@ public void toCharArrayShouldThroughNullPointer() {
325371
Bytes.allocate(4).toCharArray(null);
326372
}
327373

374+
@Test(expected = NullPointerException.class)
375+
public void fromMultipleBytes_empty_shouldThrow() {
376+
Bytes.from((Bytes[]) null);
377+
}
378+
328379
@Test
329380
public void fromMultipleBytes() {
330381
assertArrayEquals(new byte[]{0x01, 0x02, 0x03}, Bytes.from(Bytes.from((byte) 0x01), Bytes.from((byte) 0x02), Bytes.from((byte) 0x03)).array());
@@ -446,6 +497,21 @@ public void fromVariousBytes() {
446497
assertArrayEquals(example_bytes_sixteen, Bytes.fromNullSafe(example_bytes_sixteen).array());
447498
}
448499

500+
@Test(expected = NullPointerException.class)
501+
public void fromArray_empty_shouldThrow() {
502+
Bytes.from((byte[]) null);
503+
}
504+
505+
@Test(expected = NullPointerException.class)
506+
public void wrapArrayByteOrder_empty_shouldThrow() {
507+
Bytes.wrap(null, ByteOrder.BIG_ENDIAN);
508+
}
509+
510+
@Test(expected = NullPointerException.class)
511+
public void fromPartByte_empty_shouldThrow() {
512+
Bytes.from((byte[]) null, 0, 1);
513+
}
514+
449515
@Test
450516
public void fromPartByte() {
451517
assertArrayEquals(new byte[]{example_bytes_four[1]}, Bytes.from(example_bytes_four, 1, 1).array());

0 commit comments

Comments
 (0)