]> git.argeo.org Git - lgpl/argeo-commons.git/blob - basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/StreamUtils.java
Introduce simple principal
[lgpl/argeo-commons.git] / basic / runtime / org.argeo.basic.nodeps / src / main / java / org / argeo / StreamUtils.java
1 package org.argeo;
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 public class StreamUtils {
11 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
12
13 /** @return the number of bytes */
14 public static Long copy(InputStream in, OutputStream out)
15 throws IOException {
16 Long count = 0l;
17 byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
18 while (true) {
19 int length = in.read(buf);
20 if (length < 0)
21 break;
22 out.write(buf, 0, length);
23 count = count + length;
24 }
25 return count;
26 }
27
28 /** @return the number of chars */
29 public static Long copy(Reader in, Writer out) throws IOException {
30 Long count = 0l;
31 char[] buf = new char[DEFAULT_BUFFER_SIZE];
32 while (true) {
33 int length = in.read(buf);
34 if (length < 0)
35 break;
36 out.write(buf, 0, length);
37 count = count + length;
38 }
39 return count;
40 }
41
42 public static void closeQuietly(InputStream in) {
43 if (in != null)
44 try {
45 in.close();
46 } catch (Exception e) {
47 //
48 }
49 }
50
51 public static void closeQuietly(OutputStream out) {
52 if (out != null)
53 try {
54 out.close();
55 } catch (Exception e) {
56 //
57 }
58 }
59
60 public static void closeQuietly(Reader in) {
61 if (in != null)
62 try {
63 in.close();
64 } catch (Exception e) {
65 //
66 }
67 }
68
69 public static void closeQuietly(Writer out) {
70 if (out != null)
71 try {
72 out.close();
73 } catch (Exception e) {
74 //
75 }
76 }
77
78 private StreamUtils() {
79
80 }
81
82 }