Skip to content

Commit 9639fc3

Browse files
committed
Add constructor for IntBuffer and CharBuffer
1 parent c7068b2 commit 9639fc3

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* add radix encoding/parsing and fix radix tests #6, #20
66
* add support for Base32 RFC4648 non-hex alphabet encoding/parsing #21
7+
* add constructor for `IntBuffer` and `CharBuffer`
78

89
### Deprecations (will be removed in v1.0+)
910

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.nio.ByteBuffer;
3131
import java.nio.ByteOrder;
3232
import java.nio.CharBuffer;
33+
import java.nio.IntBuffer;
3334
import java.nio.ReadOnlyBufferException;
3435
import java.nio.charset.Charset;
3536
import java.nio.charset.StandardCharsets;
@@ -371,14 +372,36 @@ public static Bytes from(double double8Byte) {
371372
}
372373

373374
/**
374-
* Creates a new instance from given ByteBuffer.
375+
* Creates a new instance from given {@link ByteBuffer}.
375376
* Will use the same backing byte array and honour the buffer's byte order.
376377
*
377-
* @param buffer to get the byte array from
378+
* @param buffer to get the byte array from (must not be null)
378379
* @return new instance
379380
*/
380381
public static Bytes from(ByteBuffer buffer) {
381-
return wrap(buffer.array(), buffer.order());
382+
return wrap(Objects.requireNonNull(buffer, "provided byte buffer must not be null").array(), buffer.order());
383+
}
384+
385+
/**
386+
* Creates a new instance from given {@link CharBuffer}.
387+
* Will ignore buffer's byte order and use {@link ByteOrder#BIG_ENDIAN}
388+
*
389+
* @param buffer to get the char array from (must not be null)
390+
* @return new instance
391+
*/
392+
public static Bytes from(CharBuffer buffer) {
393+
return from(Objects.requireNonNull(buffer, "provided char buffer must not be null").array());
394+
}
395+
396+
/**
397+
* Creates a new instance from given {@link IntBuffer}.
398+
* Will ignore buffer's byte order and use {@link ByteOrder#BIG_ENDIAN}
399+
*
400+
* @param buffer to get the int array from (must not be null)
401+
* @return new instance
402+
*/
403+
public static Bytes from(IntBuffer buffer) {
404+
return from(Objects.requireNonNull(buffer, "provided int buffer must not be null").array());
382405
}
383406

384407
/**

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@
2626
import org.junit.Test;
2727
import org.junit.rules.TemporaryFolder;
2828

29-
import java.io.*;
29+
import java.io.ByteArrayInputStream;
30+
import java.io.DataInput;
31+
import java.io.DataInputStream;
32+
import java.io.File;
33+
import java.io.FileOutputStream;
3034
import java.math.BigInteger;
3135
import java.nio.ByteBuffer;
3236
import java.nio.ByteOrder;
37+
import java.nio.CharBuffer;
38+
import java.nio.IntBuffer;
3339
import java.nio.charset.Charset;
3440
import java.nio.charset.StandardCharsets;
3541
import java.text.Normalizer;
@@ -188,6 +194,13 @@ public void fromIntArray() {
188194
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());
189195
}
190196

197+
@Test
198+
public void fromIntBuffer() {
199+
assertArrayEquals(new byte[]{0, 0, 0, 1, 0, 0, 0, 2}, Bytes.from(IntBuffer.wrap(new int[]{1, 2})).array());
200+
assertArrayEquals(Bytes.from(Bytes.from(871193), Bytes.from(6761), Bytes.from(-917656)).array(), Bytes.from(IntBuffer.wrap(new int[]{871193, 6761, -917656})).array());
201+
assertArrayEquals(Bytes.empty().array(), Bytes.from(IntBuffer.allocate(0)).array());
202+
}
203+
191204
@Test
192205
public void fromLong() {
193206
long test = 172283719283L;
@@ -281,6 +294,7 @@ public void fromCharArray() {
281294
assertArrayEquals(String.valueOf(s1.toCharArray()).getBytes(StandardCharsets.ISO_8859_1), Bytes.from(s1.toCharArray(), StandardCharsets.ISO_8859_1).array());
282295
assertArrayEquals(String.valueOf(s1.toCharArray()).getBytes(StandardCharsets.UTF_16), Bytes.from(s1.toCharArray(), StandardCharsets.UTF_16).array());
283296
assertArrayEquals(String.valueOf(s1.toCharArray()).getBytes(StandardCharsets.UTF_8), Bytes.from(s1.toCharArray(), StandardCharsets.UTF_8).array());
297+
assertArrayEquals(Bytes.empty().array(), Bytes.from(CharBuffer.allocate(0)).array());
284298
}
285299

286300
@Test
@@ -306,8 +320,10 @@ private void checkString(String string, Charset charset) {
306320
private void checkCharArray(String s) {
307321
Bytes b1 = Bytes.from(s.toCharArray());
308322
Bytes b2 = Bytes.from(s.toCharArray(), StandardCharsets.UTF_8);
323+
Bytes b3 = Bytes.from(CharBuffer.wrap(s.toCharArray()));
309324
assertArrayEquals(String.valueOf(s.toCharArray()).getBytes(StandardCharsets.UTF_8), b1.array());
310325
assertArrayEquals(String.valueOf(s.toCharArray()).getBytes(StandardCharsets.UTF_8), b2.array());
326+
assertArrayEquals(String.valueOf(s.toCharArray()).getBytes(StandardCharsets.UTF_8), b3.array());
311327
}
312328

313329
@Test

0 commit comments

Comments
 (0)