From e2030eeba9de1f62c94402752cbacbf5c68e529f Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sat, 4 Aug 2018 13:34:50 -0400 Subject: [PATCH] try with resources --- .../java/baritone/bot/utils/GZIPUtils.java | 77 ++++--------------- 1 file changed, 13 insertions(+), 64 deletions(-) diff --git a/src/main/java/baritone/bot/utils/GZIPUtils.java b/src/main/java/baritone/bot/utils/GZIPUtils.java index c5bb8612..00b894bf 100644 --- a/src/main/java/baritone/bot/utils/GZIPUtils.java +++ b/src/main/java/baritone/bot/utils/GZIPUtils.java @@ -12,79 +12,28 @@ import java.util.zip.GZIPOutputStream; */ public final class 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; + private GZIPUtils() { } - public static byte[] decompress(byte[] in) { - ByteArrayInputStream byteStream = null; - GZIPInputStream gzipStream = null; - ByteArrayOutputStream outStream = null; + public static byte[] compress(byte[] in) throws IOException { + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(in.length); - try { - byteStream = new ByteArrayInputStream(in); - gzipStream = new GZIPInputStream(byteStream); - outStream = new ByteArrayOutputStream(); + try (GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream)) { + gzipStream.write(in); + } + 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]; int len; while ((len = gzipStream.read(buffer)) > 0) { 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 != null ? outStream.toByteArray() : null; + return outStream.toByteArray(); } }