Quadcap Embeddable Server

com/quadcap/io/IO.java

Go to the documentation of this file.
00001 package com.quadcap.io; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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 * @author Stan Bailes 00057 */ 00058 public class IO { 00059 /** 00060 * Diff two files, return true if the file contents are the same 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 * Copy a file 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 * Copy 'is' to 'os' until EOF 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 * Rate limited stream copy. Copy the input stream to the output 00116 * until EOF. An initial series of "buffered" bytes is sent with 00117 * no rate limit, thereafter, bytes are streamed (in 4Kbyte buffers) at 00118 * the rate limit "bps" bytes per/sec. 00119 * No particular effort is made to buffer the incoming stream. 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 * Copy 'r' to 'w' until EOF 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 * Copy 'is' to 'os' until EOF, or <b>limit</b> bytes are copied. 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 * Write a string to an outputstream using the brutal low-byte extraction 00181 * encoding technique. Works great for ISO-8859-1. 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 * Read an inputstream into a byte buffer 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 * Recursively delete an entire directory. Be careful! 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 }