diff --git a/biojava-core/src/main/java/org/biojava/nbio/core/util/FileDownloadUtils.java b/biojava-core/src/main/java/org/biojava/nbio/core/util/FileDownloadUtils.java index 8c3a691e0f..e099122af2 100644 --- a/biojava-core/src/main/java/org/biojava/nbio/core/util/FileDownloadUtils.java +++ b/biojava-core/src/main/java/org/biojava/nbio/core/util/FileDownloadUtils.java @@ -22,7 +22,6 @@ package org.biojava.nbio.core.util; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilenameFilter; @@ -34,13 +33,8 @@ import java.net.URL; import java.net.URLConnection; import java.nio.channels.Channels; -import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; +import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.Scanner; @@ -57,39 +51,6 @@ public enum Hash{ MD5, SHA1, SHA256, UNKNOWN } - /** - * Copy the content of file src to dst TODO since java 1.7 this is provided - * in java.nio.file.Files - * - * @param src - * @param dst - * @throws IOException - */ - @SuppressWarnings("resource") - public static void copy(File src, File dst) throws IOException { - - // Took following recipe from - // http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java - // The nio package seems to be the most efficient way to copy a file - FileChannel source = null; - FileChannel destination = null; - - try { - // we need the supress warnings here (the warning that the stream is not closed is harmless) - // see http://stackoverflow.com/questions/12970407/does-filechannel-close-close-the-underlying-stream - source = new FileInputStream(src).getChannel(); - destination = new FileOutputStream(dst).getChannel(); - destination.transferFrom(source, 0, source.size()); - } finally { - if (source != null) { - source.close(); - } - if (destination != null) { - destination.close(); - } - } - } - /** * Gets the file extension of a file, excluding '.'. * If the file name has no extension the file name is returned. @@ -100,7 +61,7 @@ public static String getFileExtension(File f) { String fileName = f.getName(); String ext = ""; int mid = fileName.lastIndexOf("."); - ext = fileName.substring(mid + 1, fileName.length()); + ext = fileName.substring(mid + 1); return ext; } @@ -165,7 +126,7 @@ public static void downloadFile(URL url, File destination) throws IOException { } logger.debug("Copying temp file [{}] to final location [{}]", tempFile, destination); - copy(tempFile, destination); + Files.copy(tempFile.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING); // delete the tmp file tempFile.delete(); @@ -252,24 +213,20 @@ public static void createValidationFiles(URLConnection resourceUrlConnection, Fi public static boolean validateFile(File localFile) { File sizeFile = new File(localFile.getParentFile(), localFile.getName() + SIZE_EXT); if(sizeFile.exists()) { - Scanner scanner = null; - try { - scanner = new Scanner(sizeFile); - long expectedSize = scanner.nextLong(); - long actualLSize = localFile.length(); - if (expectedSize != actualLSize) { - logger.warn("File [{}] size ({}) does not match expected size ({}).", localFile, actualLSize, expectedSize); - return false; - } - } catch (FileNotFoundException e) { - logger.warn("could not validate size of file [{}] because no size metadata file exists.", localFile); - } finally { - scanner.close(); - } + try (Scanner scanner = new Scanner(sizeFile)) { + long expectedSize = scanner.nextLong(); + long actualSize = localFile.length(); + if (expectedSize != actualSize) { + logger.warn("File [{}] size ({}) does not match expected size ({}).", localFile, actualSize, expectedSize); + return false; + } + } catch (FileNotFoundException e) { + logger.warn("could not validate size of file [{}] because no size metadata file exists.", localFile); + } } File[] hashFiles = localFile.getParentFile().listFiles(new FilenameFilter() { - String hashPattern = String.format("%s%s_(%s|%s|%s)", localFile.getName(), HASH_EXT, Hash.MD5, Hash.SHA1, Hash.SHA256); + final String hashPattern = String.format("%s%s_(%s|%s|%s)", localFile.getName(), HASH_EXT, Hash.MD5, Hash.SHA1, Hash.SHA256); @Override public boolean accept(File dir, String name) { return name.matches(hashPattern); @@ -401,7 +358,7 @@ public static URLConnection prepareURLConnection(String url, int timeout) throws public static void deleteDirectory(Path dir) throws IOException { if(dir == null || !Files.exists(dir)) return; - Files.walkFileTree(dir, new SimpleFileVisitor() { + Files.walkFileTree(dir, new SimpleFileVisitor<>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); diff --git a/biojava-core/src/test/java/org/biojava/nbio/core/util/FileDownloadUtilsTest.java b/biojava-core/src/test/java/org/biojava/nbio/core/util/FileDownloadUtilsTest.java index 987ef6be8e..201ad88e48 100644 --- a/biojava-core/src/test/java/org/biojava/nbio/core/util/FileDownloadUtilsTest.java +++ b/biojava-core/src/test/java/org/biojava/nbio/core/util/FileDownloadUtilsTest.java @@ -8,8 +8,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.net.URL; @@ -20,43 +18,6 @@ class FileDownloadUtilsTest { - @Nested - class FileCopy { - - private File createSrcFile () throws IOException { - byte [] toSave = new byte []{1,2,3,4,5}; - File src = Files.createTempFile("test", ".dat").toFile(); - try (FileOutputStream fos = new FileOutputStream(src);){ - fos.write(toSave); - } - return src; - } - - @Test - void copyFile() throws IOException { - File src = createSrcFile(); - //sanity check - assertEquals(5, src.length()); - File dest = Files.createTempFile("dest", ".dat").toFile(); - assertEquals(0, dest.length()); - FileDownloadUtils.copy(src, dest); - assertEquals(5, dest.length()); - - //original is unaffected - assertEquals(5, src.length()); - - // bytes are identical - try (FileInputStream fis1 = new FileInputStream(src); - FileInputStream fis2 = new FileInputStream(dest)) { - int b = -1; - while (( b = fis1.read()) != -1) { - int b2 = fis2.read(); - assertEquals (b, b2); - } - } - } - } - @Nested class FileExtension { @Test diff --git a/biojava-structure/src/main/java/org/biojava/nbio/structure/cath/CathInstallation.java b/biojava-structure/src/main/java/org/biojava/nbio/structure/cath/CathInstallation.java index 7c7d2163ef..acf1489a02 100644 --- a/biojava-structure/src/main/java/org/biojava/nbio/structure/cath/CathInstallation.java +++ b/biojava-structure/src/main/java/org/biojava/nbio/structure/cath/CathInstallation.java @@ -32,6 +32,7 @@ import java.io.*; import java.net.URL; import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.ParseException; @@ -653,7 +654,7 @@ protected void downloadFileFromRemote(URL remoteURL, File localFile) throws IOEx in.close(); out.close(); - FileDownloadUtils.copy(tempFile,localFile); + Files.copy(tempFile.toPath(), localFile.toPath(), StandardCopyOption.REPLACE_EXISTING); // delete the tmp file tempFile.delete(); diff --git a/biojava-structure/src/test/java/org/biojava/nbio/structure/align/util/AtomCacheTest.java b/biojava-structure/src/test/java/org/biojava/nbio/structure/align/util/AtomCacheTest.java index af56e67046..ec2763df9a 100644 --- a/biojava-structure/src/test/java/org/biojava/nbio/structure/align/util/AtomCacheTest.java +++ b/biojava-structure/src/test/java/org/biojava/nbio/structure/align/util/AtomCacheTest.java @@ -33,6 +33,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @@ -390,7 +391,7 @@ public void testEmptyChemComp() throws IOException, StructureException { Files.createDirectories(testCif.getParent()); URL resource = AtomCacheTest.class.getResource("/atp.cif.gz"); File src = new File(resource.getPath()); - FileDownloadUtils.copy(src, testCif.toFile()); + Files.copy(src.toPath(), testCif, StandardCopyOption.REPLACE_EXISTING); // Load structure Structure s = cache.getStructure("1ABC"); @@ -453,7 +454,7 @@ public void testEmptyGZChemComp() throws IOException, StructureException { Files.createDirectories(testCif.getParent()); URL resource = AtomCacheTest.class.getResource("/atp.cif.gz"); File src = new File(resource.getPath()); - FileDownloadUtils.copy(src, testCif.toFile()); + Files.copy(src.toPath(), testCif, StandardCopyOption.REPLACE_EXISTING); // Load structure Structure s = cache.getStructure("1ABC");