Merge pull request #1458 from rbri:java_code_cleanup PiperOrigin-RevId: 900612609
diff --git a/java/org/brotli/dec/CompoundDictionaryTest.java b/java/org/brotli/dec/CompoundDictionaryTest.java index 2097117..087704f 100644 --- a/java/org/brotli/dec/CompoundDictionaryTest.java +++ b/java/org/brotli/dec/CompoundDictionaryTest.java
@@ -10,6 +10,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; + import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -30,34 +31,37 @@ @Test public void testNoDictionary() throws IOException { - BrotliInputStream decoder = new BrotliInputStream(new ByteArrayInputStream(ONE_COPY)); - byte[] buffer = new byte[32]; - int length = decoder.read(buffer, 0, buffer.length); - assertEquals(TEXT.length(), length); - assertEquals("alternate\" type=\"appli", new String(buffer, 0, length, "US-ASCII")); - decoder.close(); + try (BrotliInputStream decoder = new BrotliInputStream(new ByteArrayInputStream(ONE_COPY))) { + byte[] buffer = new byte[32]; + int length = decoder.read(buffer, 0, buffer.length); + assertEquals(TEXT.length(), length); + assertEquals("alternate\" type=\"appli", new String(buffer, 0, length, "US-ASCII")); + decoder.close(); + } } @Test public void testOnePieceDictionary() throws IOException { - BrotliInputStream decoder = new BrotliInputStream(new ByteArrayInputStream(ONE_COPY)); - decoder.attachDictionaryChunk(TEXT.getBytes("US-ASCII")); - byte[] buffer = new byte[32]; - int length = decoder.read(buffer, 0, buffer.length); - assertEquals(TEXT.length(), length); - assertEquals(TEXT, new String(buffer, 0, length, "US-ASCII")); - decoder.close(); + try (BrotliInputStream decoder = new BrotliInputStream(new ByteArrayInputStream(ONE_COPY))) { + decoder.attachDictionaryChunk(TEXT.getBytes("US-ASCII")); + byte[] buffer = new byte[32]; + int length = decoder.read(buffer, 0, buffer.length); + assertEquals(TEXT.length(), length); + assertEquals(TEXT, new String(buffer, 0, length, "US-ASCII")); + decoder.close(); + } } @Test public void testTwoPieceDictionary() throws IOException { - BrotliInputStream decoder = new BrotliInputStream(new ByteArrayInputStream(ONE_COPY)); - decoder.attachDictionaryChunk(TEXT.substring(0, 13).getBytes("US-ASCII")); - decoder.attachDictionaryChunk(TEXT.substring(13).getBytes("US-ASCII")); - byte[] buffer = new byte[32]; - int length = decoder.read(buffer, 0, buffer.length); - assertEquals(TEXT.length(), length); - assertEquals(TEXT, new String(buffer, 0, length, "US-ASCII")); - decoder.close(); + try (BrotliInputStream decoder = new BrotliInputStream(new ByteArrayInputStream(ONE_COPY))) { + decoder.attachDictionaryChunk(TEXT.substring(0, 13).getBytes("US-ASCII")); + decoder.attachDictionaryChunk(TEXT.substring(13).getBytes("US-ASCII")); + byte[] buffer = new byte[32]; + int length = decoder.read(buffer, 0, buffer.length); + assertEquals(TEXT.length(), length); + assertEquals(TEXT, new String(buffer, 0, length, "US-ASCII")); + decoder.close(); + } } }
diff --git a/java/org/brotli/dec/DecodeTest.java b/java/org/brotli/dec/DecodeTest.java index db724e3..6fc70e7 100644 --- a/java/org/brotli/dec/DecodeTest.java +++ b/java/org/brotli/dec/DecodeTest.java
@@ -14,6 +14,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; + import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -26,27 +27,27 @@ byte[] buffer = new byte[65536]; ByteArrayInputStream input = new ByteArrayInputStream(data); ByteArrayOutputStream output = new ByteArrayOutputStream(); - InputStream brotliInput = newBrotliInputStream(input); - if (byByte) { - byte[] oneByte = new byte[1]; - while (true) { - int next = brotliInput.read(); - if (next == -1) { - break; + try (InputStream brotliInput = newBrotliInputStream(input)) { + if (byByte) { + byte[] oneByte = new byte[1]; + while (true) { + int next = brotliInput.read(); + if (next == -1) { + break; + } + oneByte[0] = (byte) next; + output.write(oneByte, 0, 1); } - oneByte[0] = (byte) next; - output.write(oneByte, 0, 1); - } - } else { - while (true) { - int len = brotliInput.read(buffer, 0, buffer.length); - if (len <= 0) { - break; + } else { + while (true) { + int len = brotliInput.read(buffer, 0, buffer.length); + if (len <= 0) { + break; + } + output.write(buffer, 0, len); } - output.write(buffer, 0, len); } } - brotliInput.close(); return output.toByteArray(); }
diff --git a/java/org/brotli/dec/Decoder.java b/java/org/brotli/dec/Decoder.java index 4adac65..b9d848f 100644 --- a/java/org/brotli/dec/Decoder.java +++ b/java/org/brotli/dec/Decoder.java
@@ -12,15 +12,12 @@ throws IOException { long totalOut = 0; int readBytes; - BrotliInputStream in = new BrotliInputStream(input); - in.enableLargeWindow(); - try { + try (BrotliInputStream in = new BrotliInputStream(input)) { + in.enableLargeWindow(); while ((readBytes = in.read(buffer)) >= 0) { output.write(buffer, 0, readBytes); totalOut += readBytes; } - } finally { - in.close(); } return totalOut; } @@ -29,23 +26,12 @@ long start; long bytesDecoded; long end; - InputStream in = null; - OutputStream out = null; - try { - in = new FileInputStream(fromPath); - out = new FileOutputStream(toPath); + try (InputStream in = new FileInputStream(fromPath); + OutputStream out = new FileOutputStream(toPath)) { start = System.nanoTime(); bytesDecoded = decodeBytes(in, out, buffer); end = System.nanoTime(); - } finally { - if (in != null) { - in.close(); // Hopefully, does not throw exception. - } - if (out != null) { - out.close(); - } } - double timeDelta = (end - start) / 1000000000.0; if (timeDelta <= 0) { return;