modules/jdk.packager/src/main/java/com/oracle/tools/packager/IOUtils.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.oracle.tools.packager;
  27 
  28 import com.oracle.tools.packager.Log;
  29 import java.io.*;
  30 import java.net.URL;
  31 import java.nio.channels.FileChannel;
  32 import java.util.Arrays;






  33 
  34 public class IOUtils {
  35 
  36     public static boolean deleteRecursive(File path) throws FileNotFoundException {
  37         boolean ret = true;
  38         if (!path.exists()) { //nothing to do
  39             return true;
  40         }
  41         if (path.isDirectory()) {
  42             File[] children = path.listFiles();
  43             if (children != null) {
  44                 for (File f : children) {
  45                     ret = ret && deleteRecursive(f);
  46                 }
  47             }






  48         }
  49         return ret && path.delete();
  50     }
  51 
  52     public static void copyFromURL(URL location, File file) throws IOException {
  53         copyFromURL(location, file, false);
  54     }
  55 
  56     public static void copyFromURL(URL location, File file, boolean append) throws IOException {
  57         if (location == null) {
  58             throw new IOException("Missing input resource!");
  59         }
  60         if (file.exists() && !append) {
  61            file.delete();
  62         }
  63         InputStream in = location.openStream();
  64         FileOutputStream out = new FileOutputStream(file, append);
  65         byte[] buffer = new byte[1024];
  66         int len;
  67         while ((len = in.read(buffer)) != -1) {
  68             out.write(buffer, 0, len);
  69         }




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.oracle.tools.packager;
  27 

  28 import java.io.*;
  29 import java.net.URL;

  30 import java.util.Arrays;
  31 import java.nio.channels.FileChannel;
  32 import java.nio.file.FileVisitResult;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.nio.file.SimpleFileVisitor;
  36 import java.nio.file.attribute.BasicFileAttributes;
  37 
  38 public class IOUtils {
  39 
  40     public static void deleteRecursive(File path) throws IOException {
  41         if (!path.exists()) {
  42             return;
  43         }
  44         Path directory = path.toPath();
  45         Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
  46             @Override
  47             public FileVisitResult visitFile(Path file,
  48                             BasicFileAttributes attributes) throws IOException {
  49                 Files.delete(file);
  50                 return FileVisitResult.CONTINUE;
  51             }
  52 
  53             @Override
  54             public FileVisitResult postVisitDirectory(Path dir, IOException e)
  55                             throws IOException {
  56                 Files.delete(dir);
  57                 return FileVisitResult.CONTINUE;
  58             }
  59         });
  60     }
  61 
  62     public static void copyFromURL(URL location, File file) throws IOException {
  63         copyFromURL(location, file, false);
  64     }
  65 
  66     public static void copyFromURL(URL location, File file, boolean append) throws IOException {
  67         if (location == null) {
  68             throw new IOException("Missing input resource!");
  69         }
  70         if (file.exists() && !append) {
  71            file.delete();
  72         }
  73         InputStream in = location.openStream();
  74         FileOutputStream out = new FileOutputStream(file, append);
  75         byte[] buffer = new byte[1024];
  76         int len;
  77         while ((len = in.read(buffer)) != -1) {
  78             out.write(buffer, 0, len);
  79         }