1 16 package org.apache.commons.io; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.io.OutputStream ; 23 import java.io.OutputStreamWriter ; 24 import java.io.Reader ; 25 import java.io.StringReader ; 26 import java.io.Writer ; 27 28 103 public class CopyUtils { 104 105 108 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 109 110 113 public CopyUtils() {} 114 115 119 125 public static void copy(byte[] input, OutputStream output) 126 throws IOException { 127 output.write(input); 128 } 129 130 134 142 public static void copy(byte[] input, Writer output) 143 throws IOException { 144 ByteArrayInputStream in = new ByteArrayInputStream (input); 145 copy(in, output); 146 } 147 148 149 159 public static void copy( 160 byte[] input, 161 Writer output, 162 String encoding) 163 throws IOException { 164 ByteArrayInputStream in = new ByteArrayInputStream (input); 165 copy(in, output, encoding); 166 } 167 168 169 173 180 public static int copy( 181 InputStream input, 182 OutputStream output) 183 throws IOException { 184 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 185 int count = 0; 186 int n = 0; 187 while (-1 != (n = input.read(buffer))) { 188 output.write(buffer, 0, n); 189 count += n; 190 } 191 return count; 192 } 193 194 198 205 public static int copy( 206 Reader input, 207 Writer output) 208 throws IOException { 209 char[] buffer = new char[DEFAULT_BUFFER_SIZE]; 210 int count = 0; 211 int n = 0; 212 while (-1 != (n = input.read(buffer))) { 213 output.write(buffer, 0, n); 214 count += n; 215 } 216 return count; 217 } 218 219 223 231 public static void copy( 232 InputStream input, 233 Writer output) 234 throws IOException { 235 InputStreamReader in = new InputStreamReader (input); 236 copy(in, output); 237 } 238 239 249 public static void copy( 250 InputStream input, 251 Writer output, 252 String encoding) 253 throws IOException { 254 InputStreamReader in = new InputStreamReader (input, encoding); 255 copy(in, output); 256 } 257 258 259 263 270 public static void copy( 271 Reader input, 272 OutputStream output) 273 throws IOException { 274 OutputStreamWriter out = new OutputStreamWriter (output); 275 copy(input, out); 276 out.flush(); 278 } 279 280 284 291 public static void copy( 292 String input, 293 OutputStream output) 294 throws IOException { 295 StringReader in = new StringReader (input); 296 OutputStreamWriter out = new OutputStreamWriter (output); 297 copy(in, out); 298 out.flush(); 300 } 301 302 306 312 public static void copy(String input, Writer output) 313 throws IOException { 314 output.write(input); 315 } 316 317 } | Popular Tags |