diff options
author | Florian Zschocke <florian.zschocke@cycos.com> | 2013-08-21 15:07:31 +0200 |
---|---|---|
committer | Florian Zschocke <florian.zschocke@cycos.com> | 2013-08-21 18:07:15 +0200 |
commit | fdb31ca346eb322f7cec5182ed0dc6b01259f918 (patch) | |
tree | 86be6671af563062dffcfb09e68b2bdd0a28d6bc /src/main/java | |
parent | 005a65d1862ec1fe940b4ef2b5dd47dca82d3b28 (diff) | |
download | gitblit-fdb31ca346eb322f7cec5182ed0dc6b01259f918.tar.gz gitblit-fdb31ca346eb322f7cec5182ed0dc6b01259f918.zip |
Close streams in finally block.
Close input and output streams in a finally block so that they do not linger around and occupy system resources when an exception occurs.
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/gitblit/utils/FileUtils.java | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/src/main/java/com/gitblit/utils/FileUtils.java b/src/main/java/com/gitblit/utils/FileUtils.java index a21b5128..fb3e09ce 100644 --- a/src/main/java/com/gitblit/utils/FileUtils.java +++ b/src/main/java/com/gitblit/utils/FileUtils.java @@ -108,13 +108,22 @@ public class FileUtils { */
public static byte [] readContent(File file) {
byte [] buffer = new byte[(int) file.length()];
+ BufferedInputStream is = null;
try {
- BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
+ is = new BufferedInputStream(new FileInputStream(file));
is.read(buffer, 0, buffer.length);
- is.close();
} catch (Throwable t) {
System.err.println("Failed to read byte content of " + file.getAbsolutePath());
t.printStackTrace();
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException ioe) {
+ System.err.println("Failed to close file " + file.getAbsolutePath());
+ ioe.printStackTrace();
+ }
+ }
}
return buffer;
}
@@ -128,9 +137,9 @@ public class FileUtils { */
public static String readContent(File file, String lineEnding) {
StringBuilder sb = new StringBuilder();
+ InputStreamReader is = null;
try {
- InputStreamReader is = new InputStreamReader(new FileInputStream(file),
- Charset.forName("UTF-8"));
+ is = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(is);
String line = null;
while ((line = reader.readLine()) != null) {
@@ -139,10 +148,18 @@ public class FileUtils { sb.append(lineEnding);
}
}
- reader.close();
} catch (Throwable t) {
System.err.println("Failed to read content of " + file.getAbsolutePath());
t.printStackTrace();
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException ioe) {
+ System.err.println("Failed to close file " + file.getAbsolutePath());
+ ioe.printStackTrace();
+ }
+ }
}
return sb.toString();
}
@@ -154,15 +171,24 @@ public class FileUtils { * @param content
*/
public static void writeContent(File file, String content) {
+ OutputStreamWriter os = null;
try {
- OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file),
- Charset.forName("UTF-8"));
+ os = new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8"));
BufferedWriter writer = new BufferedWriter(os);
writer.append(content);
- writer.close();
+ writer.flush();
} catch (Throwable t) {
System.err.println("Failed to write content of " + file.getAbsolutePath());
t.printStackTrace();
+ } finally {
+ if (os != null) {
+ try {
+ os.close();
+ } catch (IOException ioe) {
+ System.err.println("Failed to close file " + file.getAbsolutePath());
+ ioe.printStackTrace();
+ }
+ }
}
}
@@ -219,11 +245,11 @@ public class FileUtils { }
} finally {
try {
- bufin.close();
+ if (bufin != null) bufin.close();
} catch (Throwable t) {
}
try {
- fos.close();
+ if (fos != null) fos.close();
} catch (Throwable t) {
}
}
|