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

Print this page




   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 org.openjdk.jigsaw;
  27 
  28 import java.io.*;






  29 import java.util.jar.*;
  30 import java.util.zip.*;
  31 import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
  32 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

  33 
  34 public final class Files {
  35 
  36     private Files() { }
  37 
  38     // paths are stored with a platform agnostic separator, '/'
  39     static String convertSeparator(String path) {
  40         return path.replace(File.separatorChar, '/');
  41     }
  42 
  43     static String platformSeparator(String path) {
  44         return path.replace('/', File.separatorChar);
  45     }
  46 
  47     static void ensureWriteable(File path) throws IOException {
  48         if (!path.canWrite())
  49             throw new IOException(path + ": is not writeable.");
  50     }
  51 
  52     static String ensureNonAbsolute(String path) throws IOException {


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

















































 112     private static void copy(File src, File dst)
 113         throws IOException
 114     {
 115         java.nio.file.Files.copy(src.toPath(), dst.toPath(),
 116                                  COPY_ATTRIBUTES, REPLACE_EXISTING);
 117     }
 118 
 119     public static interface Filter<T> {
 120         public boolean accept(T x) throws IOException;
 121     }
 122 
 123     // src, dst are directories
 124     // src must exist; dst created if it does not yet exist
 125     // Copy files from src to dst, modulo filtering
 126     //
 127     public static void copyTree(File src, File dst, Filter<File> filter)
 128         throws IOException
 129     {
 130         ensureIsDirectory(src);
 131         if (dst.exists()) {




   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 org.openjdk.jigsaw;
  27 
  28 import java.io.*;
  29 import java.nio.file.attribute.BasicFileAttributes;
  30 import java.nio.file.FileVisitor;
  31 import java.nio.file.FileVisitResult;
  32 import java.nio.file.Path;
  33 import java.util.ArrayList;
  34 import java.util.List;
  35 import java.util.jar.*;
  36 import java.util.zip.*;
  37 import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
  38 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  39 import static java.lang.String.format;
  40 
  41 public final class Files {
  42 
  43     private Files() { }
  44 
  45     // paths are stored with a platform agnostic separator, '/'
  46     static String convertSeparator(String path) {
  47         return path.replace(File.separatorChar, '/');
  48     }
  49 
  50     static String platformSeparator(String path) {
  51         return path.replace('/', File.separatorChar);
  52     }
  53 
  54     static void ensureWriteable(File path) throws IOException {
  55         if (!path.canWrite())
  56             throw new IOException(path + ": is not writeable.");
  57     }
  58 
  59     static String ensureNonAbsolute(String path) throws IOException {


  99     {
 100         if (!path.delete())
 101             throw new IOException(path + ": Cannot delete");
 102     }
 103 
 104     public static void deleteTree(File dst)
 105         throws IOException
 106     {
 107         File[] fs = listFiles(dst);
 108         for (int i = 0; i < fs.length; i++) {
 109             File f = fs[i];
 110             if (f.isDirectory()) {
 111                 deleteTree(f);
 112             } else {
 113                 delete(f);
 114             }
 115         }
 116         delete(dst);
 117     }
 118 
 119     static List<IOException> deleteAllUnchecked(Path dir) {
 120         final List<IOException> excs = new ArrayList<>();
 121         try {
 122             java.nio.file.Files.walkFileTree(dir, new FileVisitor<Path>() {
 123                 @Override
 124                 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
 125                     return FileVisitResult.CONTINUE;
 126                 }
 127                 @Override
 128                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
 129                     try {
 130                         java.nio.file.Files.delete(file);
 131                     } catch (IOException x) {
 132                         excs.add(new IOException(format("Unable to delete %s%n", file)));
 133                     }
 134                     return FileVisitResult.CONTINUE;
 135                 }
 136                 @Override
 137                 public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
 138                     try {
 139                         java.nio.file.Files.delete(dir);
 140                     } catch (IOException x) {
 141                         excs.add(new IOException(format("Unable to delete %s%n", dir)));
 142                     }
 143                     return FileVisitResult.CONTINUE;
 144                 }
 145                 @Override
 146                 public FileVisitResult visitFileFailed(Path file, IOException exc) {
 147                     excs.add(new IOException(format("Unable to visit %s%n", file)));
 148                     return FileVisitResult.CONTINUE;
 149                 }
 150             });
 151         } catch (IOException x) {
 152             excs.add(x);
 153         }
 154         return excs;
 155     }
 156 
 157     static IOException deleteUnchecked(Path file)
 158     {
 159         try {
 160             java.nio.file.Files.delete(file);
 161         } catch (IOException exc) {
 162             return new IOException(format("Unable to delete %s\n", file));
 163         }
 164         return null;
 165     }
 166 
 167 
 168     private static void copy(File src, File dst)
 169         throws IOException
 170     {
 171         java.nio.file.Files.copy(src.toPath(), dst.toPath(),
 172                                  COPY_ATTRIBUTES, REPLACE_EXISTING);
 173     }
 174 
 175     public static interface Filter<T> {
 176         public boolean accept(T x) throws IOException;
 177     }
 178 
 179     // src, dst are directories
 180     // src must exist; dst created if it does not yet exist
 181     // Copy files from src to dst, modulo filtering
 182     //
 183     public static void copyTree(File src, File dst, Filter<File> filter)
 184         throws IOException
 185     {
 186         ensureIsDirectory(src);
 187         if (dst.exists()) {