From beadbb27530b75507ffa8d56a204b1a59a7bda0d Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 10 Mar 2012 03:38:35 +0100 Subject: Stabilize open InputStream's / Closeable's: Decorate w/ try-finally and close within the latter See gluegen commit 24f8694a188b4a5255d4ac4f8b49982bd8ad3228 --- .../com/jogamp/opengl/util/glsl/ShaderCode.java | 69 ++++++++++++---------- 1 file changed, 39 insertions(+), 30 deletions(-) (limited to 'src/jogl/classes/com/jogamp') diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java index 85e288638..eec055ed4 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java @@ -295,39 +295,43 @@ public class ShaderCode { } } - private static int readShaderSource(Class context, URL url, StringBuffer result, int lineno) { + private static int readShaderSource(Class context, URL url, StringBuffer result, int lineno) { try { if(DEBUG_CODE) { System.err.printf("%3d: // %s\n", lineno, url); } - BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); - String line = null; - while ((line = reader.readLine()) != null) { - lineno++; - if(DEBUG_CODE) { - System.err.printf("%3d: %s\n", lineno, line); - } - if (line.startsWith("#include ")) { - String includeFile = line.substring(9).trim(); - URL nextURL = null; - - // Try relative path first - String next = IOUtil.getRelativeOf(url, includeFile); - if(null != next) { - nextURL = IOUtil.getResource(context, next); - } - if (nextURL == null) { - // Try absolute path - nextURL = IOUtil.getResource(context, includeFile); + final BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); + try { + String line = null; + while ((line = reader.readLine()) != null) { + lineno++; + if(DEBUG_CODE) { + System.err.printf("%3d: %s\n", lineno, line); } - if (nextURL == null) { - // Fail - throw new FileNotFoundException("Can't find include file " + includeFile); + if (line.startsWith("#include ")) { + String includeFile = line.substring(9).trim(); + URL nextURL = null; + + // Try relative path first + String next = IOUtil.getRelativeOf(url, includeFile); + if(null != next) { + nextURL = IOUtil.getResource(context, next); + } + if (nextURL == null) { + // Try absolute path + nextURL = IOUtil.getResource(context, includeFile); + } + if (nextURL == null) { + // Fail + throw new FileNotFoundException("Can't find include file " + includeFile); + } + lineno = readShaderSource(context, nextURL, result, lineno); + } else { + result.append(line + "\n"); } - lineno = readShaderSource(context, nextURL, result, lineno); - } else { - result.append(line + "\n"); } + } finally { + IOUtil.close(reader, false); } } catch (IOException e) { throw new RuntimeException(e); @@ -358,12 +362,17 @@ public class ShaderCode { } public static ByteBuffer readShaderBinary(Class context, String path) { + final URL url = IOUtil.getResource(context, path); + if (url == null) { + return null; + } try { - URL url = IOUtil.getResource(context, path); - if (url == null) { - return null; + final BufferedInputStream bis = new BufferedInputStream( url.openStream() ); + try { + return IOUtil.copyStream2ByteBuffer( bis ); + } finally { + IOUtil.close(bis, false); } - return IOUtil.copyStream2ByteBuffer( new BufferedInputStream( url.openStream() ) ); } catch (IOException e) { throw new RuntimeException(e); } -- cgit v1.2.3