1 package com.quadcap.io; 2 3 40 41 import java.io.BufferedInputStream ; 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.FileOutputStream ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.io.OutputStream ; 48 import java.io.Reader ; 49 import java.io.Writer ; 50 51 import com.quadcap.util.Util; 52 53 58 public class IO { 59 62 public static boolean cmpFile(File a, File b) throws IOException { 63 FileInputStream fa = null; 64 FileInputStream fb = null; 65 try { 66 fa = new FileInputStream (a); 67 BufferedInputStream sa = new BufferedInputStream (fa); 68 fb = new FileInputStream (b); 69 BufferedInputStream sb = new BufferedInputStream (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 e) {} 79 if (fb != null) try { fb.close(); } catch (IOException e) {} 80 } 81 82 } 83 84 87 public static final void copyFile(File from, File to) throws IOException { 88 FileOutputStream fos = new FileOutputStream (to); 89 try { 90 FileInputStream fis = new FileInputStream (from); 91 try { 92 copyStream(fis, fos); 93 } finally { 94 fis.close(); 95 } 96 } finally { 97 fos.close(); 98 } 99 } 100 101 104 public static final void copyStream(InputStream is, OutputStream os) 105 throws IOException 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 121 public static final void copyStream(InputStream is, OutputStream os, 122 int bps, int buffered) 123 throws IOException 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 147 public static final void copyStream(Reader r, Writer w) 148 throws IOException 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 160 public static final void copyStream(InputStream is, OutputStream os, 161 int limit) 162 throws IOException 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 183 public static void write(OutputStream os, String s) throws IOException { 184 final int len = s.length(); 185 for (int i = 0; i < len; i++) os.write(s.charAt(i)); 186 } 187 188 191 public static void readFully(InputStream is, byte[] buf) 192 throws IOException 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 ("unexpected EOF"); 199 len -= cnt; 200 pos += cnt; 201 } while (len > 0); 202 } 203 204 207 public static void deleteDirectory(File f) throws IOException { 208 File [] files = f.listFiles(); 209 if (files != null) { 210 for (int i = 0; i < files.length; i++) { 211 File 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 |