1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.util;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.io.Reader;
22 import java.io.Writer;
23
24
25 class StreamUtils {
26 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
27
28
29
30
31
32
33 public static Long copy(InputStream in, OutputStream out)
34 throws IOException {
35 Long count = 0l;
36 byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
37 while (true) {
38 int length = in.read(buf);
39 if (length < 0)
40 break;
41 out.write(buf, 0, length);
42 count = count + length;
43 }
44 return count;
45 }
46
47
48 public static Long copy(Reader in, Writer out) throws IOException {
49 Long count = 0l;
50 char[] buf = new char[DEFAULT_BUFFER_SIZE];
51 while (true) {
52 int length = in.read(buf);
53 if (length < 0)
54 break;
55 out.write(buf, 0, length);
56 count = count + length;
57 }
58 return count;
59 }
60
61 public static void closeQuietly(InputStream in) {
62 if (in != null)
63 try {
64 in.close();
65 } catch (Exception e) {
66
67 }
68 }
69
70 public static void closeQuietly(OutputStream out) {
71 if (out != null)
72 try {
73 out.close();
74 } catch (Exception e) {
75
76 }
77 }
78
79 public static void closeQuietly(Reader in) {
80 if (in != null)
81 try {
82 in.close();
83 } catch (Exception e) {
84
85 }
86 }
87
88 public static void closeQuietly(Writer out) {
89 if (out != null)
90 try {
91 out.close();
92 } catch (Exception e) {
93
94 }
95 }
96 }