KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > FileUtil


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.FileUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.io;
23
24 import org.apache.derby.io.StorageFactory;
25 import org.apache.derby.io.WritableStorageFactory;
26 import org.apache.derby.io.StorageFile;
27
28 import java.io.*;
29 import java.net.*;
30
31 /**
32     A set of public static methods for dealing with File objects.
33 */

34 public abstract class FileUtil {
35
36     private static final int BUFFER_SIZE = 4096*4;
37     /**
38         Remove a directory and all of its contents.
39
40         The results of executing File.delete() on a File object
41         that represents a directory seems to be platform
42         dependent. This method removes the directory
43         and all of its contents.
44
45         @return true if the complete directory was removed, false if it could not be.
46         If false is returned then some of the files in the directory may have been removed.
47
48     */

49     public static boolean removeDirectory(File directory) {
50
51         // System.out.println("removeDirectory " + directory);
52

53         if (directory == null)
54             return false;
55         if (!directory.exists())
56             return true;
57         if (!directory.isDirectory())
58             return false;
59
60         String JavaDoc[] list = directory.list();
61
62         // Some JVMs return null for File.list() when the
63
// directory is empty.
64
if (list != null) {
65             for (int i = 0; i < list.length; i++) {
66                 File entry = new File(directory, list[i]);
67
68                 // System.out.println("\tremoving entry " + entry);
69

70                 if (entry.isDirectory())
71                 {
72                     if (!removeDirectory(entry))
73                         return false;
74                 }
75                 else
76                 {
77                     if (!entry.delete())
78                         return false;
79                 }
80             }
81         }
82
83         return directory.delete();
84     }
85
86     public static boolean removeDirectory(String JavaDoc directory)
87     {
88         return removeDirectory(new File(directory));
89     }
90
91     /**
92       Copy a directory and all of its contents.
93       */

94     public static boolean copyDirectory(File from, File to)
95     {
96         return copyDirectory(from, to, (byte[])null, (String JavaDoc[])null);
97     }
98
99     public static boolean copyDirectory(String JavaDoc from, String JavaDoc to)
100     {
101         return copyDirectory(new File(from), new File(to));
102     }
103
104     /**
105         @param filter - array of names to not copy.
106     */

107     public static boolean copyDirectory(File from, File to, byte[] buffer,
108                                         String JavaDoc[] filter)
109     {
110         //
111
// System.out.println("copyDirectory("+from+","+to+")");
112

113         if (from == null)
114             return false;
115         if (!from.exists())
116             return true;
117         if (!from.isDirectory())
118             return false;
119
120         if (to.exists())
121         {
122             // System.out.println(to + " exists");
123
return false;
124         }
125         if (!to.mkdirs())
126         {
127             // System.out.println("can't make" + to);
128
return false;
129         }
130
131         String JavaDoc[] list = from.list();
132
133         // Some JVMs return null for File.list() when the
134
// directory is empty.
135
if (list != null) {
136
137             if (buffer == null)
138                 buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
139

140 nextFile: for (int i = 0; i < list.length; i++) {
141
142                 String JavaDoc fileName = list[i];
143
144                 if (filter != null) {
145                     for (int j = 0; j < filter.length; j++) {
146                         if (fileName.equals(filter[j]))
147                             continue nextFile;
148                     }
149                 }
150
151
152                 File entry = new File(from, fileName);
153
154                 // System.out.println("\tcopying entry " + entry);
155

156                 if (entry.isDirectory())
157                 {
158                     if (!copyDirectory(entry,new File(to,fileName),buffer,filter))
159                         return false;
160                 }
161                 else
162                 {
163                     if (!copyFile(entry,new File(to,fileName),buffer))
164                         return false;
165                 }
166             }
167         }
168         return true;
169     }
170
171     public static boolean copyFile(File from, File to)
172     {
173         return copyFile(from, to, (byte[])null);
174     }
175
176     public static boolean copyFile(File from, File to, byte[] buf)
177     {
178         if (buf == null)
179             buf = new byte[BUFFER_SIZE];
180
181         //
182
// System.out.println("Copy file ("+from+","+to+")");
183
FileInputStream from_s = null;
184         FileOutputStream to_s = null;
185
186         try {
187             from_s = new FileInputStream(from);
188             to_s = new FileOutputStream(to);
189
190             for (int bytesRead = from_s.read(buf);
191                  bytesRead != -1;
192                  bytesRead = from_s.read(buf))
193                 to_s.write(buf,0,bytesRead);
194
195             from_s.close();
196             from_s = null;
197
198             to_s.getFD().sync(); // RESOLVE: sync or no sync?
199
to_s.close();
200             to_s = null;
201         }
202         catch (IOException ioe)
203         {
204             return false;
205         }
206         finally
207         {
208             if (from_s != null)
209             {
210                 try { from_s.close(); }
211                 catch (IOException ioe) {}
212             }
213             if (to_s != null)
214             {
215                 try { to_s.close(); }
216                 catch (IOException ioe) {}
217             }
218         }
219
220         return true;
221     }
222
223     public static boolean copyDirectory( StorageFactory storageFactory,
224                                          StorageFile from,
225                                          File to)
226     {
227         return copyDirectory( storageFactory, from, to, null, null, true);
228     }
229     
230
231     public static boolean copyDirectory( StorageFactory storageFactory,
232                                          StorageFile from,
233                                          File to,
234                                          byte[] buffer,
235                                          String JavaDoc[] filter,
236                                          boolean copySubDirs)
237     {
238         if (from == null)
239             return false;
240         if (!from.exists())
241             return true;
242         if (!from.isDirectory())
243             return false;
244
245         if (to.exists())
246         {
247             // System.out.println(to + " exists");
248
return false;
249         }
250         if (!to.mkdirs())
251         {
252             // System.out.println("can't make" + to);
253
return false;
254         }
255
256         String JavaDoc[] list = from.list();
257
258         // Some JVMs return null for File.list() when the
259
// directory is empty.
260
if (list != null)
261         {
262             if (buffer == null)
263                 buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
264

265           nextFile:
266             for (int i = 0; i < list.length; i++)
267             {
268                 String JavaDoc fileName = list[i];
269
270                 if (filter != null) {
271                     for (int j = 0; j < filter.length; j++) {
272                         if (fileName.equals(filter[j]))
273                             continue nextFile;
274                     }
275                 }
276
277                 StorageFile entry = storageFactory.newStorageFile(from, fileName);
278
279                 if (entry.isDirectory())
280                 {
281                     if(copySubDirs) {
282                         if (!copyDirectory( storageFactory, entry,
283                                             new File(to,fileName), buffer,
284                                             filter, copySubDirs))
285                             return false;
286                     }
287                     else {
288                         // the request is to not copy the directories, continue
289
// to the next file in the list.
290
continue nextFile;
291                     }
292
293                 }
294                 else
295                 {
296                     if (!copyFile( storageFactory, entry, new File(to,fileName), buffer))
297                         return false;
298                 }
299             }
300         }
301         return true;
302     } // end of copyDirectory( StorageFactory sf, StorageFile from, File to, byte[] buf, String[] filter)
303

304     public static boolean copyFile( StorageFactory storageFactory, StorageFile from, File to)
305     {
306         return copyFile( storageFactory, from, to, (byte[]) null);
307     }
308     
309     public static boolean copyFile( StorageFactory storageFactory, StorageFile from, File to, byte[] buf)
310     {
311         InputStream from_s = null;
312         FileOutputStream to_s = null;
313
314         try {
315             from_s = from.getInputStream();
316             to_s = new FileOutputStream( to);
317
318             if (buf == null)
319                 buf = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
320

321             for (int bytesRead = from_s.read(buf);
322                  bytesRead != -1;
323                  bytesRead = from_s.read(buf))
324                 to_s.write(buf,0,bytesRead);
325
326             from_s.close();
327             from_s = null;
328
329             to_s.getFD().sync(); // RESOLVE: sync or no sync?
330
to_s.close();
331             to_s = null;
332         }
333         catch (IOException ioe)
334         {
335             return false;
336         }
337         finally
338         {
339             if (from_s != null)
340             {
341                 try { from_s.close(); }
342                 catch (IOException ioe) {}
343             }
344             if (to_s != null)
345             {
346                 try { to_s.close(); }
347                 catch (IOException ioe) {}
348             }
349         }
350
351         return true;
352     } // end of copyFile( StorageFactory storageFactory, StorageFile from, File to, byte[] buf)
353

354     public static boolean copyDirectory( WritableStorageFactory storageFactory,
355                                          File from,
356                                          StorageFile to)
357     {
358         return copyDirectory( storageFactory, from, to, null, null);
359     }
360     
361     public static boolean copyDirectory( WritableStorageFactory storageFactory,
362                                          File from,
363                                          StorageFile to,
364                                          byte[] buffer,
365                                          String JavaDoc[] filter)
366     {
367         if (from == null)
368             return false;
369         if (!from.exists())
370             return true;
371         if (!from.isDirectory())
372             return false;
373
374         if (to.exists())
375         {
376             // System.out.println(to + " exists");
377
return false;
378         }
379         if (!to.mkdirs())
380         {
381             // System.out.println("can't make" + to);
382
return false;
383         }
384
385         String JavaDoc[] list = from.list();
386
387         // Some JVMs return null for File.list() when the
388
// directory is empty.
389
if (list != null)
390         {
391             if (buffer == null)
392                 buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
393

394           nextFile:
395             for (int i = 0; i < list.length; i++)
396             {
397                 String JavaDoc fileName = list[i];
398
399                 if (filter != null) {
400                     for (int j = 0; j < filter.length; j++) {
401                         if (fileName.equals(filter[j]))
402                             continue nextFile;
403                     }
404                 }
405
406                 File entry = new File(from, fileName);
407
408                 if (entry.isDirectory())
409                 {
410                     if (!copyDirectory( storageFactory, entry, storageFactory.newStorageFile(to,fileName), buffer, filter))
411                         return false;
412                 }
413                 else
414                 {
415                     if (!copyFile( storageFactory, entry, storageFactory.newStorageFile(to,fileName), buffer))
416                         return false;
417                 }
418             }
419         }
420         return true;
421     } // end of copyDirectory( StorageFactory sf, StorageFile from, File to, byte[] buf, String[] filter)
422

423     public static boolean copyFile( WritableStorageFactory storageFactory, File from, StorageFile to)
424     {
425         return copyFile( storageFactory, from, to, (byte[]) null);
426     }
427     
428     public static boolean copyFile( WritableStorageFactory storageFactory, File from, StorageFile to, byte[] buf)
429     {
430         InputStream from_s = null;
431         OutputStream to_s = null;
432
433         try {
434             from_s = new FileInputStream( from);
435             to_s = to.getOutputStream();
436
437             if (buf == null)
438                 buf = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
439

440             for (int bytesRead = from_s.read(buf);
441                  bytesRead != -1;
442                  bytesRead = from_s.read(buf))
443                 to_s.write(buf,0,bytesRead);
444
445             from_s.close();
446             from_s = null;
447
448             storageFactory.sync( to_s, false); // RESOLVE: sync or no sync?
449
to_s.close();
450             to_s = null;
451         }
452         catch (IOException ioe)
453         {
454             return false;
455         }
456         finally
457         {
458             if (from_s != null)
459             {
460                 try { from_s.close(); }
461                 catch (IOException ioe) {}
462             }
463             if (to_s != null)
464             {
465                 try { to_s.close(); }
466                 catch (IOException ioe) {}
467             }
468         }
469
470         return true;
471     } // end of copyFile
472

473
474     public static boolean copyFile( WritableStorageFactory storageFactory,
475                                     StorageFile from, StorageFile to)
476     {
477         return copyFile( storageFactory, from, to, (byte[]) null);
478     }
479     
480     public static boolean copyFile( WritableStorageFactory storageFactory,
481                                     StorageFile from, StorageFile to,
482                                     byte[] buf)
483     {
484         InputStream from_s = null;
485         OutputStream to_s = null;
486
487         try {
488             from_s = from.getInputStream();
489             to_s = to.getOutputStream();
490
491             if (buf == null)
492                 buf = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
493

494             for (int bytesRead = from_s.read(buf);
495                  bytesRead != -1;
496                  bytesRead = from_s.read(buf))
497                 to_s.write(buf,0,bytesRead);
498
499             from_s.close();
500             from_s = null;
501
502             storageFactory.sync( to_s, false); // RESOLVE: sync or no sync?
503
to_s.close();
504             to_s = null;
505         }
506         catch (IOException ioe)
507         {
508             return false;
509         }
510         finally
511         {
512             if (from_s != null)
513             {
514                 try { from_s.close(); }
515                 catch (IOException ioe) {}
516             }
517             if (to_s != null)
518             {
519                 try { to_s.close(); }
520                 catch (IOException ioe) {}
521             }
522         }
523
524         return true;
525     } // end of copyFile
526

527     /**
528         Convert a file path into a File object with an absolute path
529         relative to a passed in root. If path is absolute then
530         a file object constructed from new File(path) is returned,
531         otherwise a file object is returned from new File(root, path)
532         if root is not null, otherwise null is returned.
533     */

534     public static File getAbsoluteFile(File root, String JavaDoc path) {
535         File file = new File(path);
536         if (file.isAbsolute())
537             return file;
538
539         if (root == null)
540             return null;
541
542         return new File(root, path);
543     }
544
545     /**
546         A replacement for new File(File, String) that correctly implements
547         the case when the first argument is null. The documentation for java.io.File
548         says that new File((File) null, name) is the same as new File(name).
549         This is not the case in pre 1.1.8 vms, a NullPointerException is thrown instead.
550     */

551     public static File newFile(File parent, String JavaDoc name) {
552
553         if (parent == null)
554             return new File(name);
555         else
556             return new File(parent, name);
557     }
558
559     /**
560      * Open an input stream to read a file or a URL
561      * @param fileOrURL The file or URL to open.
562      * @param bufferSize 0 => no buffering.
563      * @return an InputStream
564      * @exception StandardException Thrown on failure
565      */

566     public static InputStream getInputStream(String JavaDoc fileOrURL,int bufferSize)
567          throws IOException
568     {
569         InputStream is;
570         try {
571             is = new FileInputStream( fileOrURL );
572         }
573
574         catch (FileNotFoundException fnfe){
575             try {
576                 is = new URL( fileOrURL ).openStream();
577             } catch (MalformedURLException mfurle) {
578
579                 // if it looks like an url throw this exception
580
// otherwise throw the file not found exception
581
// If there is no : or an early colon then it's
582
// probably a file (e.g. /foo/myjar.jar or a:/foo/myjar.jar)
583
if (fileOrURL.indexOf(':') > 2)
584                     throw mfurle;
585                 throw fnfe;
586             }
587         }
588         if (bufferSize > 0)
589             is = new BufferedInputStream(is,bufferSize);
590
591         return is;
592     }
593 }
594
Popular Tags