Fixed up javadoc in FileUtilities and changed a few methods to use try

with resources to clean them up a bit.
This commit is contained in:
ghidravore 2020-07-08 13:49:14 -04:00
parent d9d78926a6
commit 8d0d1bfb8b

View file

@ -161,9 +161,8 @@ public final class FileUtilities {
} }
byte[] data = new byte[(int) length]; byte[] data = new byte[(int) length];
InputStream fis = sourceFile.getInputStream();
try { try (InputStream fis = sourceFile.getInputStream()) {
if (fis.skip(offset) != offset) { if (fis.skip(offset) != offset) {
throw new IOException("Did not skip to the specified offset!"); throw new IOException("Did not skip to the specified offset!");
} }
@ -174,9 +173,6 @@ public final class FileUtilities {
} }
return data; return data;
} }
finally {
fis.close();
}
} }
public static byte[] getBytes(InputStream is) throws IOException { public static byte[] getBytes(InputStream is) throws IOException {
@ -255,16 +251,12 @@ public final class FileUtilities {
public final static void copyFile(ResourceFile fromFile, File toFile, boolean append, public final static void copyFile(ResourceFile fromFile, File toFile, boolean append,
TaskMonitor monitor) throws IOException { TaskMonitor monitor) throws IOException {
InputStream fin = fromFile.getInputStream(); try (InputStream fin = fromFile.getInputStream()) {
try {
if (monitor != null) { if (monitor != null) {
monitor.initialize((int) fromFile.length()); monitor.initialize((int) fromFile.length());
} }
copyStreamToFile(fin, toFile, append, monitor); copyStreamToFile(fin, toFile, append, monitor);
} }
finally {
fin.close();
}
} }
/** /**
@ -279,26 +271,14 @@ public final class FileUtilities {
public final static void copyFile(ResourceFile fromFile, ResourceFile toFile, public final static void copyFile(ResourceFile fromFile, ResourceFile toFile,
TaskMonitor monitor) throws IOException { TaskMonitor monitor) throws IOException {
InputStream fin = null; try (InputStream fin = fromFile.getInputStream();
OutputStream out = null; OutputStream out = toFile.getOutputStream()) {
try {
fin = fromFile.getInputStream();
out = toFile.getOutputStream();
if (monitor != null) { if (monitor != null) {
monitor.initialize((int) fromFile.length()); monitor.initialize((int) fromFile.length());
} }
copyStreamToStream(fin, out, monitor); copyStreamToStream(fin, out, monitor);
} }
finally {
if (fin != null) {
fin.close();
}
if (out != null) {
out.close();
}
}
} }
/** /**
@ -619,19 +599,12 @@ public final class FileUtilities {
public final static void copyFileToStream(File fromFile, OutputStream out, TaskMonitor monitor) public final static void copyFileToStream(File fromFile, OutputStream out, TaskMonitor monitor)
throws IOException { throws IOException {
InputStream fin = null; try (InputStream fin = new FileInputStream(fromFile)) {
try {
fin = new FileInputStream(fromFile);
if (monitor != null) { if (monitor != null) {
monitor.initialize((int) fromFile.length()); monitor.initialize((int) fromFile.length());
} }
copyStreamToStream(fin, out, monitor); copyStreamToStream(fin, out, monitor);
} }
finally {
if (fin != null) {
fin.close();
}
}
} }
/** /**
@ -729,9 +702,8 @@ public final class FileUtilities {
/** /**
* Returns all of the lines in the given {@link InputStream} without any newline characters. * Returns all of the lines in the given {@link InputStream} without any newline characters.
* <p> * <p>
* <b>The input stream is closed as a side-effect.</b>
* *
* @param is the input stream from which to read, as a side effect, it is closed * @param is the input stream from which to read
* @return a {@link List} of strings representing the text lines of the file * @return a {@link List} of strings representing the text lines of the file
* @throws IOException if there are any issues reading the file * @throws IOException if there are any issues reading the file
*/ */
@ -744,9 +716,7 @@ public final class FileUtilities {
* <p> * <p>
* EOL characters are normalized to simple '\n's. * EOL characters are normalized to simple '\n's.
* <p> * <p>
* <b>The input stream is closed as a side-effect.</b> * @param is the input stream from which to read
* <p>
* @param is the input stream from which to read, as a side effect, it is closed
* @return the content as a String * @return the content as a String
* @throws IOException if there are any issues reading the file * @throws IOException if there are any issues reading the file
*/ */
@ -780,10 +750,8 @@ public final class FileUtilities {
/** /**
* Returns all of the lines in the {@link BufferedReader} without any newline characters. * Returns all of the lines in the {@link BufferedReader} without any newline characters.
* <p>
* The BufferedReader is closed before returning.
* *
* @param in BufferedReader to read lines from, as a side effect, it is closed * @param in BufferedReader to read lines from. The caller is responsible for closing the reader
* @return a {@link List} of strings representing the text lines of the file * @return a {@link List} of strings representing the text lines of the file
* @throws IOException if there are any issues reading the file * @throws IOException if there are any issues reading the file
*/ */