1 4 5 package org.jfox.ioc.util; 6 7 import java.io.ByteArrayInputStream ; 8 import java.io.ByteArrayOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.InputStreamReader ; 12 import java.io.OutputStream ; 13 import java.io.OutputStreamWriter ; 14 import java.io.Reader ; 15 import java.io.StringReader ; 16 import java.io.StringWriter ; 17 import java.io.Writer ; 18 19 22 23 public class IOs { 24 27 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 28 29 32 private IOs() { 33 } 34 35 36 40 49 public static void write(byte[] input, Writer output) 50 throws IOException { 51 ByteArrayInputStream in = new ByteArrayInputStream (input); 52 write(in, output); 53 } 54 55 56 67 public static void write(byte[] input, 68 Writer output, 69 String encoding) 70 throws IOException { 71 ByteArrayInputStream in = new ByteArrayInputStream (input); 72 write(in, output, encoding); 73 } 74 75 76 80 88 public static int write(InputStream input, 89 OutputStream output) 90 throws IOException { 91 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 92 int count = 0; 93 int n = 0; 94 while(-1 != (n = input.read(buffer))) { 95 output.write(buffer, 0, n); 96 count += n; 97 } 98 return count; 99 } 100 101 105 113 public static int write(Reader input, 114 Writer output) 115 throws IOException { 116 char[] buffer = new char[DEFAULT_BUFFER_SIZE]; 117 int count = 0; 118 int n = 0; 119 while(-1 != (n = input.read(buffer))) { 120 output.write(buffer, 0, n); 121 count += n; 122 } 123 return count; 124 } 125 126 130 139 public static void write(InputStream input, 140 Writer output) 141 throws IOException { 142 InputStreamReader in = new InputStreamReader (input); 143 write(in, output); 144 } 145 146 157 public static void write(InputStream input, 158 Writer output, 159 String encoding) 160 throws IOException { 161 InputStreamReader in = new InputStreamReader (input, encoding); 162 write(in, output); 163 } 164 165 166 170 178 public static void write(Reader input, 179 OutputStream output) 180 throws IOException { 181 OutputStreamWriter out = new OutputStreamWriter (output); 182 write(input, out); 183 out.flush(); 185 } 186 187 191 199 public static void write(String input, 200 OutputStream output) 201 throws IOException { 202 StringReader in = new StringReader (input); 203 OutputStreamWriter out = new OutputStreamWriter (output); 204 write(in, out); 205 out.flush(); 207 } 208 209 213 220 public static void write(String input, Writer output) 221 throws IOException { 222 output.write(input); 223 } 224 225 226 232 public static void close(Reader input) { 233 if(input == null) { 234 return; 235 } 236 237 try { 238 input.close(); 239 } 240 catch(IOException ioe) { 241 } 242 } 243 244 250 public static void close(Writer output) { 251 if(output == null) { 252 return; 253 } 254 255 try { 256 output.close(); 257 } 258 catch(IOException ioe) { 259 } 260 } 261 262 268 public static void close(OutputStream output) { 269 if(output == null) { 270 return; 271 } 272 273 try { 274 output.close(); 275 } 276 catch(IOException ioe) { 277 } 278 } 279 280 286 public static void close(InputStream input) { 287 if(input == null) { 288 return; 289 } 290 291 try { 292 input.close(); 293 } 294 catch(IOException ioe) { 295 } 296 } 297 298 306 public static String toString(InputStream input) 307 throws IOException { 308 StringWriter sw = new StringWriter (); 309 write(input, sw); 310 return sw.toString(); 311 } 312 313 323 public static String toString(InputStream input, 324 String encoding) 325 throws IOException { 326 StringWriter sw = new StringWriter (); 327 write(input, sw, encoding); 328 return sw.toString(); 329 } 330 331 334 341 public static byte[] toByteArray(InputStream input) 342 throws IOException { 343 ByteArrayOutputStream output = new ByteArrayOutputStream (); 344 write(input, output); 345 return output.toByteArray(); 346 } 347 348 349 354 363 public static String toString(Reader input) 364 throws IOException { 365 StringWriter sw = new StringWriter (); 366 write(input, sw); 367 return sw.toString(); 368 } 369 370 371 380 public static byte[] toByteArray(Reader input) 381 throws IOException { 382 ByteArrayOutputStream output = new ByteArrayOutputStream (); 383 write(input, output); 384 return output.toByteArray(); 385 } 386 387 388 393 394 403 public static byte[] toByteArray(String input) 404 throws IOException { 405 ByteArrayOutputStream output = new ByteArrayOutputStream (); 406 write(input, output); 407 return output.toByteArray(); 408 } 409 410 411 416 419 420 public static void main(String [] args) { 421 422 } 423 } 424 425 | Popular Tags |