You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2016/06/13 07:28:33 UTC
svn commit: r1748106 -
/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java
Author: ggregory
Date: Mon Jun 13 07:28:33 2016
New Revision: 1748106
URL: http://svn.apache.org/viewvc?rev=1748106&view=rev
Log:
Use try-with-resources. Note that ByteArrayOuputStream#close() is a noop so it is OK to move BAOS.toByteArray() inside a try-w-resources block.
Modified:
commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java
Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java?rev=1748106&r1=1748105&r2=1748106&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java (original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java Mon Jun 13 07:28:33 2016
@@ -1264,14 +1264,13 @@ public abstract class Utility {
*
* @throws IOException if there's a gzip exception
*/
- public static String encode( byte[] bytes, final boolean compress ) throws IOException {
+ public static String encode(byte[] bytes, final boolean compress) throws IOException {
if (compress) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- GZIPOutputStream gos = new GZIPOutputStream(baos);
- gos.write(bytes, 0, bytes.length);
- gos.close();
- baos.close();
- bytes = baos.toByteArray();
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ GZIPOutputStream gos = new GZIPOutputStream(baos)) {
+ gos.write(bytes, 0, bytes.length);
+ bytes = baos.toByteArray();
+ }
}
CharArrayWriter caw = new CharArrayWriter();
try (JavaWriter jw = new JavaWriter(caw)) {
@@ -1292,19 +1291,16 @@ public abstract class Utility {
*
* @throws IOException if there's a gzip exception
*/
- public static byte[] decode( final String s, final boolean uncompress ) throws IOException {
- char[] chars = s.toCharArray();
- CharArrayReader car = new CharArrayReader(chars);
- JavaReader jr = new JavaReader(car);
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- int ch;
- while ((ch = jr.read()) >= 0) {
- bos.write(ch);
+ public static byte[] decode(final String s, final boolean uncompress) throws IOException {
+ byte[] bytes;
+ try (JavaReader jr = new JavaReader(new CharArrayReader(s.toCharArray()));
+ ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+ int ch;
+ while ((ch = jr.read()) >= 0) {
+ bos.write(ch);
+ }
+ bytes = bos.toByteArray();
}
- bos.close();
- car.close();
- jr.close();
- byte[] bytes = bos.toByteArray();
if (uncompress) {
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
byte[] tmp = new byte[bytes.length * 3]; // Rough estimate