00001
package com.quadcap.io;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.BufferedInputStream;
00042
import java.io.File;
00043
import java.io.FileInputStream;
00044
import java.io.FileOutputStream;
00045
import java.io.IOException;
00046
import java.io.InputStream;
00047
import java.io.OutputStream;
00048
import java.io.Reader;
00049
import java.io.Writer;
00050
00051
import com.quadcap.util.Util;
00052
00053
00054
00055
00056
00057
00058 public class IO {
00059
00060
00061
00062 public static boolean cmpFile(File a, File b)
throws IOException {
00063 FileInputStream fa = null;
00064 FileInputStream fb = null;
00065
try {
00066 fa =
new FileInputStream(a);
00067 BufferedInputStream sa =
new BufferedInputStream(fa);
00068 fb =
new FileInputStream(b);
00069 BufferedInputStream sb =
new BufferedInputStream(fb);
00070
int ca = sa.read();
00071
int cb = sb.read();
00072
while (ca >= 0 && cb >= 0 && ca == cb) {
00073 ca = sa.read();
00074 cb = sb.read();
00075 }
00076
return ca == cb;
00077 } finally {
00078
if (fa != null)
try { fa.close(); }
catch (IOException e) {}
00079
if (fb != null)
try { fb.close(); }
catch (IOException e) {}
00080 }
00081
00082 }
00083
00084
00085
00086
00087 public static final void copyFile(File from, File to)
throws IOException {
00088 FileOutputStream fos =
new FileOutputStream(to);
00089
try {
00090 FileInputStream fis =
new FileInputStream(from);
00091
try {
00092
copyStream(fis, fos);
00093 } finally {
00094 fis.close();
00095 }
00096 } finally {
00097 fos.close();
00098 }
00099 }
00100
00101
00102
00103
00104 public static final void copyStream(InputStream is, OutputStream os)
00105
throws IOException
00106 {
00107 byte[] buf =
new byte[4096];
00108
int cnt;
00109
while ((cnt = is.read(buf)) > 0) {
00110 os.write(buf, 0, cnt);
00111 }
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121 public static final void copyStream(InputStream is, OutputStream os,
00122
int bps,
int buffered)
00123
throws IOException
00124 {
00125 byte[] buf =
new byte[4096];
00126
int cnt;
00127
int amt = 0;
00128
long interval = (buf.length * 1000) / bps;
00129
long wait = 0;
00130
long start = System.currentTimeMillis();
00131
while ((cnt = is.read(buf)) > 0) {
00132 amt += cnt;
00133
if (wait > 0)
Util.sleep(wait);
00134 os.write(buf, 0, cnt);
00135
long done = System.currentTimeMillis();
00136
if (amt > buffered) {
00137
long elap = done - start;
00138 wait = interval - elap;
00139 }
00140 start = done;
00141 }
00142 }
00143
00144
00145
00146
00147 public static final void copyStream(Reader r, Writer w)
00148
throws IOException
00149 {
00150
char[] buf =
new char[4096];
00151
int cnt;
00152
while ((cnt = r.read(buf)) > 0) {
00153 w.write(buf, 0, cnt);
00154 }
00155 }
00156
00157
00158
00159
00160 public static final void copyStream(InputStream is, OutputStream os,
00161
int limit)
00162
throws IOException
00163 {
00164 byte[] buf =
new byte[4096];
00165
int cnt;
00166
while (limit > buf.length) {
00167
if ((cnt = is.read(buf)) > 0) {
00168 os.write(buf, 0, cnt);
00169 limit -= cnt;
00170 }
else {
00171 limit = 0;
00172 }
00173 }
00174
if ((cnt = is.read(buf, 0, limit)) > 0) {
00175 os.write(buf, 0, cnt);
00176 }
00177 }
00178
00179
00180
00181
00182
00183 public static void write(OutputStream os, String s)
throws IOException {
00184
final int len = s.length();
00185
for (
int i = 0; i < len; i++) os.write(s.charAt(i));
00186 }
00187
00188
00189
00190
00191 public static void readFully(InputStream is, byte[] buf)
00192
throws IOException
00193 {
00194
int pos = 0;
00195
int len = buf.length;
00196
do {
00197
int cnt = is.read(buf, pos, len);
00198
if (cnt <= 0)
throw new IOException(
"unexpected EOF");
00199 len -= cnt;
00200 pos += cnt;
00201 }
while (len > 0);
00202 }
00203
00204
00205
00206
00207 public static void deleteDirectory(File f)
throws IOException {
00208 File[] files = f.listFiles();
00209
if (files != null) {
00210
for (
int i = 0; i < files.length; i++) {
00211 File file = files[i];
00212
if (file.isDirectory()) {
00213 deleteDirectory(f);
00214 }
else {
00215 file.delete();
00216 }
00217 }
00218 }
00219 f.delete();
00220 }
00221
00222 }