2222package org .biojava .nbio .core .util ;
2323
2424import java .io .File ;
25- import java .io .FileInputStream ;
2625import java .io .FileNotFoundException ;
2726import java .io .FileOutputStream ;
2827import java .io .FilenameFilter ;
3433import java .net .URL ;
3534import java .net .URLConnection ;
3635import java .nio .channels .Channels ;
37- import java .nio .channels .FileChannel ;
3836import 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 .*;
4438import java .nio .file .attribute .BasicFileAttributes ;
4539import 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 );
0 commit comments