Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions src/main/java/com/gitblit/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,22 @@ public static long convertSizeToLong(String aString, long defaultValue) {
*/
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;
}
Expand All @@ -128,9 +137,9 @@ public static long convertSizeToLong(String aString, long defaultValue) {
*/
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) {
Expand All @@ -139,10 +148,18 @@ public static String readContent(File file, String lineEnding) {
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();
}
Expand All @@ -154,15 +171,24 @@ public static String readContent(File file, String lineEnding) {
* @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();
}
}
}
}

Expand Down Expand Up @@ -219,11 +245,11 @@ public static void copy(File destinationFolder, File... filesOrFolders)
}
} finally {
try {
bufin.close();
if (bufin != null) bufin.close();
} catch (Throwable t) {
}
try {
fos.close();
if (fos != null) fos.close();
} catch (Throwable t) {
}
}
Expand Down