1 16 17 package org.springframework.util; 18 19 import java.io.BufferedInputStream ; 20 import java.io.BufferedOutputStream ; 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.io.Reader ; 30 import java.io.StringWriter ; 31 import java.io.Writer ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 47 public abstract class FileCopyUtils { 48 49 private static final Log logger = LogFactory.getLog(FileCopyUtils.class); 50 51 public static final int BUFFER_SIZE = 4096; 52 53 54 58 65 public static int copy(File in, File out) throws IOException { 66 Assert.notNull(in, "No input File specified"); 67 Assert.notNull(out, "No output File specified"); 68 return copy(new BufferedInputStream (new FileInputStream (in)), 69 new BufferedOutputStream (new FileOutputStream (out))); 70 } 71 72 78 public static void copy(byte[] in, File out) throws IOException { 79 Assert.notNull(in, "No input byte array specified"); 80 Assert.notNull(out, "No output File specified"); 81 ByteArrayInputStream inStream = new ByteArrayInputStream (in); 82 OutputStream outStream = new BufferedOutputStream (new FileOutputStream (out)); 83 copy(inStream, outStream); 84 } 85 86 92 public static byte[] copyToByteArray(File in) throws IOException { 93 Assert.notNull(in, "No input File specified"); 94 return copyToByteArray(new BufferedInputStream (new FileInputStream (in))); 95 } 96 97 98 102 110 public static int copy(InputStream in, OutputStream out) throws IOException { 111 Assert.notNull(in, "No InputStream specified"); 112 Assert.notNull(out, "No OutputStream specified"); 113 try { 114 int byteCount = 0; 115 byte[] buffer = new byte[BUFFER_SIZE]; 116 int bytesRead = -1; 117 while ((bytesRead = in.read(buffer)) != -1) { 118 out.write(buffer, 0, bytesRead); 119 byteCount += bytesRead; 120 } 121 out.flush(); 122 return byteCount; 123 } 124 finally { 125 try { 126 in.close(); 127 } 128 catch (IOException ex) { 129 logger.warn("Could not close InputStream", ex); 130 } 131 try { 132 out.close(); 133 } 134 catch (IOException ex) { 135 logger.warn("Could not close OutputStream", ex); 136 } 137 } 138 } 139 140 147 public static void copy(byte[] in, OutputStream out) throws IOException { 148 Assert.notNull(in, "No input byte array specified"); 149 Assert.notNull(out, "No OutputStream specified"); 150 try { 151 out.write(in); 152 } 153 finally { 154 try { 155 out.close(); 156 } 157 catch (IOException ex) { 158 logger.warn("Could not close OutputStream", ex); 159 } 160 } 161 } 162 163 170 public static byte[] copyToByteArray(InputStream in) throws IOException { 171 ByteArrayOutputStream out = new ByteArrayOutputStream (BUFFER_SIZE); 172 copy(in, out); 173 return out.toByteArray(); 174 } 175 176 177 181 189 public static int copy(Reader in, Writer out) throws IOException { 190 Assert.notNull(in, "No Reader specified"); 191 Assert.notNull(out, "No Writer specified"); 192 try { 193 int byteCount = 0; 194 char[] buffer = new char[BUFFER_SIZE]; 195 int bytesRead = -1; 196 while ((bytesRead = in.read(buffer)) != -1) { 197 out.write(buffer, 0, bytesRead); 198 byteCount += bytesRead; 199 } 200 out.flush(); 201 return byteCount; 202 } 203 finally { 204 try { 205 in.close(); 206 } 207 catch (IOException ex) { 208 logger.warn("Could not close Reader", ex); 209 } 210 try { 211 out.close(); 212 } 213 catch (IOException ex) { 214 logger.warn("Could not close Writer", ex); 215 } 216 } 217 } 218 219 226 public static void copy(String in, Writer out) throws IOException { 227 Assert.notNull(in, "No input String specified"); 228 Assert.notNull(out, "No Writer specified"); 229 try { 230 out.write(in); 231 } 232 finally { 233 try { 234 out.close(); 235 } 236 catch (IOException ex) { 237 logger.warn("Could not close Writer", ex); 238 } 239 } 240 } 241 242 249 public static String copyToString(Reader in) throws IOException { 250 StringWriter out = new StringWriter (); 251 copy(in, out); 252 return out.toString(); 253 } 254 255 } 256 | Popular Tags |