]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.enterprise/src/org/argeo/util/StreamUtils.java
[maven-release-plugin] prepare release v2.3.4
[lgpl/argeo-commons.git] / org.argeo.enterprise / src / org / argeo / util / StreamUtils.java
1 package org.argeo.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.io.Reader;
7 import java.io.Writer;
8
9 /** Utilities to be used when Apache Commons IO is not available. */
10 class StreamUtils {
11 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
12
13 /*
14 * APACHE COMMONS IO (inspired)
15 */
16
17 /** @return the number of bytes */
18 public static Long copy(InputStream in, OutputStream out)
19 throws IOException {
20 Long count = 0l;
21 byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
22 while (true) {
23 int length = in.read(buf);
24 if (length < 0)
25 break;
26 out.write(buf, 0, length);
27 count = count + length;
28 }
29 return count;
30 }
31
32 /** @return the number of chars */
33 public static Long copy(Reader in, Writer out) throws IOException {
34 Long count = 0l;
35 char[] buf = new char[DEFAULT_BUFFER_SIZE];
36 while (true) {
37 int length = in.read(buf);
38 if (length < 0)
39 break;
40 out.write(buf, 0, length);
41 count = count + length;
42 }
43 return count;
44 }
45
46 public static void closeQuietly(InputStream in) {
47 if (in != null)
48 try {
49 in.close();
50 } catch (Exception e) {
51 //
52 }
53 }
54
55 public static void closeQuietly(OutputStream out) {
56 if (out != null)
57 try {
58 out.close();
59 } catch (Exception e) {
60 //
61 }
62 }
63
64 public static void closeQuietly(Reader in) {
65 if (in != null)
66 try {
67 in.close();
68 } catch (Exception e) {
69 //
70 }
71 }
72
73 public static void closeQuietly(Writer out) {
74 if (out != null)
75 try {
76 out.close();
77 } catch (Exception e) {
78 //
79 }
80 }
81 }