Permalink
Browse files

Ensure file input streams proper closing in both success and failures

Also add when possible a warning level log message on input stream
closing error instead of failing silently. This could help understanding
some IO exceptions such as "too many files open".
  • Loading branch information...
luccioman committed Jun 3, 2017
1 parent d98c048 commit a04feac064aee0cc74ecfb4e83b2a70add88905a
@@ -268,6 +268,14 @@ public static void main(final String[] args) {
ConcurrentLog.logException(e);
} catch (final IOException e) {
ConcurrentLog.logException(e);
} finally {
if(fis != null) {
try {
fis.close();
} catch(IOException ioe) {
ConcurrentLog.logException(ioe);
}
}
}
try {
@@ -301,9 +301,11 @@ public static void main(final String[] args) {
final File image = new File(args[0]);
final genericImageParser parser = new genericImageParser();
AnchorURL uri;
FileInputStream inStream = null;
try {
uri = new AnchorURL("http://localhost/" + image.getName());
final Document[] document = parser.parse(uri, "image/" + MultiProtocolURL.getFileExtension(uri.getFileName()), StandardCharsets.UTF_8.name(), new VocabularyScraper(), 0, new FileInputStream(image));
inStream = new FileInputStream(image);
final Document[] document = parser.parse(uri, "image/" + MultiProtocolURL.getFileExtension(uri.getFileName()), StandardCharsets.UTF_8.name(), new VocabularyScraper(), 0, inStream);
System.out.println(document[0].toString());
} catch (final MalformedURLException e) {
e.printStackTrace();
@@ -313,6 +315,15 @@ public static void main(final String[] args) {
e.printStackTrace();
} catch (final InterruptedException e) {
e.printStackTrace();
} finally {
if(inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ConcurrentLog.shutdown();
}
}
@@ -104,29 +104,41 @@ public BufferedImage getImage(final int index) {
}
public static void main(final String[] args) {
// read a ICO and write it as png
System.setProperty("java.awt.headless", "true");
final File in = new File(args[0]);
final File out = new File(args[1]);
try {
// read a ICO and write it as png
System.setProperty("java.awt.headless", "true");
final File in = new File(args[0]);
final File out = new File(args[1]);
final byte[] file = new byte[(int) in.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(in);
fis.read(file);
} catch (final FileNotFoundException e) {
ConcurrentLog.logException(e);
} catch (final IOException e) {
ConcurrentLog.logException(e);
}
final byte[] file = new byte[(int) in.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(in);
fis.read(file);
} catch (final FileNotFoundException e) {
ConcurrentLog.logException(e);
} catch (final IOException e) {
ConcurrentLog.logException(e);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
ConcurrentLog.logException(e);
}
}
}
final icoParser parser = new icoParser(file);
final icoParser parser = new icoParser(file);
try {
ImageIO.write(parser.getImage(0), "PNG", out);
} catch (final IOException e) {
ConcurrentLog.logException(e);
}
try {
ImageIO.write(parser.getImage(0), "PNG", out);
} catch (final IOException e) {
ConcurrentLog.logException(e);
}
} finally {
ConcurrentLog.shutdown();
}
}
}
@@ -174,7 +174,11 @@ public static void load(File propFile) {
log.info("error: file dispatcher.properties cannot be readed. Exit");
System.exit(-1);
} finally {
if (fis != null) try { fis.close(); } catch (IOException ex) { }
if (fis != null) try {
fis.close();
} catch (IOException ex) {
log.warn("Could not close input stream on file " + propFile);
}
}
}
@@ -62,23 +62,36 @@ public Gap(final File file) throws IOException {
super();
// read the index dump and fill the index
DataInputStream is;
FileInputStream fis = null;
try {
is = new DataInputStream(new BufferedInputStream(new FileInputStream(file), (Integer.SIZE + Long.SIZE) * 1024)); // equals 16*1024*recordsize
fis = new FileInputStream(file);
is = new DataInputStream(new BufferedInputStream(fis, (Integer.SIZE + Long.SIZE) * 1024)); // equals 16*1024*recordsize
} catch (final OutOfMemoryError e) {
is = new DataInputStream(new FileInputStream(file));
if(fis != null) {
/* Reuse if possible the already created FileInputStream */
is = new DataInputStream(fis);
} else {
is = new DataInputStream(new FileInputStream(file));
}
}
long p;
int l;
while (true) {
try {
p = is.readLong();
l = is.readInt();
this.put(Long.valueOf(p), Integer.valueOf(l));
} catch (final IOException e) {
break;
}
try {
long p;
int l;
while (true) {
try {
p = is.readLong();
l = is.readInt();
this.put(Long.valueOf(p), Integer.valueOf(l));
} catch (final IOException e) {
break;
}
}
} finally {
if(is != null) {
is.close();
}
}
is.close();
is = null;
}
@@ -738,10 +738,17 @@ public long length() {
public entries(final File blobFile, final int keylen) throws IOException {
if (!(blobFile.exists())) throw new IOException("file " + blobFile + " does not exist");
FileInputStream fis = null;
try {
this.is = new DataInputStream(new BufferedInputStream(new FileInputStream(blobFile), 256 * 1024));
fis = new FileInputStream(blobFile);
this.is = new DataInputStream(new BufferedInputStream(fis, 256 * 1024));
} catch (final OutOfMemoryError e) {
this.is = new DataInputStream(new FileInputStream(blobFile));
if(fis != null) {
/* Reuse if possible the already created FileInputStream */
this.is = new DataInputStream(fis);
} else {
this.is = new DataInputStream(new FileInputStream(blobFile));
}
}
this.keylen = keylen;
this.blobFile = blobFile;
@@ -88,22 +88,32 @@ public RowHandleMap(final int keylength, final ByteOrder objectOrder, final int
this(keylength, objectOrder, idxbytes, (int) (file.length() / (keylength + idxbytes)), file.getAbsolutePath());
// read the index dump and fill the index
InputStream is;
FileInputStream fis = null;
try {
is = new BufferedInputStream(new FileInputStream(file), 1024 * 1024);
fis = new FileInputStream(file);
is = new BufferedInputStream(fis, 1024 * 1024);
} catch (final OutOfMemoryError e) {
is = new FileInputStream(file);
if(fis != null) {
/* Reuse if possible the already created FileInputStream */
is = fis;
} else {
is = new FileInputStream(file);
}
}
if (file.getName().endsWith(".gz")) is = new GZIPInputStream(is);
final byte[] a = new byte[keylength + idxbytes];
int c;
Row.Entry entry;
while (true) {
c = is.read(a);
if (c <= 0) break;
entry = this.rowdef.newEntry(a); // may be null if a is not well-formed
if (entry != null) this.index.addUnique(entry);
try {
if (file.getName().endsWith(".gz")) is = new GZIPInputStream(is);
final byte[] a = new byte[keylength + idxbytes];
int c;
Row.Entry entry;
while (true) {
c = is.read(a);
if (c <= 0) break;
entry = this.rowdef.newEntry(a); // may be null if a is not well-formed
if (entry != null) this.index.addUnique(entry);
}
} finally {
is.close();
}
is.close();
is = null;
assert this.index.size() == file.length() / (keylength + idxbytes);
optimize();
@@ -107,15 +107,24 @@ public void optimize() {
public RowHandleSet(final int keylength, final ByteOrder objectOrder, final File file) throws IOException, SpaceExceededException {
this(keylength, objectOrder, (int) (file.length() / (keylength + 8)));
// read the index dump and fill the index
final InputStream is = new BufferedInputStream(new FileInputStream(file), 1024 * 1024);
final byte[] a = new byte[keylength];
int c;
while (true) {
c = is.read(a);
if (c <= 0) break;
this.index.addUnique(this.rowdef.newEntry(a));
FileInputStream fis = new FileInputStream(file);
InputStream is = null;
try {
is = new BufferedInputStream(fis, 1024 * 1024);
final byte[] a = new byte[keylength];
int c;
while (true) {
c = is.read(a);
if (c <= 0) break;
this.index.addUnique(this.rowdef.newEntry(a));
}
} finally {
if(is != null) {
is.close();
} else if(fis != null) {
fis.close();
}
}
is.close();
assert this.index.size() == file.length() / keylength;
}
@@ -388,13 +397,15 @@ public static void main(String[] args) {
// read from file
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
RowHandleSet s1 = (RowHandleSet) in.readObject();
in.close();
for (byte[] b: s1) {
System.out.println(UTF8.String(b));
try {
RowHandleSet s1 = (RowHandleSet) in.readObject();
for (byte[] b: s1) {
System.out.println(UTF8.String(b));
}
s1.close();
} finally {
in.close();
}
s1.close();
} catch(IOException e) {
e.printStackTrace();
} catch (final ClassNotFoundException e) {
@@ -292,6 +292,7 @@ public static void copyRange(final File source, final OutputStream dest, final i
try {
fis.close();
} catch (final Exception e ) {
ConcurrentLog.warn("FileUtils", "Could not close input stream on file " + source);
}
}
}
@@ -319,6 +320,7 @@ public static void copy(final File source, final OutputStream dest) throws IOExc
try {
fis.close();
} catch (final Exception e ) {
ConcurrentLog.warn("FileUtils", "Could not close input stream on file " + source);
}
}
}
@@ -411,6 +413,7 @@ public static void copy(final byte[] source, final File dest) throws IOException
try {
fis.close();
} catch (final Exception e ) {
ConcurrentLog.warn("FileUtils", "Could not close input stream on file " + source);
}
}
fis = null;
@@ -478,13 +481,13 @@ public static void copy(final byte[] source, final File dest) throws IOException
set.add(line.trim().toLowerCase());
}
}
br.close();
} catch (final IOException e ) {
} finally {
if ( br != null ) {
try {
br.close();
} catch (final Exception e ) {
ConcurrentLog.warn("FileUtils", "Could not close input stream on file " + file);
}
}
}
@@ -622,14 +625,14 @@ public static void saveMapB(final File file, final Map<String, byte[]> props, fi
while ( (line = br.readLine()) != null ) {
if (!line.isEmpty()) list.add(line);
}
br.close();
} catch (final IOException e ) {
// list is empty
} finally {
if ( br != null ) {
try {
br.close();
} catch (final Exception e ) {
ConcurrentLog.warn("FileUtils", "Could not close input stream on file " + listFile);
}
}
}
@@ -692,13 +695,13 @@ public static String getListString(final File listFile, final boolean withcommen
temp.append(line).append(CR).append(LF);
}
}
br.close();
} catch (final IOException e ) {
} finally {
if ( br != null ) {
try {
br.close();
} catch (final Exception e ) {
ConcurrentLog.warn("FileUtils", "Could not close input stream on file " + listFile);
}
}
}
@@ -529,7 +529,9 @@ private static boolean anymatchByEnumeration(final HandleSet set1, final HandleS
}
} catch (final IOException e) {
} finally {
if (br != null) try{br.close();}catch(final Exception e){}
if (br != null) try{br.close();}catch(final Exception e){
ConcurrentLog.warn("SetTools", "Could not close input stream on file " + file);
}
}
return list;
}
@@ -57,8 +57,11 @@ public XMLTables(final File file) throws IOException {
this.timestamp = System.currentTimeMillis();
if (propFile.exists()) {
final XMLDecoder xmldec = new XMLDecoder(new FileInputStream(propFile));
tables = (HashMap<String, Map<String, String>>) xmldec.readObject();
xmldec.close();
try {
tables = (HashMap<String, Map<String, String>>) xmldec.readObject();
} finally {
xmldec.close();
}
} else {
tables = new HashMap<String, Map<String, String>>();
}
Oops, something went wrong.

0 comments on commit a04feac

Please sign in to comment.