1 22 package org.jboss.util.stream; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.io.BufferedInputStream ; 28 import java.io.BufferedOutputStream ; 29 30 import org.jboss.logging.Logger; 31 32 40 public final class Streams 41 { 42 private static final Logger log = Logger.getLogger(Streams.class); 43 44 48 55 public static boolean close(final InputStream stream) { 56 if (stream == null) { 58 return true; 59 } 60 61 boolean success = true; 62 63 try { 64 stream.close(); 65 } 66 catch (IOException e) { 67 success = false; 68 } 69 70 return success; 71 } 72 73 80 public static boolean close(final OutputStream stream) { 81 if (stream == null) { 83 return true; 84 } 85 86 boolean success = true; 87 88 try { 89 stream.close(); 90 } 91 catch (IOException e) { 92 success = false; 93 } 94 95 return success; 96 } 97 98 108 public static boolean close(final Object stream) { 109 boolean success = false; 110 111 if (stream instanceof InputStream ) { 112 success = close((InputStream )stream); 113 } 114 else if (stream instanceof OutputStream ) { 115 success = close((OutputStream )stream); 116 } 117 else { 118 throw new IllegalArgumentException 119 ("stream is not an InputStream or OutputStream"); 120 } 121 122 return success; 123 } 124 125 132 public static boolean close(final InputStream [] streams) { 133 boolean success = true; 134 135 for (int i=0; i<streams.length; i++) { 136 boolean rv = close(streams[i]); 137 if (!rv) success = false; 138 } 139 140 return success; 141 } 142 143 150 public static boolean close(final OutputStream [] streams) { 151 boolean success = true; 152 153 for (int i=0; i<streams.length; i++) { 154 boolean rv = close(streams[i]); 155 if (!rv) success = false; 156 } 157 158 return success; 159 } 160 161 174 public static boolean close(final Object [] streams) { 175 boolean success = true; 176 177 for (int i=0; i<streams.length; i++) { 178 boolean rv = close(streams[i]); 179 if (!rv) success = false; 180 } 181 182 return success; 183 } 184 185 192 public static boolean fclose(final OutputStream stream) { 193 return flush(stream) && close(stream); 194 } 195 196 203 public static boolean fclose(final OutputStream [] streams) { 204 boolean success = true; 205 206 for (int i=0; i<streams.length; i++) { 207 boolean rv = fclose(streams[i]); 208 if (!rv) success = false; 209 } 210 211 return success; 212 } 213 214 215 219 226 public static boolean flush(final OutputStream stream) { 227 if (stream == null) { 229 return true; 230 } 231 232 boolean success = true; 233 234 try { 235 stream.flush(); 236 } 237 catch (IOException e) { 238 success = false; 239 } 240 241 return success; 242 } 243 244 251 public static boolean flush(final OutputStream [] streams) { 252 boolean success = true; 253 254 for (int i=0; i<streams.length; i++) { 255 boolean rv = flush(streams[i]); 256 if (!rv) success = false; 257 } 258 259 return success; 260 } 261 262 263 267 268 public static final int DEFAULT_BUFFER_SIZE = 2048; 269 270 280 public static long copy(final InputStream input, 281 final OutputStream output, 282 final byte buffer[]) 283 throws IOException 284 { 285 long total = 0; 286 int read; 287 288 boolean trace = log.isTraceEnabled(); 289 if (trace) { 290 log.trace("copying " + input + " to " + output + " with buffer size: " + buffer.length); 291 } 292 293 while ((read = input.read(buffer)) != -1) { 294 output.write(buffer, 0, read); 295 total += read; 296 297 if (trace) { 298 log.trace("bytes read: " + read + "; total bytes read: " + total); 299 } 300 } 301 302 return total; 303 } 304 305 315 public static long copy(final InputStream input, 316 final OutputStream output, 317 final int size) 318 throws IOException 319 { 320 return copy(input, output, new byte[size]); 321 } 322 323 332 public static long copy(final InputStream input, 333 final OutputStream output) 334 throws IOException 335 { 336 return copy(input, output, DEFAULT_BUFFER_SIZE); 337 } 338 339 349 public static long copyb(InputStream input, 350 OutputStream output) 351 throws IOException 352 { 353 if (!(input instanceof BufferedInputStream )) { 354 input = new BufferedInputStream (input); 355 } 356 357 if (!(output instanceof BufferedOutputStream )) { 358 output = new BufferedOutputStream (output); 359 } 360 361 long bytes = copy(input, output, DEFAULT_BUFFER_SIZE); 362 363 output.flush(); 364 365 return bytes; 366 } 367 368 380 public static long copySome(final InputStream input, 381 final OutputStream output, 382 final byte buffer[], 383 final long length) 384 throws IOException 385 { 386 long total = 0; 387 int read; 388 int readLength; 389 390 boolean trace = log.isTraceEnabled(); 391 392 readLength = Math.min((int)length, buffer.length); 395 if (trace) { 396 log.trace("initial read length: " + readLength); 397 } 398 399 while (readLength != 0 && (read = input.read(buffer, 0, readLength)) != -1) 400 { 401 if (trace) log.trace("read bytes: " + read); 402 output.write(buffer, 0, read); 403 total += read; 404 if (trace) log.trace("total bytes read: " + total); 405 406 readLength = Math.min((int)(length - total), buffer.length); 408 if (trace) log.trace("next read length: " + readLength); 409 } 410 411 return total; 412 } 413 414 426 public static long copySome(final InputStream input, 427 final OutputStream output, 428 final int size, 429 final long length) 430 throws IOException 431 { 432 return copySome(input, output, new byte[size], length); 433 } 434 435 446 public static long copySome(final InputStream input, 447 final OutputStream output, 448 final long length) 449 throws IOException 450 { 451 return copySome(input, output, DEFAULT_BUFFER_SIZE, length); 452 } 453 } 454 | Popular Tags |