11package at .favre .lib .bytes ;
22
3+ import java .io .ByteArrayInputStream ;
4+ import java .io .ByteArrayOutputStream ;
35import java .util .Objects ;
46import java .util .zip .CRC32 ;
57import java .util .zip .Checksum ;
8+ import java .util .zip .GZIPInputStream ;
9+ import java .util .zip .GZIPOutputStream ;
610
711/**
812 * Collection of additional {@link BytesTransformer} for more specific use cases
@@ -30,10 +34,6 @@ public static BytesTransformer checksumCrc32() {
3034 return new ChecksumTransformer (new CRC32 (), ChecksumTransformer .Mode .TRANSFORM , 4 );
3135 }
3236
33- /**
34- * Create a {@link BytesTransformer} which transforms to 4 byte Crc32 checksum of given bytes
35- * @return transformer
36- */
3737 /**
3838 * Create a {@link BytesTransformer} which transforms to 4 byte Crc32 checksum of given bytes
3939 *
@@ -47,6 +47,28 @@ public static BytesTransformer checksum(Checksum checksum, ChecksumTransformer.M
4747 return new ChecksumTransformer (checksum , mode , checksumLengthByte );
4848 }
4949
50+ /**
51+ * Create a {@link BytesTransformer} which gzip compresses the internal byte array
52+ *
53+ * @return transformer
54+ * @throws IllegalStateException if compression was not possible (i.e. wrapped {@link java.io.IOException})
55+ * @see <a href="https://en.wikipedia.org/wiki/Gzip">Gzip</a>
56+ */
57+ public static BytesTransformer compressGzip () {
58+ return new GzipCompressor (true );
59+ }
60+
61+ /**
62+ * Create a {@link BytesTransformer} which gzip decompresses the internal byte array
63+ *
64+ * @return transformer
65+ * @throws IllegalStateException if compression was not possible (i.e. wrapped {@link java.io.IOException})
66+ * @see <a href="https://en.wikipedia.org/wiki/Gzip">Gzip</a>
67+ */
68+ public static BytesTransformer decompressGzip () {
69+ return new GzipCompressor (false );
70+ }
71+
5072 /**
5173 * Adds or converts to arbitrary checksum
5274 */
@@ -67,7 +89,7 @@ enum Mode {
6789 private final int checksumLengthByte ;
6890
6991 public ChecksumTransformer (Checksum checksum , Mode mode , int checksumLengthByte ) {
70- if (checksumLengthByte < 0 || checksumLengthByte > 8 )
92+ if (checksumLengthByte <= 0 || checksumLengthByte > 8 )
7193 throw new IllegalArgumentException ("checksumlength must be between 1 and 8 bytes" );
7294
7395 Objects .requireNonNull (checksum , "checksum instance must not be null" );
@@ -88,4 +110,58 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
88110 }
89111 }
90112 }
113+
114+ /**
115+ * Byte compression with gzip
116+ */
117+ final static class GzipCompressor implements BytesTransformer {
118+ private final boolean compress ;
119+
120+ public GzipCompressor (boolean compress ) {
121+ this .compress = compress ;
122+ }
123+
124+ @ Override
125+ public byte [] transform (byte [] currentArray , boolean inPlace ) {
126+ return compress ? compress (currentArray ) : decompress (currentArray );
127+ }
128+
129+ private byte [] decompress (byte [] compressedContent ) {
130+ ByteArrayOutputStream bos = new ByteArrayOutputStream ();
131+ GZIPInputStream gzipInputStream = null ;
132+ byte [] returnBuffer ;
133+ try {
134+ int len ;
135+ byte buffer [] = new byte [4 * 1024 ];
136+ gzipInputStream = new GZIPInputStream (new ByteArrayInputStream (compressedContent ));
137+
138+ while ((len = gzipInputStream .read (buffer )) > 0 ) {
139+ bos .write (buffer , 0 , len );
140+ }
141+
142+ gzipInputStream .close ();
143+ returnBuffer = bos .toByteArray ();
144+ bos .close ();
145+ return returnBuffer ;
146+ } catch (Exception e ) {
147+ throw new IllegalStateException ("could not decompress gzip" , e );
148+ }
149+ }
150+
151+ private byte [] compress (byte [] content ) {
152+ ByteArrayOutputStream bos = new ByteArrayOutputStream (content .length );
153+ GZIPOutputStream gzipOutputStream = null ;
154+ byte [] returnBuffer ;
155+ try {
156+ gzipOutputStream = new GZIPOutputStream (bos );
157+ gzipOutputStream .write (content );
158+ gzipOutputStream .close ();
159+ returnBuffer = bos .toByteArray ();
160+ bos .close ();
161+ return returnBuffer ;
162+ } catch (Exception e ) {
163+ throw new IllegalStateException ("could not compress gzip" , e );
164+ }
165+ }
166+ }
91167}
0 commit comments