summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/java/com/jogamp/common/util/IOUtil.java16
-rw-r--r--src/java/com/jogamp/common/util/VersionUtil.java12
-rw-r--r--src/junit/com/jogamp/common/util/TestIOUtil01.java26
3 files changed, 48 insertions, 6 deletions
diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java
index 9e803cb..0463c37 100644
--- a/src/java/com/jogamp/common/util/IOUtil.java
+++ b/src/java/com/jogamp/common/util/IOUtil.java
@@ -30,6 +30,7 @@ package com.jogamp.common.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -585,4 +586,19 @@ public class IOUtil {
}
throw new IOException("Could not create temp directory @ "+tempRoot.getAbsolutePath()+tmpDirPrefix+"_*");
}
+
+ public static void close(Closeable stream, boolean throwRuntimeException) throws RuntimeException {
+ if(null != stream) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ if(throwRuntimeException) {
+ throw new RuntimeException(e);
+ } else if(DEBUG) {
+ System.err.println("Catched Exception: ");
+ e.printStackTrace();
+ }
+ }
+ }
+ }
}
diff --git a/src/java/com/jogamp/common/util/VersionUtil.java b/src/java/com/jogamp/common/util/VersionUtil.java
index 157edaa..30b23f2 100644
--- a/src/java/com/jogamp/common/util/VersionUtil.java
+++ b/src/java/com/jogamp/common/util/VersionUtil.java
@@ -32,6 +32,7 @@ import com.jogamp.common.os.AndroidVersion;
import com.jogamp.common.os.Platform;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
@@ -90,10 +91,17 @@ public class VersionUtil {
* @return the requested manifest or null when not found.
*/
public static Manifest getManifest(ClassLoader cl, String extension) {
+
try {
- Enumeration resources = cl.getResources("META-INF/MANIFEST.MF");
+ Enumeration<URL> resources = cl.getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
- Manifest manifest = new Manifest(((URL)resources.nextElement()).openStream());
+ final InputStream is = resources.nextElement().openStream();
+ final Manifest manifest;
+ try {
+ manifest = new Manifest(is);
+ } finally {
+ IOUtil.close(is, false);
+ }
Attributes attributes = manifest.getMainAttributes();
if(attributes != null && extension.equals(attributes.getValue(Attributes.Name.EXTENSION_NAME))) {
return manifest;
diff --git a/src/junit/com/jogamp/common/util/TestIOUtil01.java b/src/junit/com/jogamp/common/util/TestIOUtil01.java
index cac6cd8..47fa1e9 100644
--- a/src/junit/com/jogamp/common/util/TestIOUtil01.java
+++ b/src/junit/com/jogamp/common/util/TestIOUtil01.java
@@ -71,7 +71,13 @@ public class TestIOUtil01 extends JunitTracer {
public void testCopyStream01Array() throws IOException {
URL url = IOUtil.getResource(this.getClass(), tfilename);
Assert.assertNotNull(url);
- final byte[] bb = IOUtil.copyStream2ByteArray( new BufferedInputStream( url.openStream() ) );
+ final BufferedInputStream bis = new BufferedInputStream( url.openStream() );
+ final byte[] bb;
+ try {
+ bb = IOUtil.copyStream2ByteArray( bis );
+ } finally {
+ IOUtil.close(bis, false);
+ }
Assert.assertEquals("Byte number not equal orig vs array", orig.length, bb.length);
Assert.assertTrue("Bytes not equal orig vs array", Arrays.equals(orig, bb));
@@ -81,7 +87,13 @@ public class TestIOUtil01 extends JunitTracer {
public void testCopyStream02Buffer() throws IOException {
URL url = IOUtil.getResource(this.getClass(), tfilename);
Assert.assertNotNull(url);
- final ByteBuffer bb = IOUtil.copyStream2ByteBuffer( new BufferedInputStream( url.openStream() ) );
+ final BufferedInputStream bis = new BufferedInputStream( url.openStream() );
+ final ByteBuffer bb;
+ try {
+ bb = IOUtil.copyStream2ByteBuffer( bis );
+ } finally {
+ IOUtil.close(bis, false);
+ }
Assert.assertEquals("Byte number not equal orig vs buffer", orig.length, bb.limit());
int i;
for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
@@ -98,8 +110,14 @@ public class TestIOUtil01 extends JunitTracer {
IOUtil.copyURL2File(url1, file2);
URL url2 = IOUtil.getResource(this.getClass(), tfilename2);
Assert.assertNotNull(url2);
-
- final ByteBuffer bb = IOUtil.copyStream2ByteBuffer( new BufferedInputStream( url2.openStream() ) );
+
+ final BufferedInputStream bis = new BufferedInputStream( url2.openStream() );
+ final ByteBuffer bb;
+ try {
+ bb = IOUtil.copyStream2ByteBuffer( bis );
+ } finally {
+ IOUtil.close(bis, false);
+ }
Assert.assertEquals("Byte number not equal orig vs buffer", orig.length, bb.limit());
int i;
for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;