test/java/net/URLClassLoader/closetest/Common.java

Print this page




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


  26 
  27 public class Common {
  28 
  29     static void copyFile (String src, String dst) {
  30         copyFile (new File(src), new File(dst));
  31     }
  32 
  33     static void copyDir (String src, String dst) {
  34         copyDir (new File(src), new File(dst));
  35     }
  36 
  37     static void copyFile (File src, File dst) {
  38         try {
  39             if (!src.isFile()) {
  40                 throw new RuntimeException ("File not found: " + src.toString());
  41             }
  42             dst.delete();


  43             dst.createNewFile();
  44             FileInputStream i = new FileInputStream (src);
  45             FileOutputStream o = new FileOutputStream (dst);
  46             byte[] buf = new byte [1024];
  47             int count;
  48             while ((count=i.read(buf)) >= 0) {
  49                 o.write (buf, 0, count);
  50             }
  51             i.close();
  52             o.close();
  53         } catch (IOException e) {
  54             throw new RuntimeException (e);
  55         }
  56     }
  57 
  58     static void rm_minus_rf (File path) {
  59         if (!path.exists()) {
  60             return;
  61         }
  62         if (path.isFile()) {
  63             if (!path.delete()) {
  64                 throw new RuntimeException ("Could not delete " + path);
  65             }
  66         } else if (path.isDirectory ()) {
  67             String[] names = path.list();
  68             File[] files = path.listFiles();
  69             for (int i=0; i<files.length; i++) {
  70                 rm_minus_rf (new File(path, names[i]));
  71             }
  72             if (!path.delete()) {
  73                 throw new RuntimeException ("Could not delete " + path);
  74             }
  75         } else {
  76             throw new RuntimeException ("Trying to delete something that isn't a file or a directory");
  77         }
  78     }
  79 
  80     static void copyDir (File src, File dst) {
  81         if (!src.isDirectory()) {
  82             throw new RuntimeException ("Dir not found: " + src.toString());
  83         }
  84         if (dst.exists()) {
  85             throw new RuntimeException ("Dir exists: " + dst.toString());
  86         }
  87         dst.mkdir();
  88         String[] names = src.list();
  89         File[] files = src.listFiles();
  90         for (int i=0; i<files.length; i++) {
  91             String f = names[i];
  92             if (files[i].isDirectory()) {
  93                 copyDir (files[i], new File (dst, f));
  94             } else {




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 import java.net.*;
  26 import java.nio.file.NoSuchFileException;
  27 import jdk.testlibrary.FileUtils;
  28 
  29 public class Common {
  30 
  31     static void copyFile (String src, String dst) {
  32         copyFile (new File(src), new File(dst));
  33     }
  34 
  35     static void copyDir (String src, String dst) {
  36         copyDir (new File(src), new File(dst));
  37     }
  38 
  39     static void copyFile (File src, File dst) {
  40         try {
  41             if (!src.isFile()) {
  42                 throw new RuntimeException ("File not found: " + src.toString());
  43             }
  44             // delete dst, if it exists
  45             try { FileUtils.delete(dst.toPath()); }
  46             catch (NoSuchFileException x) {}
  47             dst.createNewFile();
  48             FileInputStream i = new FileInputStream (src);
  49             FileOutputStream o = new FileOutputStream (dst);
  50             byte[] buf = new byte [1024];
  51             int count;
  52             while ((count=i.read(buf)) >= 0) {
  53                 o.write (buf, 0, count);
  54             }
  55             i.close();
  56             o.close();
  57         } catch (IOException | InterruptedException e) {
  58             throw new RuntimeException (e);
  59         }
  60     }
  61 
  62     static void rm_minus_rf (File path) throws IOException, InterruptedException {
  63         if (!path.exists()) {
  64             return;
  65         }
  66         if (path.isFile()) {
  67             FileUtils.delete(path.toPath());


  68         } else if (path.isDirectory ()) {
  69             FileUtils.deleteTree(path.toPath());







  70         } else {
  71             throw new RuntimeException ("Trying to delete something that isn't a file or a directory");
  72         }
  73     }
  74 
  75     static void copyDir (File src, File dst) {
  76         if (!src.isDirectory()) {
  77             throw new RuntimeException ("Dir not found: " + src.toString());
  78         }
  79         if (dst.exists()) {
  80             throw new RuntimeException ("Dir exists: " + dst.toString());
  81         }
  82         dst.mkdir();
  83         String[] names = src.list();
  84         File[] files = src.listFiles();
  85         for (int i=0; i<files.length; i++) {
  86             String f = names[i];
  87             if (files[i].isDirectory()) {
  88                 copyDir (files[i], new File (dst, f));
  89             } else {