22
33import java .io .ByteArrayInputStream ;
44import java .io .ByteArrayOutputStream ;
5- import java .util .Objects ;
5+ import java .security .SecureRandom ;
6+ import java .util .*;
67import java .util .zip .CRC32 ;
78import java .util .zip .Checksum ;
89import java .util .zip .GZIPInputStream ;
@@ -16,6 +17,45 @@ public final class BytesTransformers {
1617 private BytesTransformers () {
1718 }
1819
20+ /**
21+ * Create a {@link BytesTransformer} which shuffles the individual bytes in an array
22+ * with an {@link SecureRandom} instance.
23+ *
24+ * @return transformer
25+ */
26+ public static BytesTransformer shuffle () {
27+ return new ShuffleTransformer (new SecureRandom ());
28+ }
29+
30+ /**
31+ * Create a {@link BytesTransformer} which shuffles the individual bytes in an array
32+ *
33+ * @param random to use for entropy
34+ * @return transformer
35+ */
36+ public static BytesTransformer shuffle (Random random ) {
37+ return new ShuffleTransformer (random );
38+ }
39+
40+ /**
41+ * Create a {@link BytesTransformer} which sorts the internal byte array with it's natural ordering.
42+ *
43+ * @return transformer
44+ */
45+ public static BytesTransformer sort () {
46+ return new SortTransformer ();
47+ }
48+
49+ /**
50+ * Create a {@link BytesTransformer} which sorts the internal byte array according to given comparator.
51+ *
52+ * @param comparator to sort the bytes
53+ * @return transformer
54+ */
55+ public static BytesTransformer sort (Comparator <Byte > comparator ) {
56+ return new SortTransformer (comparator );
57+ }
58+
1959 /**
2060 * Create a {@link BytesTransformer} which appends 4 byte Crc32 checksum to given bytes
2161 *
@@ -69,6 +109,54 @@ public static BytesTransformer decompressGzip() {
69109 return new GzipCompressor (false );
70110 }
71111
112+ /**
113+ * Shuffles the internal byte array
114+ */
115+ final static class ShuffleTransformer implements BytesTransformer {
116+ private final Random random ;
117+
118+ ShuffleTransformer (Random random ) {
119+ Objects .requireNonNull (random , "passed random must not be null" );
120+ this .random = random ;
121+ }
122+
123+ @ Override
124+ public byte [] transform (byte [] currentArray , boolean inPlace ) {
125+ byte [] out = inPlace ? currentArray : Bytes .from (currentArray ).array ();
126+ Util .shuffle (out , random );
127+ return out ;
128+ }
129+ }
130+
131+ /**
132+ * Sorts the internal byte array with given {@link java.util.Comparator}
133+ */
134+ final static class SortTransformer implements BytesTransformer {
135+ private final Comparator <Byte > comparator ;
136+
137+ SortTransformer () {
138+ this (null );
139+ }
140+
141+ SortTransformer (Comparator <Byte > comparator ) {
142+ this .comparator = comparator ;
143+ }
144+
145+ @ Override
146+ public byte [] transform (byte [] currentArray , boolean inPlace ) {
147+ if (comparator == null ) {
148+ byte [] out = inPlace ? currentArray : Bytes .from (currentArray ).array ();
149+ Arrays .sort (out );
150+ return out ;
151+ } else {
152+ //no in-place implementation with comparator
153+ List <Byte > list = Bytes .wrap (currentArray ).toList ();
154+ Collections .sort (list , comparator );
155+ return Bytes .from (list ).array ();
156+ }
157+ }
158+ }
159+
72160 /**
73161 * Adds or converts to arbitrary checksum
74162 */
@@ -88,7 +176,7 @@ enum Mode {
88176 private final Mode mode ;
89177 private final int checksumLengthByte ;
90178
91- public ChecksumTransformer (Checksum checksum , Mode mode , int checksumLengthByte ) {
179+ ChecksumTransformer (Checksum checksum , Mode mode , int checksumLengthByte ) {
92180 if (checksumLengthByte <= 0 || checksumLengthByte > 8 )
93181 throw new IllegalArgumentException ("checksumlength must be between 1 and 8 bytes" );
94182
@@ -117,7 +205,7 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
117205 final static class GzipCompressor implements BytesTransformer {
118206 private final boolean compress ;
119207
120- public GzipCompressor (boolean compress ) {
208+ GzipCompressor (boolean compress ) {
121209 this .compress = compress ;
122210 }
123211
0 commit comments