1 3 package jodd.io; 4 5 import java.io.*; 6 7 10 public class StreamUtil { 11 12 15 public static int BUFFER_SIZE = 32768; 16 17 18 20 24 public static void close(InputStream in) { 25 if (in != null) { 26 try { 27 in.close(); 28 } catch (IOException ioe) { 29 } 31 } 32 } 33 34 38 public static void close(OutputStream out) { 39 if (out != null) { 40 try { 41 out.close(); 42 } catch (IOException ioe) { 43 } 45 } 46 } 47 48 53 public static void close(Reader in) { 54 if (in != null) { 55 try { 56 in.close(); 57 } catch (IOException ioe) { 58 } 60 } 61 } 62 63 68 public static void close(Writer out) { 69 if (out != null) { 70 try { 71 out.close(); 72 } catch (IOException ioe) { 73 } 75 } 76 } 77 78 79 81 84 public static int copy(InputStream input, OutputStream output) throws IOException { 85 byte[] buffer = new byte[BUFFER_SIZE]; 86 int count = 0; 87 int read; 88 while (true) { 89 read = input.read(buffer, 0, BUFFER_SIZE); 90 if (read == -1) { 91 break; 92 } 93 output.write(buffer, 0, read); 94 count += read; 95 } 96 return count; 97 } 98 101 public static int copy(InputStream input, OutputStream output, int byteCount) throws IOException { 102 byte buffer[] = new byte[BUFFER_SIZE]; 103 int count = 0; 104 int read; 105 while (byteCount > 0) { 106 if (byteCount < BUFFER_SIZE) { 107 read = input.read(buffer, 0, byteCount); 108 } else { 109 read = input.read(buffer, 0, BUFFER_SIZE); 110 } 111 if (read == -1) { 112 break; 113 } 114 byteCount -= read; 115 count += read; 116 output.write(buffer, 0, read); 117 } 118 return count; 119 } 120 121 122 123 124 127 public static void copy(InputStream input, Writer output) throws IOException { 128 copy(new InputStreamReader(input), output); 129 } 130 133 public static void copy(InputStream input, Writer output, int byteCount) throws IOException { 134 copy(new InputStreamReader(input), output, byteCount); 135 } 136 139 public static void copy(InputStream input, Writer output, String encoding) throws IOException { 140 if (encoding == null) { 141 copy(input, output); 142 } else { 143 copy(new InputStreamReader(input, encoding), output); 144 } 145 } 146 149 public static void copy(InputStream input, Writer output, String encoding, int byteCount) throws IOException { 150 if (encoding == null) { 151 copy(input, output, byteCount); 152 } else { 153 copy(new InputStreamReader(input, encoding), output, byteCount); 154 } 155 } 156 157 158 159 162 public static int copy(Reader input, Writer output) throws IOException { 163 char[] buffer = new char[BUFFER_SIZE]; 164 int count = 0; 165 int read; 166 while ((read = input.read(buffer, 0, BUFFER_SIZE)) >= 0) { 167 output.write(buffer, 0, read); 168 count += read; 169 } 170 output.flush(); 171 return count; 172 } 173 176 public static int copy(Reader input, Writer output, int charCount) throws IOException { 177 char buffer[] = new char[BUFFER_SIZE]; 178 int count = 0; 179 int read; 180 while (charCount > 0) { 181 if (charCount < BUFFER_SIZE) { 182 read = input.read(buffer, 0, charCount); 183 } else { 184 read = input.read(buffer, 0, BUFFER_SIZE); 185 } 186 if (read == -1) { 187 break; 188 } 189 charCount -= read; 190 count += read; 191 output.write(buffer, 0, read); 192 } 193 return count; 194 } 195 196 197 198 201 public static void copy(Reader input, OutputStream output) throws IOException { 202 Writer out = new OutputStreamWriter(output); 203 copy(input, out); 204 out.flush(); 205 } 206 209 public static void copy(Reader input, OutputStream output, int charCount) throws IOException { 210 Writer out = new OutputStreamWriter(output); 211 copy(input, out, charCount); 212 out.flush(); 213 } 214 217 public static void copy(Reader input, OutputStream output, String encoding) throws IOException { 218 if (encoding == null) { 219 copy(input, output); 220 } else { 221 Writer out = new OutputStreamWriter(output); 222 copy(input, out); 223 out.flush(); 224 } 225 } 226 229 public static void copy(Reader input, OutputStream output, String encoding, int charCount) throws IOException { 230 if (encoding == null) { 231 copy(input, output, charCount); 232 } else { 233 Writer out = new OutputStreamWriter(output); 234 copy(input, out, charCount); 235 out.flush(); 236 } 237 } 238 239 240 242 249 public static byte[] readAvailableBytes(InputStream in) throws IOException { 250 int l = in.available(); 251 byte byteArray[] = new byte[l]; 252 int i = 0, j; 253 while ((i < l) && (j = in.read(byteArray, i, l - i)) >= 0) { 254 i +=j; 255 } 256 if (i < l) { 257 throw new IOException("Could not completely read from input stream."); 258 } 259 return byteArray; 260 } 261 262 public static byte[] readBytes(InputStream input) throws IOException { 263 FastByteArrayOutputStream output = new FastByteArrayOutputStream(); 264 copy(input, output); 265 return output.toByteArray(); 266 } 267 public static byte[] readBytes(InputStream input, int byteCount) throws IOException { 268 FastByteArrayOutputStream output = new FastByteArrayOutputStream(); 269 copy(input, output, byteCount); 270 return output.toByteArray(); 271 } 272 273 public static byte[] readBytes(Reader input) throws IOException { 274 FastByteArrayOutputStream output = new FastByteArrayOutputStream(); 275 copy(input, output); 276 return output.toByteArray(); 277 } 278 public static byte[] readBytes(Reader input, int byteCount) throws IOException { 279 FastByteArrayOutputStream output = new FastByteArrayOutputStream(); 280 copy(input, output, byteCount); 281 return output.toByteArray(); 282 } 283 public static byte[] readBytes(Reader input, String encoding) throws IOException { 284 FastByteArrayOutputStream output = new FastByteArrayOutputStream(); 285 copy(input, output, encoding); 286 return output.toByteArray(); 287 } 288 public static byte[] readBytes(Reader input, String encoding, int byteCount) throws IOException { 289 FastByteArrayOutputStream output = new FastByteArrayOutputStream(); 290 copy(input, output, encoding, byteCount); 291 return output.toByteArray(); 292 } 293 294 296 public static char[] readChars(InputStream input) throws IOException { 297 FastCharArrayWriter output = new FastCharArrayWriter(); 298 copy(input, output); 299 return output.toCharArray(); 300 } 301 public static char[] readChars(InputStream input, int charCount) throws IOException { 302 FastCharArrayWriter output = new FastCharArrayWriter(); 303 copy(input, output, charCount); 304 return output.toCharArray(); 305 } 306 307 public static char[] readChars(InputStream input, String encoding) throws IOException { 308 FastCharArrayWriter output = new FastCharArrayWriter(); 309 copy(input, output, encoding); 310 return output.toCharArray(); 311 } 312 public static char[] readChars(InputStream input, String encoding, int charCount) throws IOException { 313 FastCharArrayWriter output = new FastCharArrayWriter(); 314 copy(input, output, encoding, charCount); 315 return output.toCharArray(); 316 } 317 318 public static char[] readChars(Reader input) throws IOException { 319 FastCharArrayWriter output = new FastCharArrayWriter(); 320 copy(input, output); 321 return output.toCharArray(); 322 } 323 public static char[] readChars(Reader input, int charCount) throws IOException { 324 FastCharArrayWriter output = new FastCharArrayWriter(); 325 copy(input, output, charCount); 326 return output.toCharArray(); 327 } 328 329 330 332 338 public static boolean compare(InputStream input1, InputStream input2) throws IOException { 339 if (!(input1 instanceof BufferedInputStream)) { 340 input1 = new BufferedInputStream(input1); 341 } 342 if (!(input2 instanceof BufferedInputStream)) { 343 input2 = new BufferedInputStream(input2); 344 } 345 int ch = input1.read(); 346 while (ch != -1) { 347 int ch2 = input2.read(); 348 if (ch != ch2) { 349 return false; 350 } 351 ch = input1.read(); 352 } 353 int ch2 = input2.read(); 354 return (ch2 == -1); 355 } 356 362 public static boolean compare(Reader input1, Reader input2) throws IOException { 363 if (!(input1 instanceof BufferedReader)) { 364 input1 = new BufferedReader(input1); 365 } 366 if (!(input2 instanceof BufferedReader)) { 367 input2 = new BufferedReader(input2); 368 } 369 370 int ch = input1.read(); 371 while (ch != -1) { 372 int ch2 = input2.read(); 373 if (ch != ch2) { 374 return false; 375 } 376 ch = input1.read(); 377 } 378 int ch2 = input2.read(); 379 return (ch2 == -1); 380 } 381 382 } 383 | Popular Tags |