Skip to content

Commit 6f05326

Browse files
committed
Use reverse guava implementation
1 parent 4c35f89 commit 6f05326

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,7 @@ final class ReverseTransformer implements BytesTransformer {
192192
@Override
193193
public byte[] transform(byte[] currentArray, boolean inPlace) {
194194
byte[] out = inPlace ? currentArray : Bytes.from(currentArray).array();
195-
196-
for (int i = 0; i < out.length / 2; i++) {
197-
byte temp = out[i];
198-
out[i] = out[out.length - i - 1];
199-
out[out.length - i - 1] = temp;
200-
}
195+
Util.reverse(out, 0, out.length);
201196
return out;
202197
}
203198

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ static void shuffle(byte[] array, Random random) {
244244
}
245245
}
246246

247+
/**
248+
* Reverses the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex}
249+
* exclusive. This is equivalent to {@code
250+
* Collections.reverse(Bytes.asList(array).subList(fromIndex, toIndex))}, but is likely to be more
251+
* efficient.
252+
*
253+
* @throws IndexOutOfBoundsException if {@code fromIndex < 0}, {@code toIndex > array.length}, or
254+
* {@code toIndex > fromIndex}
255+
*/
256+
static void reverse(byte[] array, int fromIndex, int toIndex) {
257+
Objects.requireNonNull(array);
258+
for (int i = fromIndex, j = toIndex - 1; i < j; i++, j--) {
259+
byte tmp = array[i];
260+
array[i] = array[j];
261+
array[j] = tmp;
262+
}
263+
}
264+
247265
private static final int BUF_SIZE = 0x1000; // 4K
248266

249267
/**

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,35 @@ public void readFromStream() throws Exception {
192192
public void concatVararg() throws Exception {
193193
assertArrayEquals(new byte[]{1}, Util.concatVararg((byte) 1, null));
194194
}
195+
196+
@Test
197+
public void testReverse() {
198+
testReverse(new byte[]{}, new byte[]{});
199+
testReverse(new byte[]{1}, new byte[]{1});
200+
testReverse(new byte[]{1, 2}, new byte[]{2, 1});
201+
testReverse(new byte[]{3, 1, 1}, new byte[]{1, 1, 3});
202+
testReverse(new byte[]{-1, 1, -2, 2}, new byte[]{2, -2, 1, -1});
203+
}
204+
205+
@Test
206+
public void testReverseIndexed() {
207+
testReverse(new byte[]{}, 0, 0, new byte[]{});
208+
testReverse(new byte[]{1}, 0, 1, new byte[]{1});
209+
testReverse(new byte[]{1, 2}, 0, 2, new byte[]{2, 1});
210+
testReverse(new byte[]{3, 1, 1}, 0, 2, new byte[]{1, 3, 1});
211+
testReverse(new byte[]{3, 1, 1}, 0, 1, new byte[]{3, 1, 1});
212+
testReverse(new byte[]{-1, 1, -2, 2}, 1, 3, new byte[]{-1, -2, 1, 2});
213+
}
214+
215+
private static void testReverse(byte[] input, byte[] expectedOutput) {
216+
input = Arrays.copyOf(input, input.length);
217+
Util.reverse(input, 0, input.length);
218+
assertTrue(Arrays.equals(expectedOutput, input));
219+
}
220+
221+
private static void testReverse(byte[] input, int fromIndex, int toIndex, byte[] expectedOutput) {
222+
input = Arrays.copyOf(input, input.length);
223+
Util.reverse(input, fromIndex, toIndex);
224+
assertTrue(Arrays.equals(expectedOutput, input));
225+
}
195226
}

0 commit comments

Comments
 (0)