diff --git a/src/zutil/io/IOUtil.java b/src/zutil/io/IOUtil.java index 24a29b0..a8b56bc 100755 --- a/src/zutil/io/IOUtil.java +++ b/src/zutil/io/IOUtil.java @@ -34,44 +34,75 @@ import java.io.*; */ public class IOUtil { + /** + * Reads and returns all the content of a stream. + * The InputStream will not be closed + * + * @param stream + * @return a byte array with the stream contents + */ + public static byte[] readContent(InputStream stream) throws IOException{ + return readContent(stream, false); + } + /** * Reads and returns all the content of a stream. - * This function will close the inout stream at the end. - * + * * @param stream - * @return the stream contents + * @param close true if the stream should be closed at the end + * @return a byte array with the stream contents */ - public static byte[] readContent(InputStream stream) throws IOException{ + public static byte[] readContent(InputStream stream, boolean close) throws IOException{ DynamicByteArrayStream dyn_buff = new DynamicByteArrayStream(); byte[] buff = new byte[8192]; int len = 0; while((len = stream.read(buff)) != -1){ dyn_buff.append(buff, 0, len); } - stream.close(); + if (close) stream.close(); return dyn_buff.getBytes(); } /** * Reads and returns all the content of a stream as a String. - * This function will close the input stream at the end. + * The InputStream will not be closed * * @param stream * @return a String with the content of the stream */ public static String readContentAsString(InputStream stream) throws IOException{ - return readContentAsString(new InputStreamReader(stream)); + return readContentAsString(stream, false); + } + /** + * Reads and returns all the content of a stream as a String. + * + * @param stream + * @param close true if the stream should be closed at the end + * @return a String with the content of the stream + */ + public static String readContentAsString(InputStream stream, boolean close) throws IOException{ + return readContentAsString(new InputStreamReader(stream), close); } /** * Reads and returns all the content of a stream as a String. - * This function will close the input stream at the end. + * The Reader will not be closed * * @param reader * @return a String with the content of the stream */ public static String readContentAsString(Reader reader) throws IOException{ + return readContentAsString(reader, false); + } + /** + * Reads and returns all the content of a stream as a String. + * + * @param reader + * @param close true if the stream should be closed at the end + * @return a String with the content of the stream + */ + public static String readContentAsString(Reader reader, boolean close) throws IOException{ StringBuilder str = new StringBuilder(); BufferedReader in = null; if(reader instanceof BufferedReader) @@ -84,8 +115,8 @@ public class IOUtil { str.append(line).append("\n"); } str.delete(str.length()-1, str.length()); // remove last new line - in.close(); + if (close) reader.close(); return str.toString(); } diff --git a/src/zutil/io/file/FileUtil.java b/src/zutil/io/file/FileUtil.java index 1debbe5..7e9a6e4 100755 --- a/src/zutil/io/file/FileUtil.java +++ b/src/zutil/io/file/FileUtil.java @@ -162,10 +162,7 @@ public class FileUtil { return new String(getByteContent(file)); } public static byte[] getByteContent(File file) throws IOException { - InputStream in = new FileInputStream(file); - byte[] data = IOUtil.readContent(in); - in.close(); - return data; + return IOUtil.readContent(new FileInputStream(file), true); } /** @@ -175,9 +172,7 @@ public class FileUtil { * @return the file content */ public static String getContent(URL url) throws IOException{ - InputStream in = url.openStream(); - String data = new String(IOUtil.readContent(in)); - in.close(); + String data = new String(IOUtil.readContent(url.openStream(), true)); return data; } diff --git a/src/zutil/net/ws/soap/SOAPClientInvocationHandler.java b/src/zutil/net/ws/soap/SOAPClientInvocationHandler.java index ab69f25..85d7417 100755 --- a/src/zutil/net/ws/soap/SOAPClientInvocationHandler.java +++ b/src/zutil/net/ws/soap/SOAPClientInvocationHandler.java @@ -78,7 +78,8 @@ public class SOAPClientInvocationHandler implements InvocationHandler { request.setURL(url); request.setData(reqXml); HttpHeaderParser response = request.send(); - String rspXml = IOUtil.readContentAsString( request.getResponseReader()); + String rspXml = IOUtil.readContentAsString(request.getResponseInputStream()); + request.close(); // DEBUG if( logger.isLoggable(Level.FINEST) ){ diff --git a/src/zutil/plugin/PluginManager.java b/src/zutil/plugin/PluginManager.java index 763a705..efea874 100755 --- a/src/zutil/plugin/PluginManager.java +++ b/src/zutil/plugin/PluginManager.java @@ -70,7 +70,7 @@ public class PluginManager implements Iterable{ log.fine("Searching for plugins..."); for(FileSearcher.FileSearchItem file : search){ try { - DataNode node = JSONParser.read(IOUtil.readContentAsString(file.getInputStream())); + DataNode node = JSONParser.read(IOUtil.readContentAsString(file.getInputStream(), true)); log.fine("Found plugin: "+file.getPath()); PluginData plugin = new PluginData(node);