001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.io.FileUtil
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.services.io;
023:
024: import org.apache.derby.io.StorageFactory;
025: import org.apache.derby.io.WritableStorageFactory;
026: import org.apache.derby.io.StorageFile;
027:
028: import java.io.*;
029: import java.net.*;
030:
031: /**
032: A set of public static methods for dealing with File objects.
033: */
034: public abstract class FileUtil {
035:
036: private static final int BUFFER_SIZE = 4096 * 4;
037:
038: /**
039: Remove a directory and all of its contents.
040:
041: The results of executing File.delete() on a File object
042: that represents a directory seems to be platform
043: dependent. This method removes the directory
044: and all of its contents.
045:
046: @return true if the complete directory was removed, false if it could not be.
047: If false is returned then some of the files in the directory may have been removed.
048:
049: */
050: public static boolean removeDirectory(File directory) {
051:
052: // System.out.println("removeDirectory " + directory);
053:
054: if (directory == null)
055: return false;
056: if (!directory.exists())
057: return true;
058: if (!directory.isDirectory())
059: return false;
060:
061: String[] list = directory.list();
062:
063: // Some JVMs return null for File.list() when the
064: // directory is empty.
065: if (list != null) {
066: for (int i = 0; i < list.length; i++) {
067: File entry = new File(directory, list[i]);
068:
069: // System.out.println("\tremoving entry " + entry);
070:
071: if (entry.isDirectory()) {
072: if (!removeDirectory(entry))
073: return false;
074: } else {
075: if (!entry.delete())
076: return false;
077: }
078: }
079: }
080:
081: return directory.delete();
082: }
083:
084: public static boolean removeDirectory(String directory) {
085: return removeDirectory(new File(directory));
086: }
087:
088: /**
089: Copy a directory and all of its contents.
090: */
091: public static boolean copyDirectory(File from, File to) {
092: return copyDirectory(from, to, (byte[]) null, (String[]) null);
093: }
094:
095: public static boolean copyDirectory(String from, String to) {
096: return copyDirectory(new File(from), new File(to));
097: }
098:
099: /**
100: @param filter - array of names to not copy.
101: */
102: public static boolean copyDirectory(File from, File to,
103: byte[] buffer, String[] filter) {
104: //
105: // System.out.println("copyDirectory("+from+","+to+")");
106:
107: if (from == null)
108: return false;
109: if (!from.exists())
110: return true;
111: if (!from.isDirectory())
112: return false;
113:
114: if (to.exists()) {
115: // System.out.println(to + " exists");
116: return false;
117: }
118: if (!to.mkdirs()) {
119: // System.out.println("can't make" + to);
120: return false;
121: }
122:
123: String[] list = from.list();
124:
125: // Some JVMs return null for File.list() when the
126: // directory is empty.
127: if (list != null) {
128:
129: if (buffer == null)
130: buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
131:
132: nextFile: for (int i = 0; i < list.length; i++) {
133:
134: String fileName = list[i];
135:
136: if (filter != null) {
137: for (int j = 0; j < filter.length; j++) {
138: if (fileName.equals(filter[j]))
139: continue nextFile;
140: }
141: }
142:
143: File entry = new File(from, fileName);
144:
145: // System.out.println("\tcopying entry " + entry);
146:
147: if (entry.isDirectory()) {
148: if (!copyDirectory(entry, new File(to, fileName),
149: buffer, filter))
150: return false;
151: } else {
152: if (!copyFile(entry, new File(to, fileName), buffer))
153: return false;
154: }
155: }
156: }
157: return true;
158: }
159:
160: public static boolean copyFile(File from, File to) {
161: return copyFile(from, to, (byte[]) null);
162: }
163:
164: public static boolean copyFile(File from, File to, byte[] buf) {
165: if (buf == null)
166: buf = new byte[BUFFER_SIZE];
167:
168: //
169: // System.out.println("Copy file ("+from+","+to+")");
170: FileInputStream from_s = null;
171: FileOutputStream to_s = null;
172:
173: try {
174: from_s = new FileInputStream(from);
175: to_s = new FileOutputStream(to);
176:
177: for (int bytesRead = from_s.read(buf); bytesRead != -1; bytesRead = from_s
178: .read(buf))
179: to_s.write(buf, 0, bytesRead);
180:
181: from_s.close();
182: from_s = null;
183:
184: to_s.getFD().sync(); // RESOLVE: sync or no sync?
185: to_s.close();
186: to_s = null;
187: } catch (IOException ioe) {
188: return false;
189: } finally {
190: if (from_s != null) {
191: try {
192: from_s.close();
193: } catch (IOException ioe) {
194: }
195: }
196: if (to_s != null) {
197: try {
198: to_s.close();
199: } catch (IOException ioe) {
200: }
201: }
202: }
203:
204: return true;
205: }
206:
207: public static boolean copyDirectory(StorageFactory storageFactory,
208: StorageFile from, File to) {
209: return copyDirectory(storageFactory, from, to, null, null, true);
210: }
211:
212: public static boolean copyDirectory(StorageFactory storageFactory,
213: StorageFile from, File to, byte[] buffer, String[] filter,
214: boolean copySubDirs) {
215: if (from == null)
216: return false;
217: if (!from.exists())
218: return true;
219: if (!from.isDirectory())
220: return false;
221:
222: if (to.exists()) {
223: // System.out.println(to + " exists");
224: return false;
225: }
226: if (!to.mkdirs()) {
227: // System.out.println("can't make" + to);
228: return false;
229: }
230:
231: String[] list = from.list();
232:
233: // Some JVMs return null for File.list() when the
234: // directory is empty.
235: if (list != null) {
236: if (buffer == null)
237: buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
238:
239: nextFile: for (int i = 0; i < list.length; i++) {
240: String fileName = list[i];
241:
242: if (filter != null) {
243: for (int j = 0; j < filter.length; j++) {
244: if (fileName.equals(filter[j]))
245: continue nextFile;
246: }
247: }
248:
249: StorageFile entry = storageFactory.newStorageFile(from,
250: fileName);
251:
252: if (entry.isDirectory()) {
253: if (copySubDirs) {
254: if (!copyDirectory(storageFactory, entry,
255: new File(to, fileName), buffer, filter,
256: copySubDirs))
257: return false;
258: } else {
259: // the request is to not copy the directories, continue
260: // to the next file in the list.
261: continue nextFile;
262: }
263:
264: } else {
265: if (!copyFile(storageFactory, entry, new File(to,
266: fileName), buffer))
267: return false;
268: }
269: }
270: }
271: return true;
272: } // end of copyDirectory( StorageFactory sf, StorageFile from, File to, byte[] buf, String[] filter)
273:
274: public static boolean copyFile(StorageFactory storageFactory,
275: StorageFile from, File to) {
276: return copyFile(storageFactory, from, to, (byte[]) null);
277: }
278:
279: public static boolean copyFile(StorageFactory storageFactory,
280: StorageFile from, File to, byte[] buf) {
281: InputStream from_s = null;
282: FileOutputStream to_s = null;
283:
284: try {
285: from_s = from.getInputStream();
286: to_s = new FileOutputStream(to);
287:
288: if (buf == null)
289: buf = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
290:
291: for (int bytesRead = from_s.read(buf); bytesRead != -1; bytesRead = from_s
292: .read(buf))
293: to_s.write(buf, 0, bytesRead);
294:
295: from_s.close();
296: from_s = null;
297:
298: to_s.getFD().sync(); // RESOLVE: sync or no sync?
299: to_s.close();
300: to_s = null;
301: } catch (IOException ioe) {
302: return false;
303: } finally {
304: if (from_s != null) {
305: try {
306: from_s.close();
307: } catch (IOException ioe) {
308: }
309: }
310: if (to_s != null) {
311: try {
312: to_s.close();
313: } catch (IOException ioe) {
314: }
315: }
316: }
317:
318: return true;
319: } // end of copyFile( StorageFactory storageFactory, StorageFile from, File to, byte[] buf)
320:
321: public static boolean copyDirectory(
322: WritableStorageFactory storageFactory, File from,
323: StorageFile to) {
324: return copyDirectory(storageFactory, from, to, null, null);
325: }
326:
327: public static boolean copyDirectory(
328: WritableStorageFactory storageFactory, File from,
329: StorageFile to, byte[] buffer, String[] filter) {
330: if (from == null)
331: return false;
332: if (!from.exists())
333: return true;
334: if (!from.isDirectory())
335: return false;
336:
337: if (to.exists()) {
338: // System.out.println(to + " exists");
339: return false;
340: }
341: if (!to.mkdirs()) {
342: // System.out.println("can't make" + to);
343: return false;
344: }
345:
346: String[] list = from.list();
347:
348: // Some JVMs return null for File.list() when the
349: // directory is empty.
350: if (list != null) {
351: if (buffer == null)
352: buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
353:
354: nextFile: for (int i = 0; i < list.length; i++) {
355: String fileName = list[i];
356:
357: if (filter != null) {
358: for (int j = 0; j < filter.length; j++) {
359: if (fileName.equals(filter[j]))
360: continue nextFile;
361: }
362: }
363:
364: File entry = new File(from, fileName);
365:
366: if (entry.isDirectory()) {
367: if (!copyDirectory(
368: storageFactory,
369: entry,
370: storageFactory.newStorageFile(to, fileName),
371: buffer, filter))
372: return false;
373: } else {
374: if (!copyFile(storageFactory, entry, storageFactory
375: .newStorageFile(to, fileName), buffer))
376: return false;
377: }
378: }
379: }
380: return true;
381: } // end of copyDirectory( StorageFactory sf, StorageFile from, File to, byte[] buf, String[] filter)
382:
383: public static boolean copyFile(
384: WritableStorageFactory storageFactory, File from,
385: StorageFile to) {
386: return copyFile(storageFactory, from, to, (byte[]) null);
387: }
388:
389: public static boolean copyFile(
390: WritableStorageFactory storageFactory, File from,
391: StorageFile to, byte[] buf) {
392: InputStream from_s = null;
393: OutputStream to_s = null;
394:
395: try {
396: from_s = new FileInputStream(from);
397: to_s = to.getOutputStream();
398:
399: if (buf == null)
400: buf = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
401:
402: for (int bytesRead = from_s.read(buf); bytesRead != -1; bytesRead = from_s
403: .read(buf))
404: to_s.write(buf, 0, bytesRead);
405:
406: from_s.close();
407: from_s = null;
408:
409: storageFactory.sync(to_s, false); // RESOLVE: sync or no sync?
410: to_s.close();
411: to_s = null;
412: } catch (IOException ioe) {
413: return false;
414: } finally {
415: if (from_s != null) {
416: try {
417: from_s.close();
418: } catch (IOException ioe) {
419: }
420: }
421: if (to_s != null) {
422: try {
423: to_s.close();
424: } catch (IOException ioe) {
425: }
426: }
427: }
428:
429: return true;
430: } // end of copyFile
431:
432: public static boolean copyFile(
433: WritableStorageFactory storageFactory, StorageFile from,
434: StorageFile to) {
435: return copyFile(storageFactory, from, to, (byte[]) null);
436: }
437:
438: public static boolean copyFile(
439: WritableStorageFactory storageFactory, StorageFile from,
440: StorageFile to, byte[] buf) {
441: InputStream from_s = null;
442: OutputStream to_s = null;
443:
444: try {
445: from_s = from.getInputStream();
446: to_s = to.getOutputStream();
447:
448: if (buf == null)
449: buf = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
450:
451: for (int bytesRead = from_s.read(buf); bytesRead != -1; bytesRead = from_s
452: .read(buf))
453: to_s.write(buf, 0, bytesRead);
454:
455: from_s.close();
456: from_s = null;
457:
458: storageFactory.sync(to_s, false); // RESOLVE: sync or no sync?
459: to_s.close();
460: to_s = null;
461: } catch (IOException ioe) {
462: return false;
463: } finally {
464: if (from_s != null) {
465: try {
466: from_s.close();
467: } catch (IOException ioe) {
468: }
469: }
470: if (to_s != null) {
471: try {
472: to_s.close();
473: } catch (IOException ioe) {
474: }
475: }
476: }
477:
478: return true;
479: } // end of copyFile
480:
481: /**
482: Convert a file path into a File object with an absolute path
483: relative to a passed in root. If path is absolute then
484: a file object constructed from new File(path) is returned,
485: otherwise a file object is returned from new File(root, path)
486: if root is not null, otherwise null is returned.
487: */
488: public static File getAbsoluteFile(File root, String path) {
489: File file = new File(path);
490: if (file.isAbsolute())
491: return file;
492:
493: if (root == null)
494: return null;
495:
496: return new File(root, path);
497: }
498:
499: /**
500: A replacement for new File(File, String) that correctly implements
501: the case when the first argument is null. The documentation for java.io.File
502: says that new File((File) null, name) is the same as new File(name).
503: This is not the case in pre 1.1.8 vms, a NullPointerException is thrown instead.
504: */
505: public static File newFile(File parent, String name) {
506:
507: if (parent == null)
508: return new File(name);
509: else
510: return new File(parent, name);
511: }
512:
513: /**
514: * Open an input stream to read a file or a URL
515: * @param fileOrURL The file or URL to open.
516: * @param bufferSize 0 => no buffering.
517: * @return an InputStream
518: * @exception StandardException Thrown on failure
519: */
520: public static InputStream getInputStream(String fileOrURL,
521: int bufferSize) throws IOException {
522: InputStream is;
523: try {
524: is = new FileInputStream(fileOrURL);
525: }
526:
527: catch (FileNotFoundException fnfe) {
528: try {
529: is = new URL(fileOrURL).openStream();
530: } catch (MalformedURLException mfurle) {
531:
532: // if it looks like an url throw this exception
533: // otherwise throw the file not found exception
534: // If there is no : or an early colon then it's
535: // probably a file (e.g. /foo/myjar.jar or a:/foo/myjar.jar)
536: if (fileOrURL.indexOf(':') > 2)
537: throw mfurle;
538: throw fnfe;
539: }
540: }
541: if (bufferSize > 0)
542: is = new BufferedInputStream(is, bufferSize);
543:
544: return is;
545: }
546: }
|