1 16 17 package org.apache.log4j.lf5.util; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 24 29 30 32 public abstract class StreamUtils { 33 37 40 public static final int DEFAULT_BUFFER_SIZE = 2048; 41 42 46 50 54 58 63 public static void copy(InputStream input, OutputStream output) 64 throws IOException { 65 copy(input, output, DEFAULT_BUFFER_SIZE); 66 } 67 68 73 public static void copy(InputStream input, 74 OutputStream output, 75 int bufferSize) 76 throws IOException { 77 byte[] buf = new byte[bufferSize]; 78 int bytesRead = input.read(buf); 79 while (bytesRead != -1) { 80 output.write(buf, 0, bytesRead); 81 bytesRead = input.read(buf); 82 } 83 output.flush(); 84 } 85 86 91 public static void copyThenClose(InputStream input, OutputStream output) 92 throws IOException { 93 copy(input, output); 94 input.close(); 95 output.close(); 96 } 97 98 103 public static byte[] getBytes(InputStream input) 104 throws IOException { 105 ByteArrayOutputStream result = new ByteArrayOutputStream (); 106 copy(input, result); 107 result.close(); 108 return result.toByteArray(); 109 } 110 111 115 119 123 } 124 | Popular Tags |