forked from I2P_Developers/i2p.i2p
DataHelper: Release resources in finally block
This commit is contained in:
@@ -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,6 +1698,7 @@ 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();
|
||||||
|
try {
|
||||||
int written = 0;
|
int written = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
int read = in.read(outBuf.getData(), written, MAX_UNCOMPRESSED-written);
|
int read = in.read(outBuf.getData(), written, MAX_UNCOMPRESSED-written);
|
||||||
@@ -1712,10 +1713,11 @@ public class DataHelper {
|
|||||||
}
|
}
|
||||||
byte rv[] = new byte[written];
|
byte rv[] = new byte[written];
|
||||||
System.arraycopy(outBuf.getData(), 0, rv, 0, written);
|
System.arraycopy(outBuf.getData(), 0, rv, 0, written);
|
||||||
cache.release(outBuf);
|
|
||||||
// TODO release in finally block
|
|
||||||
ReusableGZIPInputStream.release(in);
|
|
||||||
return rv;
|
return rv;
|
||||||
|
} finally {
|
||||||
|
cache.release(outBuf);
|
||||||
|
ReusableGZIPInputStream.release(in);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user