Changed stream read methods to default not close the streams

This commit is contained in:
Ziver Koc 2016-07-14 17:50:17 +02:00
parent 95586441ee
commit 8ad285cd61
4 changed files with 45 additions and 18 deletions

View file

@ -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();
}

View file

@ -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;
}

View file

@ -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) ){

View file

@ -70,7 +70,7 @@ public class PluginManager<T> implements Iterable<PluginData>{
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);