Ensure resource is closed when reading a full file InputStream

pull/26/head
luc 9 years ago
parent 6291a57300
commit 5bbb2e1730

@ -137,7 +137,6 @@ public class FileLoader {
// load the resource
InputStream is = url.getInputStream(ClientIdentification.yacyInternetCrawlerAgent, null, null);
byte[] b = FileUtils.read(is);
is.close();
// create response with loaded content
final CrawlProfile profile = this.sb.crawler.get(ASCII.getBytes(request.profileHandle()));

@ -155,7 +155,6 @@ public class SMBLoader {
// load the resource
InputStream is = url.getInputStream(ClientIdentification.yacyInternetCrawlerAgent, null, null);
byte[] b = FileUtils.read(is);
is.close();
// create response with loaded content
final CrawlProfile profile = this.sb.crawler.get(request.profileHandle().getBytes());

@ -268,8 +268,24 @@ public final class FileUtils {
copy(new ByteArrayInputStream(source), dest);
}
/**
* Read fully source stream and close it.
* @param source must not be null
* @return source content as a byte array.
* @throws IOException when a read/write error occured
*/
public static byte[] read(final InputStream source) throws IOException {
return read(source, -1);
byte[] content;
try {
content = read(source, -1);
} finally {
/* source input stream must be closed here in all cases */
try {
source.close();
} catch(IOException ignoredException) {
}
}
return content;
}
public static byte[] read(final InputStream source, final int count) throws IOException {

Loading…
Cancel
Save