Skip to content

Commit 37b5fca

Browse files
committed
Replacing home-made copy by Files.copy. Other minor fixes
1 parent a9542ca commit 37b5fca

File tree

4 files changed

+20
-100
lines changed

4 files changed

+20
-100
lines changed

biojava-core/src/main/java/org/biojava/nbio/core/util/FileDownloadUtils.java

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
package org.biojava.nbio.core.util;
2323

2424
import java.io.File;
25-
import java.io.FileInputStream;
2625
import java.io.FileNotFoundException;
2726
import java.io.FileOutputStream;
2827
import java.io.FilenameFilter;
@@ -34,13 +33,8 @@
3433
import java.net.URL;
3534
import java.net.URLConnection;
3635
import java.nio.channels.Channels;
37-
import java.nio.channels.FileChannel;
3836
import java.nio.channels.ReadableByteChannel;
39-
import java.nio.file.FileVisitResult;
40-
import java.nio.file.Files;
41-
import java.nio.file.Path;
42-
import java.nio.file.Paths;
43-
import java.nio.file.SimpleFileVisitor;
37+
import java.nio.file.*;
4438
import java.nio.file.attribute.BasicFileAttributes;
4539
import java.util.Scanner;
4640

@@ -57,39 +51,6 @@ public enum Hash{
5751
MD5, SHA1, SHA256, UNKNOWN
5852
}
5953

60-
/**
61-
* Copy the content of file src to dst TODO since java 1.7 this is provided
62-
* in java.nio.file.Files
63-
*
64-
* @param src
65-
* @param dst
66-
* @throws IOException
67-
*/
68-
@SuppressWarnings("resource")
69-
public static void copy(File src, File dst) throws IOException {
70-
71-
// Took following recipe from
72-
// http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java
73-
// The nio package seems to be the most efficient way to copy a file
74-
FileChannel source = null;
75-
FileChannel destination = null;
76-
77-
try {
78-
// we need the supress warnings here (the warning that the stream is not closed is harmless)
79-
// see http://stackoverflow.com/questions/12970407/does-filechannel-close-close-the-underlying-stream
80-
source = new FileInputStream(src).getChannel();
81-
destination = new FileOutputStream(dst).getChannel();
82-
destination.transferFrom(source, 0, source.size());
83-
} finally {
84-
if (source != null) {
85-
source.close();
86-
}
87-
if (destination != null) {
88-
destination.close();
89-
}
90-
}
91-
}
92-
9354
/**
9455
* Gets the file extension of a file, excluding '.'.
9556
* If the file name has no extension the file name is returned.
@@ -100,7 +61,7 @@ public static String getFileExtension(File f) {
10061
String fileName = f.getName();
10162
String ext = "";
10263
int mid = fileName.lastIndexOf(".");
103-
ext = fileName.substring(mid + 1, fileName.length());
64+
ext = fileName.substring(mid + 1);
10465
return ext;
10566
}
10667

@@ -165,7 +126,7 @@ public static void downloadFile(URL url, File destination) throws IOException {
165126
}
166127

167128
logger.debug("Copying temp file [{}] to final location [{}]", tempFile, destination);
168-
copy(tempFile, destination);
129+
Files.copy(tempFile.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
169130

170131
// delete the tmp file
171132
tempFile.delete();
@@ -252,24 +213,20 @@ public static void createValidationFiles(URLConnection resourceUrlConnection, Fi
252213
public static boolean validateFile(File localFile) {
253214
File sizeFile = new File(localFile.getParentFile(), localFile.getName() + SIZE_EXT);
254215
if(sizeFile.exists()) {
255-
Scanner scanner = null;
256-
try {
257-
scanner = new Scanner(sizeFile);
258-
long expectedSize = scanner.nextLong();
259-
long actualLSize = localFile.length();
260-
if (expectedSize != actualLSize) {
261-
logger.warn("File [{}] size ({}) does not match expected size ({}).", localFile, actualLSize, expectedSize);
262-
return false;
263-
}
264-
} catch (FileNotFoundException e) {
265-
logger.warn("could not validate size of file [{}] because no size metadata file exists.", localFile);
266-
} finally {
267-
scanner.close();
268-
}
216+
try (Scanner scanner = new Scanner(sizeFile)) {
217+
long expectedSize = scanner.nextLong();
218+
long actualLSize = localFile.length();
219+
if (expectedSize != actualLSize) {
220+
logger.warn("File [{}] size ({}) does not match expected size ({}).", localFile, actualLSize, expectedSize);
221+
return false;
222+
}
223+
} catch (FileNotFoundException e) {
224+
logger.warn("could not validate size of file [{}] because no size metadata file exists.", localFile);
225+
}
269226
}
270227

271228
File[] hashFiles = localFile.getParentFile().listFiles(new FilenameFilter() {
272-
String hashPattern = String.format("%s%s_(%s|%s|%s)", localFile.getName(), HASH_EXT, Hash.MD5, Hash.SHA1, Hash.SHA256);
229+
final String hashPattern = String.format("%s%s_(%s|%s|%s)", localFile.getName(), HASH_EXT, Hash.MD5, Hash.SHA1, Hash.SHA256);
273230
@Override
274231
public boolean accept(File dir, String name) {
275232
return name.matches(hashPattern);
@@ -401,7 +358,7 @@ public static URLConnection prepareURLConnection(String url, int timeout) throws
401358
public static void deleteDirectory(Path dir) throws IOException {
402359
if(dir == null || !Files.exists(dir))
403360
return;
404-
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
361+
Files.walkFileTree(dir, new SimpleFileVisitor<>() {
405362
@Override
406363
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
407364
Files.delete(file);

biojava-core/src/test/java/org/biojava/nbio/core/util/FileDownloadUtilsTest.java

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import static org.junit.jupiter.api.Assertions.assertTrue;
99

1010
import java.io.File;
11-
import java.io.FileInputStream;
12-
import java.io.FileOutputStream;
1311
import java.io.IOException;
1412
import java.io.PrintStream;
1513
import java.net.URL;
@@ -20,43 +18,6 @@
2018

2119
class FileDownloadUtilsTest {
2220

23-
@Nested
24-
class FileCopy {
25-
26-
private File createSrcFile () throws IOException {
27-
byte [] toSave = new byte []{1,2,3,4,5};
28-
File src = Files.createTempFile("test", ".dat").toFile();
29-
try (FileOutputStream fos = new FileOutputStream(src);){
30-
fos.write(toSave);
31-
}
32-
return src;
33-
}
34-
35-
@Test
36-
void copyFile() throws IOException {
37-
File src = createSrcFile();
38-
//sanity check
39-
assertEquals(5, src.length());
40-
File dest = Files.createTempFile("dest", ".dat").toFile();
41-
assertEquals(0, dest.length());
42-
FileDownloadUtils.copy(src, dest);
43-
assertEquals(5, dest.length());
44-
45-
//original is unaffected
46-
assertEquals(5, src.length());
47-
48-
// bytes are identical
49-
try (FileInputStream fis1 = new FileInputStream(src);
50-
FileInputStream fis2 = new FileInputStream(dest)) {
51-
int b = -1;
52-
while (( b = fis1.read()) != -1) {
53-
int b2 = fis2.read();
54-
assertEquals (b, b2);
55-
}
56-
}
57-
}
58-
}
59-
6021
@Nested
6122
class FileExtension {
6223
@Test

biojava-structure/src/main/java/org/biojava/nbio/structure/cath/CathInstallation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.*;
3333
import java.net.URL;
3434
import java.nio.file.Files;
35+
import java.nio.file.StandardCopyOption;
3536
import java.text.DateFormat;
3637
import java.text.DecimalFormat;
3738
import java.text.ParseException;
@@ -653,7 +654,7 @@ protected void downloadFileFromRemote(URL remoteURL, File localFile) throws IOEx
653654
in.close();
654655
out.close();
655656

656-
FileDownloadUtils.copy(tempFile,localFile);
657+
Files.copy(tempFile.toPath(), localFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
657658

658659
// delete the tmp file
659660
tempFile.delete();

biojava-structure/src/test/java/org/biojava/nbio/structure/align/util/AtomCacheTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.nio.file.Files;
3434
import java.nio.file.Path;
3535
import java.nio.file.Paths;
36+
import java.nio.file.StandardCopyOption;
3637
import java.text.ParseException;
3738
import java.text.SimpleDateFormat;
3839
import java.util.Date;
@@ -390,7 +391,7 @@ public void testEmptyChemComp() throws IOException, StructureException {
390391
Files.createDirectories(testCif.getParent());
391392
URL resource = AtomCacheTest.class.getResource("/atp.cif.gz");
392393
File src = new File(resource.getPath());
393-
FileDownloadUtils.copy(src, testCif.toFile());
394+
Files.copy(src.toPath(), testCif, StandardCopyOption.REPLACE_EXISTING);
394395

395396
// Load structure
396397
Structure s = cache.getStructure("1ABC");
@@ -453,7 +454,7 @@ public void testEmptyGZChemComp() throws IOException, StructureException {
453454
Files.createDirectories(testCif.getParent());
454455
URL resource = AtomCacheTest.class.getResource("/atp.cif.gz");
455456
File src = new File(resource.getPath());
456-
FileDownloadUtils.copy(src, testCif.toFile());
457+
Files.copy(src.toPath(), testCif, StandardCopyOption.REPLACE_EXISTING);
457458

458459
// Load structure
459460
Structure s = cache.getStructure("1ABC");

0 commit comments

Comments
 (0)