Skip to content

Commit 4c35f89

Browse files
committed
Add resize modes
1 parent 0ff21ec commit 4c35f89

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,37 @@ public Bytes reverse() {
735735
* contain identical values. For any indices that are valid in the
736736
* copy but not the original, the copy will contain {@code (byte)0}.
737737
* <p>
738-
* If if the internal array will be grown, zero bytes will be added on the left,
739-
* keeping the value the same.
738+
* Resize from LSB or length, so an array [0,1,2,3] resized to 3 will result in [1,2,3] or resized to 5 [0,0,1,2,3].
739+
* So when a 8 byte value resized to 4 byte will result in the same 32 bit integer value
740740
*
741741
* @param newByteLength the length of the copy to be returned
742742
* @return a copy with the desired size or "this" instance if newByteLength == current length
743743
*/
744744
public Bytes resize(int newByteLength) {
745-
return transform(new BytesTransformer.ResizeTransformer(newByteLength));
745+
return resize(newByteLength, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_MAX_LENGTH);
746+
}
747+
748+
/**
749+
* Copies the specified array, truncating or padding with zeros (if necessary)
750+
* so the copy has the specified length. For all indices that are
751+
* valid in both the original array and the copy, the two arrays will
752+
* contain identical values. For any indices that are valid in the
753+
* copy but not the original, the copy will contain {@code (byte)0}.
754+
* <p>
755+
* <strong>Modes:</strong>
756+
* <ul>
757+
* <li>{@link BytesTransformer.ResizeTransformer.Mode#RESIZE_KEEP_FROM_ZERO_INDEX}: Resize from MSB or index 0;
758+
* so an array [0,1,2,3] resized to 3 will result in [0,1,2] or resized to 5 [0,1,2,3,0]</li>
759+
* <li>{@link BytesTransformer.ResizeTransformer.Mode#RESIZE_KEEP_FROM_MAX_LENGTH}: Resize from LSB or length;
760+
* so an array [0,1,2,3] resized to 3 will result in [1,2,3] or resized to 5 [0,0,1,2,3]</li>
761+
* </ul>
762+
*
763+
* @param newByteLength the length of the copy to be returned
764+
* @param mode from which end the length will start to count (either index 0 or length())
765+
* @return a copy with the desired size or "this" instance if newByteLength == current length
766+
*/
767+
public Bytes resize(int newByteLength, BytesTransformer.ResizeTransformer.Mode mode) {
768+
return transform(new BytesTransformer.ResizeTransformer(newByteLength, mode));
746769
}
747770

748771
/**

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,17 @@ public boolean supportInPlaceTransformation() {
243243
* keeping the value the same.
244244
*/
245245
final class ResizeTransformer implements BytesTransformer {
246+
public enum Mode {
247+
RESIZE_KEEP_FROM_ZERO_INDEX,
248+
RESIZE_KEEP_FROM_MAX_LENGTH
249+
}
250+
246251
private final int newSize;
252+
private final Mode mode;
247253

248-
ResizeTransformer(int newSize) {
254+
ResizeTransformer(int newSize, Mode mode) {
249255
this.newSize = newSize;
256+
this.mode = mode;
250257
}
251258

252259
@Override
@@ -264,10 +271,15 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
264271
}
265272

266273
byte[] resizedArray = new byte[newSize];
267-
if (newSize > currentArray.length) {
268-
System.arraycopy(currentArray, 0, resizedArray, Math.max(0, Math.abs(newSize - currentArray.length)), Math.min(newSize, currentArray.length));
274+
275+
if (mode == Mode.RESIZE_KEEP_FROM_MAX_LENGTH) {
276+
if (newSize > currentArray.length) {
277+
System.arraycopy(currentArray, 0, resizedArray, Math.max(0, Math.abs(newSize - currentArray.length)), Math.min(newSize, currentArray.length));
278+
} else {
279+
System.arraycopy(currentArray, Math.max(0, Math.abs(newSize - currentArray.length)), resizedArray, Math.min(0, Math.abs(newSize - currentArray.length)), Math.min(newSize, currentArray.length));
280+
}
269281
} else {
270-
System.arraycopy(currentArray, Math.max(0, Math.abs(newSize - currentArray.length)), resizedArray, Math.min(0, Math.abs(newSize - currentArray.length)), Math.min(newSize, currentArray.length));
282+
System.arraycopy(currentArray, 0, resizedArray, 0, Math.min(currentArray.length, resizedArray.length));
271283
}
272284

273285
return resizedArray;

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void appendMulti() throws Exception {
6767
}
6868

6969
@Test
70-
public void resizeGrow() throws Exception {
70+
public void resizeGrowLsb() throws Exception {
7171
assertArrayEquals(new byte[8], Bytes.from(new byte[0]).resize(8).array());
7272
assertArrayEquals(example_bytes_one, Bytes.from(example_bytes_one).resize(1).array());
7373
assertArrayEquals(example_bytes_one, Bytes.from(example_bytes_one).resize(1).array());
@@ -77,7 +77,17 @@ public void resizeGrow() throws Exception {
7777
}
7878

7979
@Test
80-
public void resizeShrink() throws Exception {
80+
public void resizeGrowMsb() throws Exception {
81+
assertArrayEquals(new byte[8], Bytes.from(new byte[0]).resize(8, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
82+
assertArrayEquals(example_bytes_one, Bytes.from(example_bytes_one).resize(1, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
83+
assertArrayEquals(example_bytes_one, Bytes.from(example_bytes_one).resize(1, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
84+
assertArrayEquals(Util.concat(example_bytes_one, new byte[7]), Bytes.from(example_bytes_one).resize(8, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
85+
assertArrayEquals(Util.concat(example_bytes_seven, new byte[1]), Bytes.from(example_bytes_seven).resize(8, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
86+
assertArrayEquals(Util.concat(example_bytes_sixteen, new byte[1]), Bytes.from(example_bytes_sixteen).resize(17, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
87+
}
88+
89+
@Test
90+
public void resizeShrinkLsb() throws Exception {
8191
assertArrayEquals(new byte[0], Bytes.from(example_bytes_one).resize(0).array());
8292
assertArrayEquals(new byte[]{example_bytes_two[1]}, Bytes.from(example_bytes_two).resize(1).array());
8393
assertArrayEquals(new byte[]{example_bytes_four[3]}, Bytes.from(example_bytes_four).resize(1).array());
@@ -89,8 +99,21 @@ public void resizeShrink() throws Exception {
8999
try {
90100
Bytes.from(new byte[0]).resize(-1);
91101
fail();
92-
} catch (IllegalArgumentException e) {
102+
} catch (IllegalArgumentException ignore) {
103+
}
104+
}
93105

106+
@Test
107+
public void resizeShrinkMsb() throws Exception {
108+
assertArrayEquals(new byte[0], Bytes.from(example_bytes_one).resize(0, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
109+
assertArrayEquals(new byte[]{example_bytes_two[0]}, Bytes.from(example_bytes_two).resize(1, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
110+
assertArrayEquals(new byte[]{example_bytes_four[0]}, Bytes.from(example_bytes_four).resize(1, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
111+
assertArrayEquals(new byte[]{example_bytes_four[0], example_bytes_four[1]}, Bytes.from(example_bytes_four).resize(2, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX).array());
112+
113+
try {
114+
Bytes.from(new byte[0]).resize(-1, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_ZERO_INDEX);
115+
fail();
116+
} catch (IllegalArgumentException ignore) {
94117
}
95118
}
96119

@@ -142,7 +165,7 @@ public void or() throws Exception {
142165
try {
143166
Bytes.from(example_bytes_seven).or(example_bytes_eight);
144167
fail();
145-
} catch (Exception e) {
168+
} catch (Exception ignore) {
146169
}
147170
}
148171

@@ -162,7 +185,7 @@ public void and() throws Exception {
162185
try {
163186
Bytes.from(example_bytes_seven).and(example_bytes_eight);
164187
fail();
165-
} catch (Exception e) {
188+
} catch (Exception ignore) {
166189
}
167190
}
168191

@@ -355,7 +378,7 @@ public void transformerInPlaceTest() throws Exception {
355378

356379
assertFalse(new BytesTransformer.MessageDigestTransformer("SHA1").supportInPlaceTransformation());
357380
assertFalse(new BytesTransformer.CopyTransformer(0, 0).supportInPlaceTransformation());
358-
assertFalse(new BytesTransformer.ResizeTransformer(0).supportInPlaceTransformation());
381+
assertFalse(new BytesTransformer.ResizeTransformer(0, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_MAX_LENGTH).supportInPlaceTransformation());
359382
assertFalse(new BytesTransformer.ConcatTransformer(new byte[]{}).supportInPlaceTransformation());
360383

361384
assertFalse(new BytesTransformers.GzipCompressor(false).supportInPlaceTransformation());

0 commit comments

Comments
 (0)