Browse Source

Merge pull request #115 from fzs/fu-close-finally

Close streams in finally block.
tags/v1.3.2
James Moger 10 years ago
parent
commit
2806d9849b
1 changed files with 36 additions and 10 deletions
  1. 36
    10
      src/main/java/com/gitblit/utils/FileUtils.java

+ 36
- 10
src/main/java/com/gitblit/utils/FileUtils.java View File

*/ */
public static byte [] readContent(File file) { public static byte [] readContent(File file) {
byte [] buffer = new byte[(int) file.length()]; byte [] buffer = new byte[(int) file.length()];
BufferedInputStream is = null;
try { try {
BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
is = new BufferedInputStream(new FileInputStream(file));
is.read(buffer, 0, buffer.length); is.read(buffer, 0, buffer.length);
is.close();
} catch (Throwable t) { } catch (Throwable t) {
System.err.println("Failed to read byte content of " + file.getAbsolutePath()); System.err.println("Failed to read byte content of " + file.getAbsolutePath());
t.printStackTrace(); 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; return buffer;
} }
*/ */
public static String readContent(File file, String lineEnding) { public static String readContent(File file, String lineEnding) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
InputStreamReader is = null;
try { 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); BufferedReader reader = new BufferedReader(is);
String line = null; String line = null;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
sb.append(lineEnding); sb.append(lineEnding);
} }
} }
reader.close();
} catch (Throwable t) { } catch (Throwable t) {
System.err.println("Failed to read content of " + file.getAbsolutePath()); System.err.println("Failed to read content of " + file.getAbsolutePath());
t.printStackTrace(); 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(); return sb.toString();
} }
* @param content * @param content
*/ */
public static void writeContent(File file, String content) { public static void writeContent(File file, String content) {
OutputStreamWriter os = null;
try { 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); BufferedWriter writer = new BufferedWriter(os);
writer.append(content); writer.append(content);
writer.close();
writer.flush();
} catch (Throwable t) { } catch (Throwable t) {
System.err.println("Failed to write content of " + file.getAbsolutePath()); System.err.println("Failed to write content of " + file.getAbsolutePath());
t.printStackTrace(); t.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException ioe) {
System.err.println("Failed to close file " + file.getAbsolutePath());
ioe.printStackTrace();
}
}
} }
} }
} }
} finally { } finally {
try { try {
bufin.close();
if (bufin != null) bufin.close();
} catch (Throwable t) { } catch (Throwable t) {
} }
try { try {
fos.close();
if (fos != null) fos.close();
} catch (Throwable t) { } catch (Throwable t) {
} }
} }

Loading…
Cancel
Save