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
32 public final class Files {
33
34 private Files() { }
35
36 // paths are stored with a platform agnostic separator, '/'
37 static String convertSeparator(String path) {
38 return path.replace(File.separatorChar, '/');
39 }
40
41 static String platformSeparator(String path) {
42 return path.replace('/', File.separatorChar);
43 }
44
45 static void ensureWriteable(File path) throws IOException {
46 if (!path.canWrite())
47 throw new IOException(path + ": is not writeable.");
48 }
49
50 static String ensureNonAbsolute(String path) throws IOException {
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);
156 } else if (!dst.mkdirs())
157 throw new IOException(dst + ": Cannot create directory");
158 String[] sls = list(src);
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)
|
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()) {
132 if (!dst.isDirectory())
133 delete(dst);
134 } else if (!dst.mkdirs())
135 throw new IOException(dst + ": Cannot create directory");
136 String[] sls = list(src);
150 public static void copyTree(File src, File dst)
151 throws IOException
152 {
153 copyTree(src, dst, null);
154 }
155
156 private static void storeTree(File src, JarOutputStream dst, boolean deflate,
157 Filter<File> filter, String dstPath)
158 throws IOException
159 {
160 ensureIsDirectory(src);
161 String[] sls = list(src);
162 for (int i = 0; i < sls.length; i++) {
163 File sf = new File(src, sls[i]);
164 if (filter != null && !filter.accept(sf))
165 continue;
166 String dp = (dstPath == null) ? sls[i] : dstPath + "/" + sls[i];
167 if (sf.isDirectory()) {
168 storeTree(sf, dst, deflate, filter, dp);
169 } else {
170 ensureIsFile(sf);
171 try (OutputStream out = newOutputStream(dst, deflate, dp)) {
172 java.nio.file.Files.copy(sf.toPath(), out);
173 }
174 }
175 }
176 }
177
178 public static void storeTree(File src, JarOutputStream dst, boolean deflate,
179 Filter<File> filter)
180 throws IOException
181 {
182 storeTree(src, dst, deflate, filter, null);
183 }
184
185 public static void storeTree(File src, JarOutputStream dst, boolean deflate)
186 throws IOException
187 {
188 storeTree(src, dst, deflate, null, null);
189 }
190
191 public static interface Visitor<T> {
192 public void accept(T x) throws IOException;
193 }
194
195 public static void walkTree(File src, Visitor<File> visitor)
|