KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > util > IOUtil


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.util;
21
22 //import java.io.FileFilter;
23
import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
30 /**
31  *
32  *
33  * @author $author$
34  */

35 public class IOUtil {
36
37     /**
38      * Default buffer size for stream utility methods
39      */

40     public static int BUFFER_SIZE = 8192;
41
42     /**
43      * Copy from an input stream to an output stream. It is up to the caller to
44      * close the streams.
45      *
46      * @param in input stream
47      * @param out output stream
48      * @throws IOException on any error
49      */

50     public static void copy(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
51         copy(in, out, -1);
52     }
53
54
55     /**
56      * Copy the specified number of bytes from an input stream to an output
57      * stream. It is up to the caller to close the streams.
58      *
59      * @param in input stream
60      * @param out output stream
61      * @param count number of bytes to copy
62      * @throws IOException on any error
63      */

64     public static void copy(InputStream JavaDoc in, OutputStream JavaDoc out, long count) throws IOException JavaDoc {
65         copy(in, out, count, BUFFER_SIZE);
66     }
67
68     /**
69      * Copy the specified number of bytes from an input stream to an output
70      * stream. It is up to the caller to close the streams.
71      *
72      * @param in input stream
73      * @param out output stream
74      * @param count number of bytes to copy
75      * @param bufferSize buffer size
76      * @throws IOException on any error
77      */

78     public static void copy(InputStream JavaDoc in, OutputStream JavaDoc out, long count, int bufferSize) throws IOException JavaDoc {
79         byte buffer[] = new byte[bufferSize];
80         int i = bufferSize;
81         if (count >= 0) {
82             while (count > 0) {
83                 if (count < bufferSize)
84                     i = in.read(buffer, 0, (int) count);
85                 else
86                     i = in.read(buffer, 0, bufferSize);
87
88                 if (i == -1)
89                     break;
90
91                 count -= i;
92                 out.write(buffer, 0, i);
93             }
94         } else {
95             while (true) {
96                 i = in.read(buffer, 0, bufferSize);
97                 if (i < 0)
98                     break;
99                 out.write(buffer, 0, i);
100             }
101         }
102     }
103   /**
104    *
105    *
106    * @param in
107    *
108    * @return
109    */

110   public static boolean closeStream(InputStream JavaDoc in) {
111     try {
112       if (in != null) {
113         in.close();
114       }
115
116       return true;
117     }
118     catch (IOException JavaDoc ioe) {
119       return false;
120     }
121   }
122
123   /**
124    *
125    *
126    * @param out
127    *
128    * @return
129    */

130   public static boolean closeStream(OutputStream JavaDoc out) {
131     try {
132       if (out != null) {
133         out.close();
134       }
135
136       return true;
137     }
138     catch (IOException JavaDoc ioe) {
139       return false;
140     }
141   }
142
143   public static boolean delTree(File JavaDoc file) {
144     if (file.isFile()) {
145       return file.delete();
146     }
147     else {
148       String JavaDoc[] list = file.list();
149       for (int i = 0; i < list.length; i++) {
150         if (!delTree(new File JavaDoc(file, list[i]))) {
151           return false;
152         }
153       }
154     }
155     return true;
156   }
157
158   public static void recurseDeleteDirectory(File JavaDoc dir) {
159
160     String JavaDoc[] files = dir.list();
161
162     if (files == null) {
163       return; // Directory could not be read
164
}
165
166     for (int i = 0; i < files.length; i++) {
167       File JavaDoc f = new File JavaDoc(dir, files[i]);
168
169       if (f.isDirectory()) {
170         recurseDeleteDirectory(f);
171
172       }
173       f.delete();
174     }
175
176     dir.delete();
177
178   }
179
180   public static void copyFile(File JavaDoc from, File JavaDoc to) throws IOException JavaDoc {
181
182     if (from.isDirectory()) {
183       if (!to.exists()) {
184         to.mkdir();
185       }
186       String JavaDoc[] children = from.list();
187       for (int i = 0; i < children.length; i++) {
188         File JavaDoc f = new File JavaDoc(from, children[i]);
189         if (f.getName().equals(".")
190             || f.getName().equals("..")) {
191           continue;
192         }
193         if (f.isDirectory()) {
194           File JavaDoc f2 = new File JavaDoc(to, f.getName());
195           copyFile(f, f2);
196         }
197         else {
198           copyFile(f, to);
199         }
200       }
201     }
202     else if (from.isFile() && (to.isDirectory() || to.isFile())) {
203       if (to.isDirectory()) {
204         to = new File JavaDoc(to, from.getName());
205       }
206       FileInputStream JavaDoc in = new FileInputStream JavaDoc(from);
207       FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(to);
208       byte[] buf = new byte[32678];
209       int read;
210       while ( (read = in.read(buf)) > -1) {
211         out.write(buf, 0, read);
212       }
213       closeStream(in);
214       closeStream(out);
215
216     }
217   }
218
219 }
220
Popular Tags