1 16 package org.apache.commons.io; 17 18 import java.io.BufferedInputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.io.Reader ; 23 import java.io.StringWriter ; 24 import java.io.Writer ; 25 26 import org.apache.commons.io.output.ByteArrayOutputStream; 27 28 45 public final class IOUtils 46 { 47 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 48 49 52 public IOUtils() {} 53 54 60 public static void closeQuietly( Reader input ) 61 { 62 if( input == null ) 63 { 64 return; 65 } 66 67 try 68 { 69 input.close(); 70 } 71 catch( IOException ioe ) 72 { 73 } 74 } 75 76 82 public static void closeQuietly( Writer output ) 83 { 84 if( output == null ) 85 { 86 return; 87 } 88 89 try 90 { 91 output.close(); 92 } 93 catch( IOException ioe ) 94 { 95 } 96 } 97 98 103 public static void closeQuietly( OutputStream output ) 104 { 105 if( output == null ) 106 { 107 return; 108 } 109 110 try 111 { 112 output.close(); 113 } 114 catch( IOException ioe ) 115 { 116 } 117 } 118 119 124 public static void closeQuietly( InputStream input ) 125 { 126 if( input == null ) 127 { 128 return; 129 } 130 131 try 132 { 133 input.close(); 134 } 135 catch( IOException ioe ) 136 { 137 } 138 } 139 140 147 public static String toString( InputStream input ) 148 throws IOException 149 { 150 StringWriter sw = new StringWriter (); 151 CopyUtils.copy( input, sw ); 152 return sw.toString(); 153 } 154 155 164 public static String toString( InputStream input, 165 String encoding ) 166 throws IOException 167 { 168 StringWriter sw = new StringWriter (); 169 CopyUtils.copy( input, sw, encoding ); 170 return sw.toString(); 171 } 172 173 176 182 public static byte[] toByteArray( InputStream input ) 183 throws IOException 184 { 185 ByteArrayOutputStream output = new ByteArrayOutputStream(); 186 CopyUtils.copy( input, output ); 187 return output.toByteArray(); 188 } 189 190 191 196 204 public static String toString( Reader input ) 205 throws IOException 206 { 207 StringWriter sw = new StringWriter (); 208 CopyUtils.copy( input, sw ); 209 return sw.toString(); 210 } 211 212 213 221 public static byte[] toByteArray( Reader input ) 222 throws IOException 223 { 224 ByteArrayOutputStream output = new ByteArrayOutputStream(); 225 CopyUtils.copy( input, output ); 226 return output.toByteArray(); 227 } 228 229 230 235 236 244 public static byte[] toByteArray( String input ) 245 throws IOException 246 { 247 ByteArrayOutputStream output = new ByteArrayOutputStream(); 248 CopyUtils.copy( input, output ); 249 return output.toByteArray(); 250 } 251 252 253 258 261 268 public static String toString( byte[] input ) 269 throws IOException 270 { 271 StringWriter sw = new StringWriter (); 272 CopyUtils.copy( input, sw ); 273 return sw.toString(); 274 } 275 276 277 286 public static String toString( byte[] input, 287 String encoding ) 288 throws IOException 289 { 290 StringWriter sw = new StringWriter (); 291 CopyUtils.copy( input, sw, encoding ); 292 return sw.toString(); 293 } 294 295 296 304 public static boolean contentEquals( InputStream input1, 305 InputStream input2 ) 306 throws IOException 307 { 308 InputStream bufferedInput1 = new BufferedInputStream ( input1 ); 309 InputStream bufferedInput2 = new BufferedInputStream ( input2 ); 310 311 int ch = bufferedInput1.read(); 312 while( -1 != ch ) 313 { 314 int ch2 = bufferedInput2.read(); 315 if( ch != ch2 ) 316 { 317 return false; 318 } 319 ch = bufferedInput1.read(); 320 } 321 322 int ch2 = bufferedInput2.read(); 323 if( -1 != ch2 ) 324 { 325 return false; 326 } 327 else 328 { 329 return true; 330 } 331 } 332 } 333 | Popular Tags |