From b58826aaaffd095254ae3342b0d8631395d2838e Mon Sep 17 00:00:00 2001 From: Major- Date: Fri, 8 Aug 2014 06:33:13 +0100 Subject: [PATCH] Refactor some utility classes. --- src/org/apollo/util/CompressionUtil.java | 18 ++++++++---------- src/org/apollo/util/TextUtil.java | 6 +++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/org/apollo/util/CompressionUtil.java b/src/org/apollo/util/CompressionUtil.java index cc3ce697..16029374 100644 --- a/src/org/apollo/util/CompressionUtil.java +++ b/src/org/apollo/util/CompressionUtil.java @@ -14,7 +14,7 @@ import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; /** - * A utility class for performing compression/uncompression. + * A utility class for performing compression/decompression. * * @author Graham */ @@ -56,10 +56,10 @@ public final class CompressionUtil { } /** - * Unbzip2s the compressed array and places the result into the uncompressed array. + * Unbzip2s the compressed array and places the result into the decompressed array. * * @param compressed The compressed array. - * @param uncompressed The uncompressed array. + * @param uncompressed The decompressed array. * @throws IOException If an I/O error occurs. */ public static void unbzip2(byte[] compressed, byte[] uncompressed) throws IOException { @@ -77,10 +77,10 @@ public final class CompressionUtil { } /** - * Ungzips the compressed array and places the results into the uncompressed array. + * Ungzips the compressed array and places the results into the decompressed array. * * @param compressed The compressed array. - * @param uncompressed The uncompressed array. + * @param uncompressed The decompressed array. * @throws IOException If an I/O error occurs. */ public static void ungzip(byte[] compressed, byte[] uncompressed) throws IOException { @@ -93,15 +93,15 @@ public final class CompressionUtil { * Ungzips the compressed buffer and places the results into the returning array. * * @param compressed The compressed buffer. - * @return The uncompressed array. + * @return The decompressed array. * @throws IOException If an I/O error occurs. */ public static byte[] ungzip(ByteBuffer compressed) throws IOException { byte[] data = new byte[compressed.remaining()]; compressed.get(data); - InputStream is = new GZIPInputStream(new ByteArrayInputStream(data)); - try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { + try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(data)); + ByteArrayOutputStream os = new ByteArrayOutputStream()) { while (true) { byte[] buf = new byte[1024]; int read = is.read(buf, 0, buf.length); @@ -112,8 +112,6 @@ public final class CompressionUtil { } return os.toByteArray(); - } finally { - is.close(); } } diff --git a/src/org/apollo/util/TextUtil.java b/src/org/apollo/util/TextUtil.java index fa9a5ee8..0fce4aff 100644 --- a/src/org/apollo/util/TextUtil.java +++ b/src/org/apollo/util/TextUtil.java @@ -100,16 +100,16 @@ public final class TextUtil { * @return The filtered string. */ public static String filterInvalidCharacters(String str) { - StringBuilder bldr = new StringBuilder(); + StringBuilder builder = new StringBuilder(); for (char c : str.toLowerCase().toCharArray()) { for (char validChar : FREQUENCY_ORDERED_CHARS) { if (c == validChar) { - bldr.append((char) c); + builder.append((char) c); break; } } } - return bldr.toString(); + return builder.toString(); } /**