diff --git a/src/main/java/com/turn/ttorrent/common/Torrent.java b/src/main/java/com/turn/ttorrent/common/Torrent.java index 1f698e074..5e47135c3 100644 --- a/src/main/java/com/turn/ttorrent/common/Torrent.java +++ b/src/main/java/com/turn/ttorrent/common/Torrent.java @@ -53,6 +53,7 @@ import jargs.gnu.CmdLineParser; +import org.apache.commons.io.FileUtils; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.ConsoleAppender; import org.apache.log4j.PatternLayout; @@ -408,15 +409,7 @@ public boolean isSeeder() { * @throws IOException If an I/O error occurs while writing the file. */ public void save(File file) throws IOException { - FileOutputStream fOut = null; - try { - fOut = new FileOutputStream(file); - fOut.write(this.getEncoded()); - } finally { - if (fOut != null){ - fOut.close(); - } - } + FileUtils.writeByteArrayToFile(file, this.getEncoded()); } public static byte[] hash(byte[] data) throws NoSuchAlgorithmException { diff --git a/src/test/java/com/turn/ttorrent/FileUtil.java b/src/test/java/com/turn/ttorrent/FileUtil.java deleted file mode 100644 index 3abff5e1d..000000000 --- a/src/test/java/com/turn/ttorrent/FileUtil.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.turn.ttorrent; - -import java.io.Closeable; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; - -public class FileUtil { - - public static File getTempDirectory() { - return new File(System.getProperty("java.io.tmpdir")); - } - - public static void delete(File file) { - if (file.isDirectory()) { - File[] files = file.listFiles(); - for (File f: files) { - delete(f); - } - } - deleteFile(file); - } - - private static boolean deleteFile(File file) { - if (!file.exists()) return false; - for (int i=0; i<10; i++) { - if (file.delete()) return true; - try { - Thread.sleep(1); - } catch (InterruptedException e) { - // - } - } - return false; - } - - public static void writeFile(File file, String content) throws IOException { - FileWriter fw = null; - try { - fw = new FileWriter(file); - fw.write(content); - } finally { - close(fw); - } - } - - public static void close(Closeable closeable) { - if (closeable != null) { - try { - closeable.close(); - } catch (IOException e) { - // - } - } - } -} diff --git a/src/test/java/com/turn/ttorrent/TempFiles.java b/src/test/java/com/turn/ttorrent/TempFiles.java index bf0953d1a..50fbbb174 100644 --- a/src/test/java/com/turn/ttorrent/TempFiles.java +++ b/src/test/java/com/turn/ttorrent/TempFiles.java @@ -1,18 +1,15 @@ package com.turn.ttorrent; +import org.apache.commons.io.FileUtils; + import java.io.*; import java.util.*; public class TempFiles { - private static final File ourCurrentTempDir = FileUtil.getTempDirectory(); + private static final File ourCurrentTempDir = FileUtils.getTempDirectory(); private final File myCurrentTempDir; - private static Random ourRandom; - - static { - ourRandom = new Random(); - ourRandom.setSeed(System.currentTimeMillis()); - } + private static Random ourRandom = new Random(); private final List myFilesToDelete = new ArrayList(); private final Thread myShutdownHook; @@ -23,7 +20,7 @@ public TempFiles() { if (!myCurrentTempDir.isDirectory()) { throw new IllegalStateException("Temp directory is not a directory, was deleted by some process: " + myCurrentTempDir.getAbsolutePath() + - "\njava.io.tmpdir: " + FileUtil.getTempDirectory()); + "\njava.io.tmpdir: " + FileUtils.getTempDirectory()); } myShutdownHook = new Thread(new Runnable() { @@ -55,12 +52,6 @@ private File doCreateTempFile(String prefix, String suffix) throws IOException { return file; } - public final File createTempFile(String content) throws IOException { - File tempFile = createTempFile(); - FileUtil.writeFile(tempFile, content); - return tempFile; - } - public final File createTempFile() throws IOException { File tempFile = doCreateTempFile("test", null); registerAsTempFile(tempFile); @@ -123,10 +114,12 @@ public File getCurrentTempDir() { public void cleanup() { try { for (File file : myFilesToDelete) { - FileUtil.delete(file); + FileUtils.forceDelete(file); } myFilesToDelete.clear(); + } catch (IOException e) { + } finally { if (!myInsideShutdownHook) { Runtime.getRuntime().removeShutdownHook(myShutdownHook); diff --git a/src/test/java/com/turn/ttorrent/WaitFor.java b/src/test/java/com/turn/ttorrent/WaitFor.java index 41ab425ed..0da04cd9d 100644 --- a/src/test/java/com/turn/ttorrent/WaitFor.java +++ b/src/test/java/com/turn/ttorrent/WaitFor.java @@ -8,26 +8,15 @@ protected WaitFor() { } protected WaitFor(long timeout) { - long started = System.currentTimeMillis(); + long maxWaitMoment = System.currentTimeMillis() + timeout; try { - while(true) { - if (condition()) return; - if (System.currentTimeMillis() - started < timeout) { - Thread.sleep(myPollInterval); - } else { - break; - } + while(!condition() && System.currentTimeMillis() < maxWaitMoment){ + Thread.sleep(myPollInterval); } - } catch (InterruptedException e) { //NOP } } - protected WaitFor(long timeout, long pollInterval) { - this(timeout); - myPollInterval = pollInterval; - } - protected abstract boolean condition(); }