@Test public void reuse() throws Exception { byte[] contents = { 1, 2, 3 }; File f = temporary.newFile(); TemporaryFileInstaller installer = create(false, contents); assertThat(installer.install(f, true), is(true)); assertThat(installer.install(f, true), is(false)); check(f, contents); }
public boolean install(File target, boolean reuse) throws IOException { File parent = target.getAbsoluteFile().getParentFile(); if (parent.mkdirs() == false && parent.isDirectory() == false) { throw new IOException(MessageFormat.format( "failed to create a file: {0}", target)); } try (RandomAccessFile file = new RandomAccessFile(target, "rw"); FileLock lock = file.getChannel().lock(0, 0, false)) { if (reuse && isReusable(target, file)) { return false; } doInstall(target, file); return true; } }
TemporaryFileInstaller { public boolean install(File target, boolean reuse) throws IOException { File parent = target.getAbsoluteFile().getParentFile(); if (parent.mkdirs() == false && parent.isDirectory() == false) { throw new IOException(MessageFormat.format( "failed to create a file: {0}", target)); } try (RandomAccessFile file = new RandomAccessFile(target, "rw"); FileLock lock = file.getChannel().lock(0, 0, false)) { if (reuse && isReusable(target, file)) { return false; } doInstall(target, file); return true; } } }
TemporaryFileInstaller { public boolean install(File target, boolean reuse) throws IOException { File parent = target.getAbsoluteFile().getParentFile(); if (parent.mkdirs() == false && parent.isDirectory() == false) { throw new IOException(MessageFormat.format( "failed to create a file: {0}", target)); } try (RandomAccessFile file = new RandomAccessFile(target, "rw"); FileLock lock = file.getChannel().lock(0, 0, false)) { if (reuse && isReusable(target, file)) { return false; } doInstall(target, file); return true; } } private  TemporaryFileInstaller(byte[] bytes, boolean executable); }
TemporaryFileInstaller { public boolean install(File target, boolean reuse) throws IOException { File parent = target.getAbsoluteFile().getParentFile(); if (parent.mkdirs() == false && parent.isDirectory() == false) { throw new IOException(MessageFormat.format( "failed to create a file: {0}", target)); } try (RandomAccessFile file = new RandomAccessFile(target, "rw"); FileLock lock = file.getChannel().lock(0, 0, false)) { if (reuse && isReusable(target, file)) { return false; } doInstall(target, file); return true; } } private  TemporaryFileInstaller(byte[] bytes, boolean executable); static TemporaryFileInstaller newInstance(InputStream contents, boolean executable); boolean install(File target, boolean reuse); }
TemporaryFileInstaller { public boolean install(File target, boolean reuse) throws IOException { File parent = target.getAbsoluteFile().getParentFile(); if (parent.mkdirs() == false && parent.isDirectory() == false) { throw new IOException(MessageFormat.format( "failed to create a file: {0}", target)); } try (RandomAccessFile file = new RandomAccessFile(target, "rw"); FileLock lock = file.getChannel().lock(0, 0, false)) { if (reuse && isReusable(target, file)) { return false; } doInstall(target, file); return true; } } private  TemporaryFileInstaller(byte[] bytes, boolean executable); static TemporaryFileInstaller newInstance(InputStream contents, boolean executable); boolean install(File target, boolean reuse);  }