Skip to content

Commit e507231

Browse files
committed
Add method to see if a transfomer can transform in place
1 parent d2ea35b commit e507231

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public interface BytesTransformer {
3939
*/
4040
byte[] transform(byte[] currentArray, boolean inPlace);
4141

42+
/**
43+
* If this transformer supports transformation without creation a new array
44+
*
45+
* @return true if supported
46+
*/
47+
boolean supportInPlaceTransformation();
48+
4249
/**
4350
* Simple transformer for bitwise operations on {@link Bytes} instances
4451
*
@@ -85,6 +92,11 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
8592

8693
return out;
8794
}
95+
96+
@Override
97+
public boolean supportInPlaceTransformation() {
98+
return true;
99+
}
88100
}
89101

90102
/**
@@ -103,6 +115,11 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
103115

104116
return out;
105117
}
118+
119+
@Override
120+
public boolean supportInPlaceTransformation() {
121+
return true;
122+
}
106123
}
107124

108125
/**
@@ -137,6 +154,11 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
137154
return bigInt.shiftRight(shiftCount).toByteArray();
138155
}
139156
}
157+
158+
@Override
159+
public boolean supportInPlaceTransformation() {
160+
return true;
161+
}
140162
}
141163

142164
/**
@@ -156,6 +178,11 @@ final class ConcatTransformer implements BytesTransformer {
156178
public byte[] transform(byte[] currentArray, boolean inPlace) {
157179
return Util.concat(currentArray, secondArray);
158180
}
181+
182+
@Override
183+
public boolean supportInPlaceTransformation() {
184+
return false;
185+
}
159186
}
160187

161188
/**
@@ -173,6 +200,11 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
173200
}
174201
return out;
175202
}
203+
204+
@Override
205+
public boolean supportInPlaceTransformation() {
206+
return true;
207+
}
176208
}
177209

178210
/**
@@ -193,6 +225,11 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
193225
System.arraycopy(currentArray, offset, copy, 0, copy.length);
194226
return copy;
195227
}
228+
229+
@Override
230+
public boolean supportInPlaceTransformation() {
231+
return false;
232+
}
196233
}
197234

198235
/**
@@ -235,6 +272,11 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
235272

236273
return resizedArray;
237274
}
275+
276+
@Override
277+
public boolean supportInPlaceTransformation() {
278+
return false;
279+
}
238280
}
239281

240282
/**
@@ -267,14 +309,18 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
267309
}
268310
return out;
269311
}
312+
313+
@Override
314+
public boolean supportInPlaceTransformation() {
315+
return true;
316+
}
270317
}
271318

272319
/**
273320
* Converts to hash
274321
*/
275322
class MessageDigestTransformer implements BytesTransformer {
276323
final static String ALGORITHM_SHA_256 = "SHA-256";
277-
final static String ALGORITHM_SHA_512 = "SHA-512";
278324

279325
private final MessageDigest messageDigest;
280326

@@ -291,5 +337,10 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
291337
messageDigest.update(currentArray);
292338
return messageDigest.digest();
293339
}
340+
341+
@Override
342+
public boolean supportInPlaceTransformation() {
343+
return false;
344+
}
294345
}
295346
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
126126
Util.shuffle(out, random);
127127
return out;
128128
}
129+
130+
@Override
131+
public boolean supportInPlaceTransformation() {
132+
return true;
133+
}
129134
}
130135

131136
/**
@@ -155,6 +160,11 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
155160
return Bytes.from(list).array();
156161
}
157162
}
163+
164+
@Override
165+
public boolean supportInPlaceTransformation() {
166+
return comparator == null;
167+
}
158168
}
159169

160170
/**
@@ -197,6 +207,11 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
197207
return Bytes.from(currentArray, checksumBytes).array();
198208
}
199209
}
210+
211+
@Override
212+
public boolean supportInPlaceTransformation() {
213+
return false;
214+
}
200215
}
201216

202217
/**
@@ -251,5 +266,10 @@ private byte[] compress(byte[] content) {
251266
throw new IllegalStateException("could not compress gzip", e);
252267
}
253268
}
269+
270+
@Override
271+
public boolean supportInPlaceTransformation() {
272+
return false;
273+
}
254274
}
255275
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,47 @@ public void transform() throws Exception {
326326
public byte[] transform(byte[] currentArray, boolean inPlace) {
327327
return Bytes.from(currentArray).array();
328328
}
329+
330+
@Override
331+
public boolean supportInPlaceTransformation() {
332+
return false;
333+
}
329334
}).array());
330335
assertArrayEquals(new byte[2], Bytes.from(example_bytes_two).transform(new BytesTransformer() {
331336
@Override
332337
public byte[] transform(byte[] currentArray, boolean inPlace) {
333338
return Bytes.allocate(currentArray.length).array();
334339
}
340+
341+
@Override
342+
public boolean supportInPlaceTransformation() {
343+
return false;
344+
}
335345
}).array());
336346
}
347+
348+
@Test
349+
public void transformerInPlaceTest() throws Exception {
350+
assertTrue(new BytesTransformer.BitSwitchTransformer(0, true).supportInPlaceTransformation());
351+
assertTrue(new BytesTransformer.BitWiseOperatorTransformer(new byte[]{}, BytesTransformer.BitWiseOperatorTransformer.Mode.XOR).supportInPlaceTransformation());
352+
assertTrue(new BytesTransformer.NegateTransformer().supportInPlaceTransformation());
353+
assertTrue(new BytesTransformer.ShiftTransformer(0, BytesTransformer.ShiftTransformer.Type.LEFT_SHIFT).supportInPlaceTransformation());
354+
assertTrue(new BytesTransformer.ReverseTransformer().supportInPlaceTransformation());
355+
356+
assertFalse(new BytesTransformer.MessageDigestTransformer("SHA1").supportInPlaceTransformation());
357+
assertFalse(new BytesTransformer.CopyTransformer(0, 0).supportInPlaceTransformation());
358+
assertFalse(new BytesTransformer.ResizeTransformer(0).supportInPlaceTransformation());
359+
assertFalse(new BytesTransformer.ConcatTransformer(new byte[]{}).supportInPlaceTransformation());
360+
361+
assertFalse(new BytesTransformers.GzipCompressor(false).supportInPlaceTransformation());
362+
assertFalse(new BytesTransformers.ChecksumTransformer(new CRC32(), ChecksumTransformer.Mode.TRANSFORM, 4).supportInPlaceTransformation());
363+
assertTrue(new BytesTransformers.SortTransformer().supportInPlaceTransformation());
364+
assertFalse(new BytesTransformers.SortTransformer(new Comparator<Byte>() {
365+
@Override
366+
public int compare(Byte o1, Byte o2) {
367+
return 0;
368+
}
369+
}).supportInPlaceTransformation());
370+
assertFalse(new BytesTransformers.ShuffleTransformer(new SecureRandom()).supportInPlaceTransformation());
371+
}
337372
}

0 commit comments

Comments
 (0)