try with resources
This commit is contained in:
parent
d6b3d57560
commit
e2030eeba9
@ -12,79 +12,28 @@ import java.util.zip.GZIPOutputStream;
|
|||||||
*/
|
*/
|
||||||
public final class GZIPUtils {
|
public final class GZIPUtils {
|
||||||
|
|
||||||
private GZIPUtils() {}
|
private GZIPUtils() {
|
||||||
|
|
||||||
public static byte[] compress(byte[] in) {
|
|
||||||
ByteArrayOutputStream byteStream = null;
|
|
||||||
GZIPOutputStream gzipStream = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
byteStream = new ByteArrayOutputStream(in.length);
|
|
||||||
gzipStream = new GZIPOutputStream(byteStream);
|
|
||||||
gzipStream.write(in);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
|
|
||||||
// Clean up the byte array stream
|
|
||||||
if (byteStream != null) {
|
|
||||||
try {
|
|
||||||
byteStream.close();
|
|
||||||
} catch (IOException ignored) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up the GZIP compression stream
|
|
||||||
if (gzipStream != null) {
|
|
||||||
try {
|
|
||||||
gzipStream.close();
|
|
||||||
} catch (IOException ignored) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return byteStream != null ? byteStream.toByteArray(): null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] decompress(byte[] in) {
|
public static byte[] compress(byte[] in) throws IOException {
|
||||||
ByteArrayInputStream byteStream = null;
|
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(in.length);
|
||||||
GZIPInputStream gzipStream = null;
|
|
||||||
ByteArrayOutputStream outStream = null;
|
|
||||||
|
|
||||||
try {
|
try (GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream)) {
|
||||||
byteStream = new ByteArrayInputStream(in);
|
gzipStream.write(in);
|
||||||
gzipStream = new GZIPInputStream(byteStream);
|
}
|
||||||
outStream = new ByteArrayOutputStream();
|
return byteStream.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] decompress(byte[] in) throws IOException {
|
||||||
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
try (GZIPInputStream gzipStream = new GZIPInputStream(new ByteArrayInputStream(in))) {
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
int len;
|
int len;
|
||||||
while ((len = gzipStream.read(buffer)) > 0) {
|
while ((len = gzipStream.read(buffer)) > 0) {
|
||||||
outStream.write(buffer, 0, len);
|
outStream.write(buffer, 0, len);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
|
|
||||||
// Clean up the byte array stream
|
|
||||||
if (byteStream != null) {
|
|
||||||
try {
|
|
||||||
byteStream.close();
|
|
||||||
} catch (IOException ignored) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up the GZIP compression stream
|
|
||||||
if (gzipStream != null) {
|
|
||||||
try {
|
|
||||||
gzipStream.close();
|
|
||||||
} catch (IOException ignored) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up the byte output stream
|
|
||||||
if (outStream != null) {
|
|
||||||
try {
|
|
||||||
outStream.close();
|
|
||||||
} catch (IOException ignored) {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return outStream.toByteArray();
|
||||||
return outStream != null ? outStream.toByteArray() : null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user