Skip to content

Commit

Permalink
close streams after use
Browse files Browse the repository at this point in the history
  • Loading branch information
darkv committed Sep 28, 2012
1 parent 338242c commit bca5d72
Showing 1 changed file with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -351,7 +352,7 @@ public static File writeInputStreamToTempFile(InputStream stream, String prefix,
* @throws IOException if things go wrong
*/
public static void writeInputStreamToFile(InputStream stream, File file) throws IOException {
FileOutputStream out;
FileOutputStream out = null;
try {
if (file == null) throw new IllegalArgumentException("Attempting to write to a null file!");
File parent = file.getParentFile();
Expand All @@ -360,22 +361,26 @@ public static void writeInputStreamToFile(InputStream stream, File file) throws
throw new RuntimeException("Cannot create parent directory for file");
}
out = new FileOutputStream(file);
}
catch (IOException e) {
stream.close();
throw e;
}
catch (RuntimeException e) {
ERXFileUtilities.writeInputStreamToOutputStream(stream, true, out, true);
} finally {
stream.close();
throw e;
if (out != null) {
out.close();
}
}
ERXFileUtilities.writeInputStreamToOutputStream(stream, true, out, true);
}

public static void writeInputStreamToGZippedFile(InputStream stream, File file) throws IOException {
if (file == null) throw new IllegalArgumentException("Attempting to write to a null file!");
FileOutputStream out = new FileOutputStream(file);
ERXFileUtilities.writeInputStreamToOutputStream(stream, false, new GZIPOutputStream(out), true);
GZIPOutputStream out = null;
try {
out = new GZIPOutputStream(new FileOutputStream(file));
ERXFileUtilities.writeInputStreamToOutputStream(stream, false, out, true);
} finally {
if (out != null) {
out.close();
}
}
}

/**
Expand Down Expand Up @@ -697,7 +702,7 @@ public static URL URLFromPath(String fileName) {
if(fileName != null) {
try {
url = new URL("file://" + fileName);
} catch (java.net.MalformedURLException ex) {
} catch(MalformedURLException ex) {
throw new NSForwardException(ex);
}
}
Expand Down Expand Up @@ -1296,12 +1301,18 @@ public static File unzipFile(File f, File destination) throws IOException {
log.debug("created directory "+d.getAbsolutePath());
}
} else {
InputStream is = zipFile.getInputStream(ze);
writeInputStreamToFile(is, new File(absolutePath, name));
if (log.isDebugEnabled()) {
log.debug("unzipped file "+ze.getName()+" into "+(absolutePath + name));
InputStream is = null;
try {
is = zipFile.getInputStream(ze);
writeInputStreamToFile(is, new File(absolutePath, name));
if (log.isDebugEnabled()) {
log.debug("unzipped file "+ze.getName()+" into "+(absolutePath + name));
}
} finally {
if (is != null) {
is.close();
}
}
is.close();
}
}

Expand Down Expand Up @@ -1375,9 +1386,10 @@ public static File zipFile(File f, boolean absolutePaths, boolean deleteOriginal
}
origin.close();
}
zout.close();
} catch(Exception e) {
e.printStackTrace();
} finally {
zout.close();
}

if (deleteOriginal) {
Expand Down

0 comments on commit bca5d72

Please sign in to comment.