src/share/classes/org/openjdk/jigsaw/Files.java

Print this page




  90     {
  91         if (!path.delete())
  92             throw new IOException(path + ": Cannot delete");
  93     }
  94 
  95     public static void deleteTree(File dst)
  96         throws IOException
  97     {
  98         File[] fs = listFiles(dst);
  99         for (int i = 0; i < fs.length; i++) {
 100             File f = fs[i];
 101             if (f.isDirectory()) {
 102                 deleteTree(f);
 103             } else {
 104                 delete(f);
 105             }
 106         }
 107         delete(dst);
 108     }
 109 
 110     private static void copy(File src, OutputStream out)
 111         throws IOException
 112     {
 113         ensureIsFile(src);
 114         byte[] buf = new byte[8192];
 115         FileInputStream in = new FileInputStream(src);
 116         try {
 117             try {
 118                 int n;
 119                 while ((n = in.read(buf)) > 0) {
 120                     out.write(buf, 0, n);
 121                 }
 122             } finally {
 123                 out.close();
 124             }
 125         } finally {
 126             in.close();
 127         }
 128     }
 129 
 130     private static void copy(File src, File dst)
 131         throws IOException
 132     {
 133         if (dst.exists())
 134             ensureIsFile(dst);
 135         copy(src, new FileOutputStream(dst));



 136         dst.setLastModified(src.lastModified());
 137         if (src.canExecute())
 138             dst.setExecutable(true, false);
 139     }
 140 
 141     public static interface Filter<T> {
 142         public boolean accept(T x) throws IOException;
 143     }
 144 
 145     // src, dst are directories
 146     // src must exist; dst created if it does not yet exist
 147     // Copy files from src to dst, modulo filtering
 148     //
 149     public static void copyTree(File src, File dst, Filter<File> filter)
 150         throws IOException
 151     {
 152         ensureIsDirectory(src);
 153         if (dst.exists()) {
 154             if (!dst.isDirectory())
 155                 delete(dst);


 172     public static void copyTree(File src, File dst)
 173         throws IOException
 174     {
 175         copyTree(src, dst, null);
 176     }
 177 
 178     private static void storeTree(File src, JarOutputStream dst, boolean deflate,
 179                                   Filter<File> filter, String dstPath)
 180         throws IOException
 181     {
 182         ensureIsDirectory(src);
 183         String[] sls = list(src);
 184         for (int i = 0; i < sls.length; i++) {
 185             File sf = new File(src, sls[i]);
 186             if (filter != null && !filter.accept(sf))
 187                 continue;
 188             String dp = (dstPath == null) ? sls[i] : dstPath + "/" + sls[i];
 189             if (sf.isDirectory()) {
 190                 storeTree(sf, dst, deflate, filter, dp);
 191             } else {
 192                 copy(sf, newOutputStream(dst, deflate, dp));

 193             }
 194         }

 195     }
 196 
 197     public static void storeTree(File src, JarOutputStream dst, boolean deflate,
 198                                  Filter<File> filter)
 199         throws IOException
 200     {
 201         storeTree(src, dst, deflate, filter, null);
 202     }
 203 
 204     public static void storeTree(File src, JarOutputStream dst, boolean deflate)
 205         throws IOException
 206     {
 207         storeTree(src, dst, deflate, null, null);
 208     }
 209 
 210     public static interface Visitor<T> {
 211         public void accept(T x) throws IOException;
 212     }
 213 
 214     public static void walkTree(File src, Visitor<File> visitor)




  90     {
  91         if (!path.delete())
  92             throw new IOException(path + ": Cannot delete");
  93     }
  94 
  95     public static void deleteTree(File dst)
  96         throws IOException
  97     {
  98         File[] fs = listFiles(dst);
  99         for (int i = 0; i < fs.length; i++) {
 100             File f = fs[i];
 101             if (f.isDirectory()) {
 102                 deleteTree(f);
 103             } else {
 104                 delete(f);
 105             }
 106         }
 107         delete(dst);
 108     }
 109 




















 110     private static void copy(File src, File dst)
 111         throws IOException
 112     {
 113         if (dst.exists())
 114             ensureIsFile(dst);
 115         ensureIsFile(src);
 116         try (FileOutputStream fos = new FileOutputStream(dst)) {
 117             java.nio.file.Files.copy(src.toPath(), fos);
 118         }
 119         dst.setLastModified(src.lastModified());
 120         if (src.canExecute())
 121             dst.setExecutable(true, false);
 122     }
 123 
 124     public static interface Filter<T> {
 125         public boolean accept(T x) throws IOException;
 126     }
 127 
 128     // src, dst are directories
 129     // src must exist; dst created if it does not yet exist
 130     // Copy files from src to dst, modulo filtering
 131     //
 132     public static void copyTree(File src, File dst, Filter<File> filter)
 133         throws IOException
 134     {
 135         ensureIsDirectory(src);
 136         if (dst.exists()) {
 137             if (!dst.isDirectory())
 138                 delete(dst);


 155     public static void copyTree(File src, File dst)
 156         throws IOException
 157     {
 158         copyTree(src, dst, null);
 159     }
 160 
 161     private static void storeTree(File src, JarOutputStream dst, boolean deflate,
 162                                   Filter<File> filter, String dstPath)
 163         throws IOException
 164     {
 165         ensureIsDirectory(src);
 166         String[] sls = list(src);
 167         for (int i = 0; i < sls.length; i++) {
 168             File sf = new File(src, sls[i]);
 169             if (filter != null && !filter.accept(sf))
 170                 continue;
 171             String dp = (dstPath == null) ? sls[i] : dstPath + "/" + sls[i];
 172             if (sf.isDirectory()) {
 173                 storeTree(sf, dst, deflate, filter, dp);
 174             } else {
 175                 try (OutputStream out = newOutputStream(dst, deflate, dp)) {
 176                     java.nio.file.Files.copy(src.toPath(), out);
 177                 }
 178             }
 179         }
 180     }
 181 
 182     public static void storeTree(File src, JarOutputStream dst, boolean deflate,
 183                                  Filter<File> filter)
 184         throws IOException
 185     {
 186         storeTree(src, dst, deflate, filter, null);
 187     }
 188 
 189     public static void storeTree(File src, JarOutputStream dst, boolean deflate)
 190         throws IOException
 191     {
 192         storeTree(src, dst, deflate, null, null);
 193     }
 194 
 195     public static interface Visitor<T> {
 196         public void accept(T x) throws IOException;
 197     }
 198 
 199     public static void walkTree(File src, Visitor<File> visitor)