001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.BufferedInputStream;
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.io.FileOutputStream;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.io.Reader;
049: import java.io.Writer;
050:
051: import com.quadcap.util.Util;
052:
053: /**
054: *
055: *
056: * @author Stan Bailes
057: */
058: public class IO {
059: /**
060: * Diff two files, return true if the file contents are the same
061: */
062: public static boolean cmpFile(File a, File b) throws IOException {
063: FileInputStream fa = null;
064: FileInputStream fb = null;
065: try {
066: fa = new FileInputStream(a);
067: BufferedInputStream sa = new BufferedInputStream(fa);
068: fb = new FileInputStream(b);
069: BufferedInputStream sb = new BufferedInputStream(fb);
070: int ca = sa.read();
071: int cb = sb.read();
072: while (ca >= 0 && cb >= 0 && ca == cb) {
073: ca = sa.read();
074: cb = sb.read();
075: }
076: return ca == cb;
077: } finally {
078: if (fa != null)
079: try {
080: fa.close();
081: } catch (IOException e) {
082: }
083: if (fb != null)
084: try {
085: fb.close();
086: } catch (IOException e) {
087: }
088: }
089:
090: }
091:
092: /**
093: * Copy a file
094: */
095: public static final void copyFile(File from, File to)
096: throws IOException {
097: FileOutputStream fos = new FileOutputStream(to);
098: try {
099: FileInputStream fis = new FileInputStream(from);
100: try {
101: copyStream(fis, fos);
102: } finally {
103: fis.close();
104: }
105: } finally {
106: fos.close();
107: }
108: }
109:
110: /**
111: * Copy 'is' to 'os' until EOF
112: */
113: public static final void copyStream(InputStream is, OutputStream os)
114: throws IOException {
115: byte[] buf = new byte[4096];
116: int cnt;
117: while ((cnt = is.read(buf)) > 0) {
118: os.write(buf, 0, cnt);
119: }
120: }
121:
122: /**
123: * Rate limited stream copy. Copy the input stream to the output
124: * until EOF. An initial series of "buffered" bytes is sent with
125: * no rate limit, thereafter, bytes are streamed (in 4Kbyte buffers) at
126: * the rate limit "bps" bytes per/sec.
127: * No particular effort is made to buffer the incoming stream.
128: */
129: public static final void copyStream(InputStream is,
130: OutputStream os, int bps, int buffered) throws IOException {
131: byte[] buf = new byte[4096];
132: int cnt;
133: int amt = 0;
134: long interval = (buf.length * 1000) / bps;
135: long wait = 0;
136: long start = System.currentTimeMillis();
137: while ((cnt = is.read(buf)) > 0) {
138: amt += cnt;
139: if (wait > 0)
140: Util.sleep(wait);
141: os.write(buf, 0, cnt);
142: long done = System.currentTimeMillis();
143: if (amt > buffered) {
144: long elap = done - start;
145: wait = interval - elap;
146: }
147: start = done;
148: }
149: }
150:
151: /**
152: * Copy 'r' to 'w' until EOF
153: */
154: public static final void copyStream(Reader r, Writer w)
155: throws IOException {
156: char[] buf = new char[4096];
157: int cnt;
158: while ((cnt = r.read(buf)) > 0) {
159: w.write(buf, 0, cnt);
160: }
161: }
162:
163: /**
164: * Copy 'is' to 'os' until EOF, or <b>limit</b> bytes are copied.
165: */
166: public static final void copyStream(InputStream is,
167: OutputStream os, int limit) throws IOException {
168: byte[] buf = new byte[4096];
169: int cnt;
170: while (limit > buf.length) {
171: if ((cnt = is.read(buf)) > 0) {
172: os.write(buf, 0, cnt);
173: limit -= cnt;
174: } else {
175: limit = 0;
176: }
177: }
178: if ((cnt = is.read(buf, 0, limit)) > 0) {
179: os.write(buf, 0, cnt);
180: }
181: }
182:
183: /**
184: * Write a string to an outputstream using the brutal low-byte extraction
185: * encoding technique. Works great for ISO-8859-1.
186: */
187: public static void write(OutputStream os, String s)
188: throws IOException {
189: final int len = s.length();
190: for (int i = 0; i < len; i++)
191: os.write(s.charAt(i));
192: }
193:
194: /**
195: * Read an inputstream into a byte buffer
196: */
197: public static void readFully(InputStream is, byte[] buf)
198: throws IOException {
199: int pos = 0;
200: int len = buf.length;
201: do {
202: int cnt = is.read(buf, pos, len);
203: if (cnt <= 0)
204: throw new IOException("unexpected EOF");
205: len -= cnt;
206: pos += cnt;
207: } while (len > 0);
208: }
209:
210: /**
211: * Recursively delete an entire directory. Be careful!
212: */
213: public static void deleteDirectory(File f) throws IOException {
214: File[] files = f.listFiles();
215: if (files != null) {
216: for (int i = 0; i < files.length; i++) {
217: File file = files[i];
218: if (file.isDirectory()) {
219: deleteDirectory(f);
220: } else {
221: file.delete();
222: }
223: }
224: }
225: f.delete();
226: }
227:
228: }
|