Fixing RandomAccessFile usage so that the file will be properly closed
even if there is an exception
This commit is contained in:
Christopher L. Shannon (cshannon) 2016-09-29 06:16:40 -04:00
parent 0dd806f43f
commit 351faf2699
2 changed files with 6 additions and 8 deletions

View File

@ -374,10 +374,8 @@ public class Journal {
}
private void doPreallocationKernelCopy(RecoverableRandomAccessFile file) {
try {
RandomAccessFile templateRaf = new RandomAccessFile(osKernelCopyTemplateFile, "rw");
try (RandomAccessFile templateRaf = new RandomAccessFile(osKernelCopyTemplateFile, "rw");){
templateRaf.getChannel().transferTo(0, getMaxFileLength(), file.getChannel());
templateRaf.close();
} catch (ClosedByInterruptException ignored) {
LOG.trace("Could not preallocate journal file with kernel copy", ignored);
} catch (FileNotFoundException e) {

View File

@ -300,11 +300,11 @@ public class DiskBenchmark {
if (tmpFile.exists()) {
tmpFile.delete();
}
RandomAccessFile templateFile = new RandomAccessFile(tmpFile, "rw");
templateFile.setLength(size);
templateFile.getChannel().force(true);
templateFile.getChannel().transferTo(0, size, raf.getChannel());
templateFile.close();
try (RandomAccessFile templateFile = new RandomAccessFile(tmpFile, "rw");) {
templateFile.setLength(size);
templateFile.getChannel().force(true);
templateFile.getChannel().transferTo(0, size, raf.getChannel());
}
tmpFile.delete();
}