Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing home-made copy by Files.copy #1085

Merged
merged 4 commits into from Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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.
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Path>() {
Files.walkFileTree(dir, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
Expand Down
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down