Skip to content

Commit 173363a

Browse files
committed
Add float and double constructor
1 parent 5eb7dca commit 173363a

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## v0.4.6
44

55
* add appending with strings
6-
* add boolean constructor
6+
* add boolean, float and double constructor
77

88
## v0.4.5
99

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,26 @@ public static Bytes from(long... longArray) {
313313
return wrap(Util.toByteArray(longArray));
314314
}
315315

316+
/**
317+
* Creates a new instance from given 4 byte floating point number (float).
318+
*
319+
* @param float4byte to create from
320+
* @return new instance
321+
*/
322+
public static Bytes from(float float4byte) {
323+
return wrap(ByteBuffer.allocate(4).putFloat(float4byte).array());
324+
}
325+
326+
/**
327+
* Creates a new instance from given 8 byte floating point number (double).
328+
*
329+
* @param double8Byte to create from
330+
* @return new instance
331+
*/
332+
public static Bytes from(double double8Byte) {
333+
return wrap(ByteBuffer.allocate(8).putDouble(double8Byte).array());
334+
}
335+
316336
/**
317337
* Creates a new instance from given ByteBuffer.
318338
* Will use the same backing byte array and honour the buffer's byte order.
@@ -1488,7 +1508,7 @@ public float toFloat() {
14881508

14891509
/**
14901510
* If the underlying byte array is exactly 8 byte / 64 bit long, return the
1491-
* representation for a Java float value. The output is dependent on the set {@link #byteOrder()}.
1511+
* representation for a Java double value. The output is dependent on the set {@link #byteOrder()}.
14921512
*
14931513
* @return the double representation
14941514
* @throws IllegalStateException if byte array has length not equal to 8

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

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class BytesConstructorTests extends ABytesTest {
4646
public TemporaryFolder testFolder = new TemporaryFolder();
4747

4848
@Test
49-
public void wrapTest() throws Exception {
49+
public void wrapTest() {
5050
Bytes b = Bytes.wrap(example_bytes_seven);
5151
assertSame(example_bytes_seven, b.array());
5252
byte[] copy = Arrays.copyOf(example_bytes_seven, example_bytes_seven.length);
@@ -66,17 +66,17 @@ public void wrapTest() throws Exception {
6666
}
6767

6868
@Test(expected = NullPointerException.class)
69-
public void wrapTestNullExpected() throws Exception {
69+
public void wrapTestNullExpected() {
7070
Bytes.wrap((byte[]) null);
7171
}
7272

7373
@Test
74-
public void wrapTestNullSafe() throws Exception {
74+
public void wrapTestNullSafe() {
7575
Bytes.wrapNullSafe(null);
7676
}
7777

7878
@Test
79-
public void allocate() throws Exception {
79+
public void allocate() {
8080
assertEquals(0, Bytes.allocate(0).length());
8181
assertEquals(2, Bytes.allocate(2).length());
8282
assertEquals(16, Bytes.allocate(16).length());
@@ -98,7 +98,7 @@ private void checkAllocate(int length, byte content) {
9898
}
9999

100100
@Test
101-
public void fromBitSet() throws Exception {
101+
public void fromBitSet() {
102102
checkBitSet(example_bytes_empty);
103103
checkBitSet(example_bytes_one);
104104
checkBitSet(example_bytes_two);
@@ -114,7 +114,7 @@ private void checkBitSet(byte[] array) {
114114
}
115115

116116
@Test
117-
public void fromBigInteger() throws Exception {
117+
public void fromBigInteger() {
118118
checkBigInteger(example_bytes_one);
119119
checkBigInteger(example_bytes_two);
120120
checkBigInteger(example_bytes_seven);
@@ -129,21 +129,21 @@ private void checkBigInteger(byte[] array) {
129129
}
130130

131131
@Test
132-
public void fromBoolean() throws Exception {
132+
public void fromBoolean() {
133133
assertArrayEquals(new byte[]{(byte) 1}, Bytes.from(true).array());
134134
assertArrayEquals(new byte[]{(byte) 0}, Bytes.from(false).array());
135135
}
136136

137137
@Test
138-
public void fromByte() throws Exception {
138+
public void fromByte() {
139139
byte test = 0x4E;
140140
assertArrayEquals(new byte[]{test}, Bytes.from(test).array());
141141
assertArrayEquals(new byte[1], Bytes.from((byte) 0).array());
142142
assertEquals(test, Bytes.from(test).toByte());
143143
}
144144

145145
@Test
146-
public void fromChar() throws Exception {
146+
public void fromChar() {
147147
char test = 5821;
148148
byte[] primitiveArray = ByteBuffer.allocate(2).putChar(test).array();
149149
assertArrayEquals(primitiveArray, Bytes.from(test).array());
@@ -152,7 +152,7 @@ public void fromChar() throws Exception {
152152
}
153153

154154
@Test
155-
public void fromShort() throws Exception {
155+
public void fromShort() {
156156
short test = 12721;
157157
byte[] primitiveArray = ByteBuffer.allocate(2).putShort(test).array();
158158
assertArrayEquals(primitiveArray, Bytes.from(test).array());
@@ -161,7 +161,7 @@ public void fromShort() throws Exception {
161161
}
162162

163163
@Test
164-
public void fromInt() throws Exception {
164+
public void fromInt() {
165165
int test = 722837193;
166166
byte[] primitiveArray = ByteBuffer.allocate(4).putInt(test).array();
167167
assertArrayEquals(primitiveArray, Bytes.from(test).array());
@@ -170,15 +170,15 @@ public void fromInt() throws Exception {
170170
}
171171

172172
@Test
173-
public void fromIntArray() throws Exception {
173+
public void fromIntArray() {
174174
assertArrayEquals(new byte[]{0, 0, 0, 1, 0, 0, 0, 2}, Bytes.from(1, 2).array());
175175
assertArrayEquals(Bytes.from(Bytes.from(871193), Bytes.from(6761), Bytes.from(-917656)).array(), Bytes.from(871193, 6761, -917656).array());
176176
assertArrayEquals(Bytes.from(Bytes.from(1678), Bytes.from(-223), Bytes.from(11114)).array(), Bytes.from(1678, -223, 11114).array());
177177
assertArrayEquals(new byte[]{0, 11, 30, 55, 0, 0, 35, 53, 0, 0, 0, 0, 0, 0, 56, -70}, Bytes.from(728631, 9013, 0, 14522).array());
178178
}
179179

180180
@Test
181-
public void fromLong() throws Exception {
181+
public void fromLong() {
182182
long test = 172283719283L;
183183
byte[] primitiveArray = ByteBuffer.allocate(8).putLong(test).array();
184184
assertArrayEquals(primitiveArray, Bytes.from(test).array());
@@ -187,15 +187,33 @@ public void fromLong() throws Exception {
187187
}
188188

189189
@Test
190-
public void fromLongArray() throws Exception {
190+
public void fromLongArray() {
191191
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());
192192
assertArrayEquals(Bytes.from(Bytes.from(871193L), Bytes.from(6761L), Bytes.from(-917656L)).array(), Bytes.from(new long[]{871193, 6761, -917656}).array());
193193
assertArrayEquals(Bytes.from(Bytes.from(1678L), Bytes.from(-223L), Bytes.from(11114L)).array(), Bytes.from(1678L, -223L, 11114L).array());
194194
assertArrayEquals(Bytes.from(Bytes.from(1273612831678L), Bytes.from(-72639123786223L)).array(), Bytes.from(1273612831678L, -72639123786223L).array());
195195
}
196196

197197
@Test
198-
public void fromByteBuffer() throws Exception {
198+
public void fromFloat() {
199+
float test = 63278.123f;
200+
byte[] primitiveArray = ByteBuffer.allocate(4).putFloat(test).array();
201+
assertArrayEquals(primitiveArray, Bytes.from(test).array());
202+
assertArrayEquals(new byte[4], Bytes.from(0f).array());
203+
assertEquals(test, Bytes.from(test).toFloat(), 0.01);
204+
}
205+
206+
@Test
207+
public void fromDouble() {
208+
double test = 3423423.8923423974123;
209+
byte[] primitiveArray = ByteBuffer.allocate(8).putDouble(test).array();
210+
assertArrayEquals(primitiveArray, Bytes.from(test).array());
211+
assertArrayEquals(new byte[8], Bytes.from(0.0).array());
212+
assertEquals(test, Bytes.from(test).toDouble(), 0.01);
213+
}
214+
215+
@Test
216+
public void fromByteBuffer() {
199217
checkByteBuffer(example_bytes_empty);
200218
checkByteBuffer(example_bytes_one);
201219
checkByteBuffer(example_bytes_two);
@@ -210,7 +228,7 @@ private void checkByteBuffer(byte[] array) {
210228
}
211229

212230
@Test
213-
public void fromString() throws Exception {
231+
public void fromString() {
214232
checkString("", StandardCharsets.UTF_8);
215233
checkString(" ", StandardCharsets.UTF_8);
216234
checkString("\t", StandardCharsets.UTF_8);
@@ -224,7 +242,7 @@ public void fromString() throws Exception {
224242
}
225243

226244
@Test
227-
public void fromMultipleBytes() throws Exception {
245+
public void fromMultipleBytes() {
228246
assertArrayEquals(new byte[]{0x01, 0x02, 0x03}, Bytes.from(Bytes.from((byte) 0x01), Bytes.from((byte) 0x02), Bytes.from((byte) 0x03)).array());
229247
}
230248

@@ -244,7 +262,7 @@ private void checkString(String string, Charset charset) {
244262
}
245263

246264
@Test
247-
public void fromInputStream() throws Exception {
265+
public void fromInputStream() {
248266
checkInputStream(example_bytes_one);
249267
checkInputStream(example_bytes_two);
250268
checkInputStream(example_bytes_four);
@@ -259,7 +277,7 @@ private void checkInputStream(byte[] array) {
259277
}
260278

261279
@Test
262-
public void fromDataInput() throws Exception {
280+
public void fromDataInput() {
263281
checkDataInput(example_bytes_one);
264282
checkDataInput(example_bytes_two);
265283
checkDataInput(example_bytes_four);
@@ -274,12 +292,12 @@ private void checkDataInput(byte[] array) {
274292
}
275293

276294
@Test(expected = IllegalStateException.class)
277-
public void fromDataInputShouldThrowException() throws Exception {
295+
public void fromDataInputShouldThrowException() {
278296
Bytes.from(new DataInputStream(new ByteArrayInputStream(example_bytes_one)), 2);
279297
}
280298

281299
@Test
282-
public void fromList() throws Exception {
300+
public void fromList() {
283301
checkList(example_bytes_one);
284302
checkList(example_bytes_two);
285303
checkList(example_bytes_four);
@@ -296,7 +314,7 @@ private void checkList(byte[] array) {
296314
}
297315

298316
@Test
299-
public void fromVariousBytes() throws Exception {
317+
public void fromVariousBytes() {
300318
assertArrayEquals(example_bytes_one, Bytes.from(example_bytes_one).array());
301319
assertArrayEquals(example_bytes_two, Bytes.from(example_bytes_two).array());
302320
assertArrayEquals(example_bytes_seven, Bytes.from(example_bytes_seven).array());
@@ -318,7 +336,7 @@ public void fromVariousBytes() throws Exception {
318336
}
319337

320338
@Test
321-
public void fromPartByte() throws Exception {
339+
public void fromPartByte() {
322340
assertArrayEquals(new byte[]{example_bytes_four[1]}, Bytes.from(example_bytes_four, 1, 1).array());
323341
assertArrayEquals(new byte[]{example_bytes_eight[4], example_bytes_eight[5], example_bytes_eight[6]}, Bytes.from(example_bytes_eight, 4, 3).array());
324342
}
@@ -336,7 +354,7 @@ public void fromFile() throws Exception {
336354
}
337355

338356
@Test(expected = IllegalArgumentException.class)
339-
public void fromFileNotExisting() throws Exception {
357+
public void fromFileNotExisting() {
340358
Bytes.from(new File("doesnotexist"));
341359
}
342360

@@ -358,7 +376,7 @@ public void fromFileCannotRead() throws Exception {
358376
}
359377

360378
@Test
361-
public void fromObjectArray() throws Exception {
379+
public void fromObjectArray() {
362380
Byte[] objectArray = new Byte[]{0x01, 0x02, 0x03, 0x04};
363381
Bytes b = Bytes.from(objectArray);
364382
assertArrayEquals(new byte[]{0x01, 0x02, 0x03, 0x04}, b.array());

0 commit comments

Comments
 (0)