KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > util > FileUtil


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.util;
7
8 import java.io.BufferedReader JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.OutputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.List JavaDoc;
20
21 import fr.jayasoft.ivy.url.URLHandlerRegistry;
22
23 /**
24  * @author x.hanin
25  *
26  */

27 public class FileUtil {
28     // tried some other values with empty files... seems to be the best one (512 * 1024 is very bad)
29
// 8 * 1024 is also the size used by ant in its FileUtils... maybe they've done more study about it ;-)
30
private static final int BUFFER_SIZE = 8 * 1024;
31     public static void copy(File JavaDoc src, File JavaDoc dest, CopyProgressListener l) throws IOException JavaDoc {
32         copy(src, dest, l, false);
33     }
34     public static void copy(File JavaDoc src, File JavaDoc dest, CopyProgressListener l, boolean overwrite) throws IOException JavaDoc {
35         if (dest.exists()) {
36             if (!dest.isFile()) {
37                 throw new IOException JavaDoc("impossible to copy: destination is not a file: "+dest);
38             }
39             if (overwrite) {
40                 if (!dest.canWrite()) {
41                     dest.delete();
42                 } // if dest is writable, the copy will overwrite it without requiring a delete
43
} else {
44                 Message.verbose(dest+" already exists, nothing done");
45             }
46         }
47         copy(new FileInputStream JavaDoc(src), dest, l);
48         long srcLen = src.length();
49         long destLen = dest.length();
50         if (srcLen != destLen) {
51             dest.delete();
52             throw new IOException JavaDoc("size of source file " + src.toString() + "("
53                     + srcLen + ") differs from size of dest file " + dest.toString()
54                     + "(" + destLen + ") - please retry");
55         }
56         dest.setLastModified(src.lastModified());
57     }
58
59     public static void copy(URL JavaDoc src, File JavaDoc dest, CopyProgressListener l) throws IOException JavaDoc {
60         URLHandlerRegistry.getDefault().download(src, dest, l);
61     }
62
63     public static void copy(InputStream JavaDoc src, File JavaDoc dest, CopyProgressListener l) throws IOException JavaDoc {
64         if (dest.getParentFile() != null) {
65             dest.getParentFile().mkdirs();
66         }
67         copy(src, new FileOutputStream JavaDoc(dest), l);
68     }
69
70     public static void copy(InputStream JavaDoc src, OutputStream JavaDoc dest, CopyProgressListener l) throws IOException JavaDoc {
71         try {
72             CopyProgressEvent evt = null;
73             if (l != null) {
74                 evt = new CopyProgressEvent();
75             }
76             byte buffer[]=new byte[BUFFER_SIZE];
77             int c;
78             long total = 0;
79             
80             if (l != null) {
81                 l.start(evt);
82             }
83             while( (c = src.read(buffer)) != -1 ) {
84                 if (Thread.currentThread().isInterrupted()) {
85                     throw new IOException JavaDoc("transfer interrupted");
86                 }
87                 dest.write(buffer, 0, c);
88                 total += c;
89                 if (l != null) {
90                     l.progress(evt.update(buffer, c, total));
91                 }
92             }
93             if (l != null) {
94                 l.end(evt.update(buffer, 0, total));
95             }
96         } finally {
97             try {
98                 src.close();
99             } catch (IOException JavaDoc ex) {
100                 dest.close();
101                 throw ex;
102             }
103             dest.close();
104         }
105     }
106
107     public static String JavaDoc readEntirely(BufferedReader JavaDoc in) throws IOException JavaDoc {
108         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
109
110         String JavaDoc line = in.readLine();
111         while (line != null) {
112             buf.append(line + "\n");
113             line = in.readLine();
114         }
115         in.close();
116         return buf.toString();
117     }
118     
119     public static String JavaDoc concat(String JavaDoc dir, String JavaDoc file) {
120         return dir+"/"+file;
121     }
122     
123     public static void forceDelete(File JavaDoc f) {
124         if (f.isDirectory()) {
125             File JavaDoc[] sub = f.listFiles();
126             for (int i = 0; i < sub.length; i++) {
127                 forceDelete(sub[i]);
128             }
129         }
130         f.delete();
131     }
132     /**
133      * Returns a list of Files composed of all directories being
134      * parent of file and child of root + file and root themselves.
135      *
136      * Example:
137      * getPathFiles(new File("test"), new File("test/dir1/dir2/file.txt"))
138      * => {new File("test/dir1"), new File("test/dir1/dir2"), new File("test/dir1/dir2/file.txt") }
139      *
140      * Note that if root is not an ancester of file, or if root is null, all directories from the
141      * file system root will be returned.
142      */

143     public static List JavaDoc getPathFiles(File JavaDoc root, File JavaDoc file) {
144         List JavaDoc ret = new ArrayList JavaDoc();
145         while (file != null && !file.getAbsolutePath().equals(root.getAbsolutePath())) {
146             ret.add(file);
147             file = file.getParentFile();
148         }
149         if (root != null) {
150             ret.add(root);
151         }
152         Collections.reverse(ret);
153         return ret;
154     }
155     /**
156      * Returns a collection of all Files being contained in the given directory,
157      * recursively, including directories.
158      * @param dir
159      * @return
160      */

161     public static Collection JavaDoc listAll(File JavaDoc dir) {
162         return listAll(dir, new ArrayList JavaDoc());
163     }
164     private static Collection JavaDoc listAll(File JavaDoc file, Collection JavaDoc list) {
165         if (file.exists()) {
166             list.add(file);
167         }
168         if (file.isDirectory()) {
169             File JavaDoc[] files = file.listFiles();
170             for (int i = 0; i < files.length; i++) {
171                 listAll(files[i], list);
172             }
173         }
174         return list;
175     }
176
177 }
178
Popular Tags