| 1 |
package org.apache.commons.jcs3.utils.zip; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.io.ByteArrayInputStream; |
| 23 |
import java.io.ByteArrayOutputStream; |
| 24 |
import java.io.IOException; |
| 25 |
import java.util.zip.DataFormatException; |
| 26 |
import java.util.zip.Deflater; |
| 27 |
import java.util.zip.GZIPInputStream; |
| 28 |
import java.util.zip.Inflater; |
| 29 |
|
| 30 |
import org.apache.commons.jcs3.log.Log; |
| 31 |
import org.apache.commons.jcs3.log.LogManager; |
| 32 |
|
| 33 |
|
| 34 |
public final class CompressionUtil |
| 35 |
{ |
| 36 |
|
| 37 |
private static final Log log = LogManager.getLog( CompressionUtil.class ); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private CompressionUtil() |
| 43 |
{ |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static byte[] decompressByteArray( final byte[] input ) |
| 54 |
{ |
| 55 |
return decompressByteArray( input, 1024 ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static byte[] decompressByteArray( final byte[] input, final int bufferLength ) |
| 66 |
{ |
| 67 |
if ( null == input ) |
| 68 |
{ |
| 69 |
throw new IllegalArgumentException( "Input was null" ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
final Inflater decompressor = new Inflater(); |
| 74 |
|
| 75 |
decompressor.setInput( input ); |
| 76 |
|
| 77 |
|
| 78 |
final ByteArrayOutputStream baos = new ByteArrayOutputStream( input.length ); |
| 79 |
|
| 80 |
|
| 81 |
final byte[] buf = new byte[bufferLength]; |
| 82 |
|
| 83 |
try |
| 84 |
{ |
| 85 |
while ( !decompressor.finished() ) |
| 86 |
{ |
| 87 |
final int count = decompressor.inflate( buf ); |
| 88 |
baos.write( buf, 0, count ); |
| 89 |
} |
| 90 |
} |
| 91 |
catch ( final DataFormatException ex ) |
| 92 |
{ |
| 93 |
log.error( "Problem decompressing.", ex ); |
| 94 |
} |
| 95 |
|
| 96 |
decompressor.end(); |
| 97 |
|
| 98 |
try |
| 99 |
{ |
| 100 |
baos.close(); |
| 101 |
} |
| 102 |
catch ( final IOException ex ) |
| 103 |
{ |
| 104 |
log.error( "Problem closing stream.", ex ); |
| 105 |
} |
| 106 |
|
| 107 |
return baos.toByteArray(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static byte[] compressByteArray( final byte[] input ) |
| 118 |
throws IOException |
| 119 |
{ |
| 120 |
return compressByteArray( input, 1024 ); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static byte[] compressByteArray( final byte[] input, final int bufferLength ) |
| 132 |
throws IOException |
| 133 |
{ |
| 134 |
|
| 135 |
final Deflater compressor = new Deflater(); |
| 136 |
compressor.setLevel( Deflater.BEST_COMPRESSION ); |
| 137 |
|
| 138 |
|
| 139 |
compressor.setInput( input ); |
| 140 |
compressor.finish(); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
final ByteArrayOutputStream bos = new ByteArrayOutputStream( input.length ); |
| 146 |
|
| 147 |
|
| 148 |
final byte[] buf = new byte[bufferLength]; |
| 149 |
while ( !compressor.finished() ) |
| 150 |
{ |
| 151 |
final int count = compressor.deflate( buf ); |
| 152 |
bos.write( buf, 0, count ); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
compressor.end(); |
| 157 |
bos.close(); |
| 158 |
|
| 159 |
|
| 160 |
return bos.toByteArray(); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static byte[] decompressGzipByteArray( final byte[] compressedByteArray ) |
| 172 |
throws IOException |
| 173 |
{ |
| 174 |
return decompressGzipByteArray( compressedByteArray, 1024 ); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public static byte[] decompressGzipByteArray( final byte[] compressedByteArray, final int bufferlength ) |
| 186 |
throws IOException |
| 187 |
{ |
| 188 |
final ByteArrayOutputStream uncompressedStream = new ByteArrayOutputStream(); |
| 189 |
|
| 190 |
final GZIPInputStream compressedStream = new GZIPInputStream( new ByteArrayInputStream( compressedByteArray ) ); |
| 191 |
|
| 192 |
final byte[] buffer = new byte[bufferlength]; |
| 193 |
|
| 194 |
int index = -1; |
| 195 |
|
| 196 |
while ( ( index = compressedStream.read( buffer ) ) != -1 ) |
| 197 |
{ |
| 198 |
uncompressedStream.write( buffer, 0, index ); |
| 199 |
} |
| 200 |
|
| 201 |
return uncompressedStream.toByteArray(); |
| 202 |
} |
| 203 |
} |