Skip to content

Commit bb54fc9

Browse files
committed
Add more create methods
1 parent 5a4a8ab commit bb54fc9

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ public static Bytes from(byte[]... moreArrays) {
153153
return wrap(Util.concat(moreArrays));
154154
}
155155

156+
/**
157+
* Creates a new instance from given array of byte arrays
158+
*
159+
* @param moreBytes must not be null
160+
* @return new instance
161+
*/
162+
public static Bytes from(Bytes... moreBytes) {
163+
Objects.requireNonNull(moreBytes, "bytes most not be null");
164+
byte[][] bytes = new byte[moreBytes.length][];
165+
for (int i = 0; i < moreBytes.length; i++) {
166+
bytes[i] = moreBytes[i].array();
167+
}
168+
return from(bytes);
169+
}
170+
156171
/**
157172
* Creates a new instance from given collections. This will create a lot of auto-unboxing events,
158173
* so use with care with bigger lists.
@@ -275,7 +290,7 @@ public static Bytes from(InputStream stream) {
275290
* @param utf8String to get the internal byte array from
276291
* @return new instance
277292
*/
278-
public static Bytes from(String utf8String) {
293+
public static Bytes from(CharSequence utf8String) {
279294
return from(utf8String, StandardCharsets.UTF_8);
280295
}
281296

@@ -286,7 +301,7 @@ public static Bytes from(String utf8String) {
286301
* @param form to normalize, usually you want {@link java.text.Normalizer.Form#NFKD} for compatibility
287302
* @return new instance
288303
*/
289-
public static Bytes from(String utf8String, Normalizer.Form form) {
304+
public static Bytes from(CharSequence utf8String, Normalizer.Form form) {
290305
return from(Normalizer.normalize(utf8String, form), StandardCharsets.UTF_8);
291306
}
292307

@@ -297,8 +312,8 @@ public static Bytes from(String utf8String, Normalizer.Form form) {
297312
* @param charset used to decode the string
298313
* @return new instance
299314
*/
300-
public static Bytes from(String string, Charset charset) {
301-
return wrap(string.getBytes(charset));
315+
public static Bytes from(CharSequence string, Charset charset) {
316+
return wrap(string.toString().getBytes(charset));
302317
}
303318

304319
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.ByteBuffer;
2828
import java.nio.charset.Charset;
2929
import java.nio.charset.StandardCharsets;
30+
import java.text.Normalizer;
3031
import java.util.Arrays;
3132
import java.util.BitSet;
3233
import java.util.LinkedList;
@@ -148,14 +149,22 @@ private void checkByteBuffer(byte[] array) {
148149
@Test
149150
public void fromString() throws Exception {
150151
checkString("", StandardCharsets.UTF_8);
152+
checkString(" ", StandardCharsets.UTF_8);
153+
checkString("\t", StandardCharsets.UTF_8);
151154
checkString("a", StandardCharsets.UTF_8);
155+
checkString("12345678abcdefjkl", StandardCharsets.UTF_8);
152156
checkString("asdghasdu72Ahdans", StandardCharsets.UTF_8);
153157
checkString("asdghasdu72Ahdans", StandardCharsets.ISO_8859_1);
154158
checkString("7asdh#ö01^^`´dµ@€", StandardCharsets.UTF_8);
155159
checkString("7asdh#ö01^^`´dµ@€", StandardCharsets.US_ASCII);
156160
checkString("7asdh#ö01^^`´dµ@€", StandardCharsets.ISO_8859_1);
157161
}
158162

163+
@Test
164+
public void fromMultipleBytes() throws Exception {
165+
assertArrayEquals(new byte[]{0x01, 0x02, 0x03}, Bytes.from(Bytes.from((byte) 0x01), Bytes.from((byte) 0x02), Bytes.from((byte) 0x03)).array());
166+
}
167+
159168
private void checkString(String string, Charset charset) {
160169
Bytes b = Bytes.from(string, charset);
161170
assertArrayEquals(string.getBytes(charset), b.array());
@@ -165,6 +174,9 @@ private void checkString(String string, Charset charset) {
165174
Bytes bUtf8 = Bytes.from(string);
166175
assertArrayEquals(string.getBytes(StandardCharsets.UTF_8), bUtf8.array());
167176
assertEquals(new String(string.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8), bUtf8.encodeUtf8());
177+
} else {
178+
Bytes bNormalized = Bytes.from(string, Normalizer.Form.NFKD);
179+
assertArrayEquals(Normalizer.normalize(string, Normalizer.Form.NFKD).getBytes(charset), bNormalized.array());
168180
}
169181
}
170182

0 commit comments

Comments
 (0)