KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > io > IO


1 package com.quadcap.io;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedInputStream JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44 import java.io.FileOutputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.OutputStream JavaDoc;
48 import java.io.Reader JavaDoc;
49 import java.io.Writer JavaDoc;
50
51 import com.quadcap.util.Util;
52
53 /**
54  *
55  *
56  * @author Stan Bailes
57  */

58 public class IO {
59     /**
60      * Diff two files, return true if the file contents are the same
61      */

62     public static boolean cmpFile(File JavaDoc a, File JavaDoc b) throws IOException JavaDoc {
63         FileInputStream JavaDoc fa = null;
64         FileInputStream JavaDoc fb = null;
65         try {
66             fa = new FileInputStream JavaDoc(a);
67             BufferedInputStream JavaDoc sa = new BufferedInputStream JavaDoc(fa);
68             fb = new FileInputStream JavaDoc(b);
69             BufferedInputStream JavaDoc sb = new BufferedInputStream JavaDoc(fb);
70             int ca = sa.read();
71             int cb = sb.read();
72             while (ca >= 0 && cb >= 0 && ca == cb) {
73                 ca = sa.read();
74                 cb = sb.read();
75             }
76             return ca == cb;
77         } finally {
78             if (fa != null) try { fa.close(); } catch (IOException JavaDoc e) {}
79             if (fb != null) try { fb.close(); } catch (IOException JavaDoc e) {}
80         }
81             
82     }
83     
84     /**
85      * Copy a file
86      */

87     public static final void copyFile(File JavaDoc from, File JavaDoc to) throws IOException JavaDoc {
88         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(to);
89         try {
90             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(from);
91             try {
92                 copyStream(fis, fos);
93             } finally {
94                 fis.close();
95             }
96         } finally {
97             fos.close();
98         }
99     }
100
101     /**
102      * Copy 'is' to 'os' until EOF
103      */

104     public static final void copyStream(InputStream JavaDoc is, OutputStream JavaDoc os)
105         throws IOException JavaDoc
106     {
107         byte[] buf = new byte[4096];
108         int cnt;
109         while ((cnt = is.read(buf)) > 0) {
110             os.write(buf, 0, cnt);
111         }
112     }
113
114     /**
115      * Rate limited stream copy. Copy the input stream to the output
116      * until EOF. An initial series of "buffered" bytes is sent with
117      * no rate limit, thereafter, bytes are streamed (in 4Kbyte buffers) at
118      * the rate limit "bps" bytes per/sec.
119      * No particular effort is made to buffer the incoming stream.
120      */

121     public static final void copyStream(InputStream JavaDoc is, OutputStream JavaDoc os,
122                                         int bps, int buffered)
123         throws IOException JavaDoc
124     {
125         byte[] buf = new byte[4096];
126         int cnt;
127         int amt = 0;
128         long interval = (buf.length * 1000) / bps;
129         long wait = 0;
130         long start = System.currentTimeMillis();
131         while ((cnt = is.read(buf)) > 0) {
132             amt += cnt;
133             if (wait > 0) Util.sleep(wait);
134             os.write(buf, 0, cnt);
135             long done = System.currentTimeMillis();
136             if (amt > buffered) {
137                 long elap = done - start;
138                 wait = interval - elap;
139             }
140             start = done;
141         }
142     }
143
144     /**
145      * Copy 'r' to 'w' until EOF
146      */

147     public static final void copyStream(Reader JavaDoc r, Writer JavaDoc w)
148         throws IOException JavaDoc
149     {
150         char[] buf = new char[4096];
151         int cnt;
152         while ((cnt = r.read(buf)) > 0) {
153             w.write(buf, 0, cnt);
154         }
155     }
156
157     /**
158      * Copy 'is' to 'os' until EOF, or <b>limit</b> bytes are copied.
159      */

160     public static final void copyStream(InputStream JavaDoc is, OutputStream JavaDoc os,
161                                         int limit)
162         throws IOException JavaDoc
163     {
164         byte[] buf = new byte[4096];
165         int cnt;
166         while (limit > buf.length) {
167             if ((cnt = is.read(buf)) > 0) {
168                 os.write(buf, 0, cnt);
169                 limit -= cnt;
170             } else {
171                 limit = 0;
172             }
173         }
174         if ((cnt = is.read(buf, 0, limit)) > 0) {
175             os.write(buf, 0, cnt);
176         }
177     }
178
179     /**
180      * Write a string to an outputstream using the brutal low-byte extraction
181      * encoding technique. Works great for ISO-8859-1.
182      */

183     public static void write(OutputStream JavaDoc os, String JavaDoc s) throws IOException JavaDoc {
184         final int len = s.length();
185         for (int i = 0; i < len; i++) os.write(s.charAt(i));
186     }
187
188     /**
189      * Read an inputstream into a byte buffer
190      */

191     public static void readFully(InputStream JavaDoc is, byte[] buf)
192         throws IOException JavaDoc
193     {
194         int pos = 0;
195         int len = buf.length;
196         do {
197             int cnt = is.read(buf, pos, len);
198             if (cnt <= 0) throw new IOException JavaDoc("unexpected EOF");
199             len -= cnt;
200             pos += cnt;
201         } while (len > 0);
202     }
203
204     /**
205      * Recursively delete an entire directory. Be careful!
206      */

207     public static void deleteDirectory(File JavaDoc f) throws IOException JavaDoc {
208         File JavaDoc[] files = f.listFiles();
209         if (files != null) {
210             for (int i = 0; i < files.length; i++) {
211                 File JavaDoc file = files[i];
212                 if (file.isDirectory()) {
213                     deleteDirectory(f);
214                 } else {
215                     file.delete();
216                 }
217             }
218         }
219         f.delete();
220     }
221
222 }
223
Popular Tags