From 3630d06bc4858ff82ea8cbe7b699fda211c0ec47 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Tue, 13 Sep 2011 17:55:21 +0200 Subject: extracted readStream utility method to CLUtil --- src/com/jogamp/opencl/CLContext.java | 11 ++--------- src/com/jogamp/opencl/util/CLUtil.java | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/com/jogamp/opencl/CLContext.java b/src/com/jogamp/opencl/CLContext.java index 45fdee64..771b594a 100644 --- a/src/com/jogamp/opencl/CLContext.java +++ b/src/com/jogamp/opencl/CLContext.java @@ -28,6 +28,7 @@ package com.jogamp.opencl; +import com.jogamp.opencl.util.CLUtil; import com.jogamp.opencl.llb.CL; import com.jogamp.common.nio.Buffers; import com.jogamp.opencl.CLDevice.Type; @@ -38,7 +39,6 @@ import com.jogamp.opencl.llb.CLContextBinding; import com.jogamp.opencl.llb.impl.CLImageFormatImpl; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.DoubleBuffer; @@ -284,14 +284,7 @@ public class CLContext extends CLObjectResource { char[] buffer = new char[1024]; for (InputStream source : sources) { - InputStreamReader reader = new InputStreamReader(source); - try { - int len = 0; - while ((len = reader.read(buffer)) != -1) - sb.append(buffer, 0, len); - } finally { - reader.close(); - } + CLUtil.readStream(source, sb, buffer); } return createProgram(sb); diff --git a/src/com/jogamp/opencl/util/CLUtil.java b/src/com/jogamp/opencl/util/CLUtil.java index 98a6cd7e..ff04f745 100644 --- a/src/com/jogamp/opencl/util/CLUtil.java +++ b/src/com/jogamp/opencl/util/CLUtil.java @@ -33,6 +33,9 @@ import com.jogamp.opencl.llb.CL; import com.jogamp.opencl.CLDevice; import com.jogamp.opencl.CLPlatform; import com.jogamp.opencl.CLProperty; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -81,6 +84,30 @@ public class CLUtil { return b ? CL.CL_TRUE : CL.CL_FALSE; } + /** + * Reads chars from input stream and puts them into the supplied StringBuilder. + * The stream is closed after successful or unsuccessful read. + */ + public static StringBuilder readStream(InputStream source, StringBuilder dest) throws IOException { + return readStream(source, dest, new char[1024]); + } + + /** + * Reads chars from input stream and puts them into the supplied StringBuilder using the supplied buffer. + * The stream is closed after successful or unsuccessful read. + */ + public static StringBuilder readStream(InputStream source, StringBuilder dest, char[] buffer) throws IOException { + InputStreamReader reader = new InputStreamReader(source); + try { + int len = 0; + while ((len = reader.read(buffer)) != -1) + dest.append(buffer, 0, len); + } finally { + reader.close(); + } + return dest; + } + /** * Reads all platform properties and returns them as key-value map. */ -- cgit v1.2.3