Skip to content

Commit

Permalink
close some more streams after use
Browse files Browse the repository at this point in the history
  • Loading branch information
darkv committed Oct 4, 2012
1 parent 53a0701 commit 7a519d8
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOExceptionWithCause;

import com.webobjects.appserver.WOComponent;
import com.webobjects.appserver.WOContext;
import com.webobjects.appserver.WOMultipartIterator;
Expand Down Expand Up @@ -235,13 +237,18 @@ public void takeValuesFromRequest(WORequest request, WOContext context) {
tempFile = ERXFileUtilities.writeInputStreamToTempFile(anInputStream, context.session().sessionID(), ".tmp");
} catch (IOException e) {
throw new RuntimeException("Couldn't write input stream to temp file: " + e);
} finally {
try { anInputStream.close(); } catch (IOException e) {}
}
} else {
OutputStream anOutputStream = (OutputStream) valueForBinding(Bindings.outputStream);
try {
ERXFileUtilities.writeInputStreamToOutputStream(anInputStream, anOutputStream);
} catch (IOException e) {
throw new RuntimeException("Couldn't write input stream to output stream: " + e);
} finally {
try { anOutputStream.close(); } catch (IOException e) {}
try { anInputStream.close(); } catch (IOException e) {}
}
}

Expand All @@ -267,6 +274,8 @@ public void takeValuesFromRequest(WORequest request, WOContext context) {
}
catch (IOException e) {
throw new RuntimeException("Error skipping empty file upload: " + e);
} finally {
try { anInputStream.close(); } catch (IOException e) {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ public void takeValuesFromRequest(WORequest request, WOContext context) {
} catch (IOException e) {
exception = new RuntimeException("Couldn't write input stream to output stream: " + e);
throw exception;
} finally {
try { anOutputStream.close(); } catch (IOException e) {}
try { anInputStream.close(); } catch (IOException e) {}
}
} else {
if (hasBinding(Bindings.finalFilePath)) {
Expand All @@ -228,6 +231,8 @@ public void takeValuesFromRequest(WORequest request, WOContext context) {
} catch (IOException e) {
exception = new RuntimeException("Couldn't write input stream to temp file: " + e);
throw exception;
} finally {
try { anInputStream.close(); } catch (IOException e) {}
}
}

Expand Down Expand Up @@ -256,7 +261,9 @@ public void takeValuesFromRequest(WORequest request, WOContext context) {
catch (IOException e) {
exception = new RuntimeException("Error skipping empty file upload: " + e);
throw exception;
}
} finally {
try { anInputStream.close(); } catch (IOException e) {}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,18 @@ public void takeValuesFromRequest(WORequest request, WOContext context) {
tempFile = ERXFileUtilities.writeInputStreamToTempFile(anInputStream, context.session().sessionID(), ".tmp");
} catch (IOException e) {
throw new RuntimeException("Couldn't write input stream to temp file: " + e);
} finally {
try { anInputStream.close(); } catch (IOException e) {}
}
} else {
OutputStream anOutputStream = (OutputStream) valueForBinding(Bindings.outputStream);
try {
ERXFileUtilities.writeInputStreamToOutputStream(anInputStream, anOutputStream);
} catch (IOException e) {
throw new RuntimeException("Couldn't write input stream to output stream: " + e);
} finally {
try { anOutputStream.close(); } catch (IOException e) {}
try { anInputStream.close(); } catch (IOException e2) {}
}
}

Expand All @@ -268,6 +273,8 @@ public void takeValuesFromRequest(WORequest request, WOContext context) {
}
catch (IOException e) {
throw new RuntimeException("Error skipping empty file upload: " + e);
} finally {
try { anInputStream.close(); } catch (IOException e2) {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ public long writeObjectToFile (File localFile) throws FileNotFoundException, Htt
if (ret == -1)
{
out.write(data, 0, data.length);
out.flush();
out.close();
in.close ();
return data.length;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ private static synchronized void loadPropertiesFromClasspath() throws IOExceptio
{
throw new FileNotFoundException("Property file '" + file + "' not found in the classpath.");
}
loadProperties(io);
try {
loadProperties(io);
} finally {
io.close();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,9 @@ private Properties readProperties(NSBundle bundle, String name) {
return bundle.properties();
}

InputStream inputStream = null;
try {
InputStream inputStream = bundle.inputStreamForResourcePath(name);
inputStream = bundle.inputStreamForResourcePath(name);
if(inputStream == null) {
return null;
}
Expand All @@ -438,6 +439,10 @@ private Properties readProperties(NSBundle bundle, String name) {
}
catch (IOException exception) {
return null;
} finally {
if (inputStream != null) {
try { inputStream.close(); } catch (IOException e) {}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,17 @@ public static String stringFromResource(String name, String extension, NSBundle
}
path = bundle.resourcePathForLocalizedResourceNamed(name + (extension == null || extension.length() == 0 ? "" : "." + extension), null);
if(path != null) {
InputStream stream = null;
try {
InputStream stream = bundle.inputStreamForResourcePath(path);
stream = bundle.inputStreamForResourcePath(path);
byte bytes[] = ERXFileUtilities.bytesFromInputStream(stream);
return new String(bytes);
} catch (IOException e) {
log.warn("IOException when stringFromResource(" + name + "." + extension + " in bundle " + bundle.name());
} finally {
if (stream != null) {
try { stream.close(); } catch (IOException e) {}
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,10 @@ protected void copyEntity(EOEntity entity) throws SQLException {
tempFile.delete();

continue;
} finally {
try { bis.close(); } catch (IOException e) {}
}
FileInputStream fis;
FileInputStream fis = null;
try {
fis = new FileInputStream(tempFile);
}
Expand All @@ -332,6 +334,10 @@ protected void copyEntity(EOEntity entity) throws SQLException {
tempFile.delete();

continue;
} finally {
if (fis != null) {
try { fis.close(); } catch (IOException e) {}
}
}
upps.setBinaryStream(i + 1, fis, (int) tempFile.length());
tempfilesToDelete.addObject(tempFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static String _stringFromFileSafely(String path) {
} catch (java.io.IOException e) {
return null;
} finally {
if (f != null) {
if (fis != null) {
try {
fis.close();
} catch (java.io.IOException e) {
Expand All @@ -173,15 +173,12 @@ private static String _stringFromFileSafely(String path) {
NSLog.debug.appendln(e);
}
}

f = null;
}
}

if (bytesRead == 0)
return null;
else
return new String(data);
return new String(data);
}

/**
Expand Down Expand Up @@ -210,7 +207,7 @@ protected static String _stringFromFile(String path) {
} catch (IOException e) {
throw NSForwardException._runtimeExceptionForThrowable(e);
} finally {
if (f != null) {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
Expand All @@ -219,8 +216,6 @@ protected static String _stringFromFile(String path) {
NSLog.debug.appendln(e);
}
}

f = null;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,9 @@ public Object get( String key, Integer hashCode, boolean asString ) {
log.error( "++++ ClassNotFoundException thrown while trying to deserialize for key: " + key, e );
throw new NestedIOException( "+++ failed while trying to deserialize for key: " + key, e );
}
finally {
ois.close();
}
}
}
else if ( END.equals( line ) ) {
Expand Down Expand Up @@ -1726,6 +1729,9 @@ private void loadMulti( LineInputStream input, Map<String,Object> hm, boolean as
log.error( "++++ ClassNotFoundException thrown while trying to deserialize for key: " + key, e );
throw new NestedIOException( "+++ failed while trying to deserialize for key: " + key, e );
}
finally {
ois.close();
}
}

// store the object into the cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public NSDictionary jdbcInfo() {
}
catch (IOException e) {
throw new RuntimeException("Failed to load 'JDBCInfo.plist' from this plugin jar.", e);
} finally {
try { jdbcInfoStream.close(); } catch (IOException e) {}
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ public NSDictionary jdbcInfo() {
}
catch (IOException e) {
throw new RuntimeException("Failed to load 'JDBCInfo.plist' from this plugin jar: " + e, e);
} finally {
try { jdbcInfoStream.close(); } catch (IOException e) {}
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public NSDictionary<String, Object> jdbcInfo() {
}
catch (IOException e) {
throw new RuntimeException("Failed to load 'FrontBaseJDBCInfo.plist' from this plugin jar.", e);
} finally {
try { jdbcInfoStream.close(); } catch (IOException e) {}
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@ public NSDictionary jdbcInfo() {
catch (IOException e) {
throw new RuntimeException("Failed to load 'H2JDBCInfo.plist' from this plugin jar: " + e, e);
}
finally {
try { jdbcInfoStream.close(); } catch (IOException e) {}
}
}
else {
jdbcInfo = super.jdbcInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,8 @@ public NSDictionary<String, Object> jdbcInfo() {
jdbcInfo = (NSDictionary<String, Object>) NSPropertyListSerialization.propertyListFromData(new NSData(jdbcInfoStream, 2048), "US-ASCII");
} catch (IOException e) {
throw new RuntimeException("Failed to load 'JDBCInfo.plist' from this plugin jar.", e);
} finally {
try { jdbcInfoStream.close(); } catch (IOException e) {}
}

} else {
Expand Down

0 comments on commit 7a519d8

Please sign in to comment.