DataHelper: Release resources in finally block

This commit is contained in:
zzz
2017-01-04 13:25:49 +00:00
parent 134cbd46e4
commit 6843950bdc

View File

@@ -1677,7 +1677,7 @@ public class DataHelper {
/** /**
* Decompress the GZIP compressed data (returning null on error). * Decompress the GZIP compressed data (returning null on error).
* @throws IOE if uncompressed is over 40 KB * @throws IOException if uncompressed is over 40 KB
*/ */
public static byte[] decompress(byte orig[]) throws IOException { public static byte[] decompress(byte orig[]) throws IOException {
return (orig != null ? decompress(orig, 0, orig.length) : null); return (orig != null ? decompress(orig, 0, orig.length) : null);
@@ -1685,7 +1685,7 @@ public class DataHelper {
/** /**
* Decompress the GZIP compressed data (returning null on error). * Decompress the GZIP compressed data (returning null on error).
* @throws IOE if uncompressed is over 40 KB * @throws IOException if uncompressed is over 40 KB
*/ */
public static byte[] decompress(byte orig[], int offset, int length) throws IOException { public static byte[] decompress(byte orig[], int offset, int length) throws IOException {
if ((orig == null) || (orig.length <= 0)) return orig; if ((orig == null) || (orig.length <= 0)) return orig;
@@ -1698,24 +1698,26 @@ public class DataHelper {
// don't make this a static field, or else I2PAppContext gets initialized too early // don't make this a static field, or else I2PAppContext gets initialized too early
ByteCache cache = ByteCache.getInstance(8, MAX_UNCOMPRESSED); ByteCache cache = ByteCache.getInstance(8, MAX_UNCOMPRESSED);
ByteArray outBuf = cache.acquire(); ByteArray outBuf = cache.acquire();
int written = 0; try {
while (true) { int written = 0;
int read = in.read(outBuf.getData(), written, MAX_UNCOMPRESSED-written); while (true) {
if (read == -1) int read = in.read(outBuf.getData(), written, MAX_UNCOMPRESSED-written);
break; if (read == -1)
written += read; break;
if (written >= MAX_UNCOMPRESSED) { written += read;
if (in.available() > 0) if (written >= MAX_UNCOMPRESSED) {
throw new IOException("Uncompressed data larger than " + MAX_UNCOMPRESSED); if (in.available() > 0)
break; throw new IOException("Uncompressed data larger than " + MAX_UNCOMPRESSED);
break;
}
} }
byte rv[] = new byte[written];
System.arraycopy(outBuf.getData(), 0, rv, 0, written);
return rv;
} finally {
cache.release(outBuf);
ReusableGZIPInputStream.release(in);
} }
byte rv[] = new byte[written];
System.arraycopy(outBuf.getData(), 0, rv, 0, written);
cache.release(outBuf);
// TODO release in finally block
ReusableGZIPInputStream.release(in);
return rv;
} }
/** /**