@@ -38,7 +38,7 @@ public final class ByteUtils {
38
38
private ByteUtils () {}
39
39
40
40
/**
41
- * Concatenate the given {@code byte} arrays into one, with overlapping array elements included twice .
41
+ * Concatenate the given {@code byte} arrays into one.
42
42
* <p>
43
43
* The order of elements in the original arrays is preserved.
44
44
*
@@ -47,16 +47,12 @@ private ByteUtils() {}
47
47
* @return the new array.
48
48
*/
49
49
public static byte [] concat (byte [] array1 , byte [] array2 ) {
50
-
51
- byte [] result = Arrays .copyOf (array1 , array1 .length + array2 .length );
52
- System .arraycopy (array2 , 0 , result , array1 .length , array2 .length );
53
-
54
- return result ;
50
+ return concatAll (array1 , array2 );
55
51
}
56
52
57
53
/**
58
- * Concatenate the given {@code byte} arrays into one, with overlapping array elements included twice. Returns a new,
59
- * empty array if {@code arrays} was empty and returns the first array if {@code arrays} contains only a single array.
54
+ * Concatenate the given {@code byte} arrays into one. Returns a new, empty array if {@code arrays} was empty and
55
+ * returns the first array if {@code arrays} contains only a single array.
60
56
* <p>
61
57
* The order of elements in the original arrays is preserved.
62
58
*
@@ -66,24 +62,48 @@ public static byte[] concat(byte[] array1, byte[] array2) {
66
62
public static byte [] concatAll (byte []... arrays ) {
67
63
68
64
if (arrays .length == 0 ) {
69
- return new byte [] {} ;
65
+ return new byte [0 ] ;
70
66
}
67
+
71
68
if (arrays .length == 1 ) {
72
69
return arrays [0 ];
73
70
}
74
71
75
- // Sum the total result length
76
- int sum = 0 ;
77
- for (int i = 0 ; i < arrays .length ; ++i ) {
78
- sum += arrays [i ].length ;
72
+ long totalArraySize = 0 ;
73
+ for (byte [] array : arrays ) {
74
+ totalArraySize += array .length ;
75
+ }
76
+
77
+ if (totalArraySize == 0 ) {
78
+ return new byte [0 ];
79
79
}
80
80
81
- byte [] result = Arrays . copyOf ( arrays [ 0 ], sum ) ;
82
- int copied = arrays [ 0 ]. length ;
83
- for (int i = 1 ; i < arrays . length ; ++ i ) {
84
- System .arraycopy (arrays [ i ] , 0 , result , copied , arrays [ i ] .length );
85
- copied += arrays [ i ] .length ;
81
+ byte [] result = new byte [ Math . toIntExact ( totalArraySize )] ;
82
+ int copied = 0 ;
83
+ for (byte [] array : arrays ) {
84
+ System .arraycopy (array , 0 , result , copied , array .length );
85
+ copied += array .length ;
86
86
}
87
+
88
+ return result ;
89
+ }
90
+
91
+ /**
92
+ * Merge multiple {@code byte} arrays into one array
93
+ *
94
+ * @param firstArray must not be {@literal null}
95
+ * @param additionalArrays must not be {@literal null}
96
+ * @return
97
+ */
98
+ public static byte [][] mergeArrays (byte [] firstArray , byte []... additionalArrays ) {
99
+
100
+ Assert .notNull (firstArray , "first array must not be null" );
101
+ Assert .notNull (additionalArrays , "additional arrays must not be null" );
102
+
103
+ byte [][] result = new byte [additionalArrays .length + 1 ][];
104
+ result [0 ] = firstArray ;
105
+ System .arraycopy (additionalArrays , 0 , result , 1 , additionalArrays .length );
106
+
87
107
return result ;
88
108
}
89
109
@@ -118,25 +138,6 @@ public static byte[][] split(byte[] source, int c) {
118
138
return bytes .toArray (new byte [bytes .size ()][]);
119
139
}
120
140
121
- /**
122
- * Merge multiple {@code byte} arrays into one array
123
- *
124
- * @param firstArray must not be {@literal null}
125
- * @param additionalArrays must not be {@literal null}
126
- * @return
127
- */
128
- public static byte [][] mergeArrays (byte [] firstArray , byte []... additionalArrays ) {
129
-
130
- Assert .notNull (firstArray , "first array must not be null" );
131
- Assert .notNull (additionalArrays , "additional arrays must not be null" );
132
-
133
- byte [][] result = new byte [additionalArrays .length + 1 ][];
134
- result [0 ] = firstArray ;
135
- System .arraycopy (additionalArrays , 0 , result , 1 , additionalArrays .length );
136
-
137
- return result ;
138
- }
139
-
140
141
/**
141
142
* Extract a byte array from {@link ByteBuffer} without consuming it. The resulting {@code byte[]} is a copy of the
142
143
* buffer's contents and not updated upon changes within the buffer.
0 commit comments