Skip to content

Commit ec1bcc5

Browse files
committed
Polishing.
Simplify code using arraycopy only for array copies. Call concatAll(…) from concat(…). Use i++ intead of ++i notation. See #2366
1 parent e13b61f commit ec1bcc5

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

src/main/java/org/springframework/data/redis/util/ByteUtils.java

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public final class ByteUtils {
3838
private ByteUtils() {}
3939

4040
/**
41-
* Concatenate the given {@code byte} arrays into one, with overlapping array elements included twice.
41+
* Concatenate the given {@code byte} arrays into one.
4242
* <p>
4343
* The order of elements in the original arrays is preserved.
4444
*
@@ -47,16 +47,12 @@ private ByteUtils() {}
4747
* @return the new array.
4848
*/
4949
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);
5551
}
5652

5753
/**
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.
6056
* <p>
6157
* The order of elements in the original arrays is preserved.
6258
*
@@ -66,24 +62,48 @@ public static byte[] concat(byte[] array1, byte[] array2) {
6662
public static byte[] concatAll(byte[]... arrays) {
6763

6864
if (arrays.length == 0) {
69-
return new byte[] {};
65+
return new byte[0];
7066
}
67+
7168
if (arrays.length == 1) {
7269
return arrays[0];
7370
}
7471

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];
7979
}
8080

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;
8686
}
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+
87107
return result;
88108
}
89109

@@ -118,25 +138,6 @@ public static byte[][] split(byte[] source, int c) {
118138
return bytes.toArray(new byte[bytes.size()][]);
119139
}
120140

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-
140141
/**
141142
* Extract a byte array from {@link ByteBuffer} without consuming it. The resulting {@code byte[]} is a copy of the
142143
* buffer's contents and not updated upon changes within the buffer.

0 commit comments

Comments
 (0)