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) { private void doPreallocationKernelCopy(RecoverableRandomAccessFile file) {
try { try (RandomAccessFile templateRaf = new RandomAccessFile(osKernelCopyTemplateFile, "rw");){
RandomAccessFile templateRaf = new RandomAccessFile(osKernelCopyTemplateFile, "rw");
templateRaf.getChannel().transferTo(0, getMaxFileLength(), file.getChannel()); templateRaf.getChannel().transferTo(0, getMaxFileLength(), file.getChannel());
templateRaf.close();
} catch (ClosedByInterruptException ignored) { } catch (ClosedByInterruptException ignored) {
LOG.trace("Could not preallocate journal file with kernel copy", ignored); LOG.trace("Could not preallocate journal file with kernel copy", ignored);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {

View File

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