Skip to content

Commit d85b422

Browse files
committed
Improve toArray() util to not use intermittent array (ie. less memory complexity)
1 parent efa66c4 commit d85b422

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,12 @@ private Converter() {
322322
* is null
323323
*/
324324
static byte[] toArray(Collection<java.lang.Byte> collection) {
325-
Object[] boxedArray = collection.toArray();
326-
int len = boxedArray.length;
327-
byte[] array = new byte[len];
328-
for (int i = 0; i < len; i++) {
329-
array[i] = (java.lang.Byte) boxedArray[i];
325+
final int len = collection.size();
326+
final byte[] array = new byte[len];
327+
int i = 0;
328+
for (java.lang.Byte b : collection) {
329+
array[i] = b;
330+
i++;
330331
}
331332
return array;
332333
}

0 commit comments

Comments
 (0)