]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/StreamUtils.java
Deal with corner case of empty localised strings
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / StreamUtils.java
1 package org.argeo.cms.util;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.io.Reader;
9 import java.io.UncheckedIOException;
10 import java.io.Writer;
11 import java.nio.charset.Charset;
12 import java.nio.charset.StandardCharsets;
13 import java.util.StringJoiner;
14
15 /** Stream utilities to be used when Apache Commons IO is not available. */
16 public class StreamUtils {
17 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
18
19 /*
20 * APACHE COMMONS IO (inspired)
21 */
22
23 /** @return the number of bytes */
24 public static Long copy(InputStream in, OutputStream out) throws IOException {
25 Long count = 0l;
26 byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
27 while (true) {
28 int length = in.read(buf);
29 if (length < 0)
30 break;
31 out.write(buf, 0, length);
32 count = count + length;
33 }
34 return count;
35 }
36
37 /** @return the number of chars */
38 public static Long copy(Reader in, Writer out) throws IOException {
39 Long count = 0l;
40 char[] buf = new char[DEFAULT_BUFFER_SIZE];
41 while (true) {
42 int length = in.read(buf);
43 if (length < 0)
44 break;
45 out.write(buf, 0, length);
46 count = count + length;
47 }
48 return count;
49 }
50
51 public static byte[] toByteArray(InputStream in) throws IOException {
52 try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
53 copy(in, out);
54 return out.toByteArray();
55 }
56 }
57
58 public static void closeQuietly(InputStream in) {
59 if (in != null)
60 try {
61 in.close();
62 } catch (Exception e) {
63 //
64 }
65 }
66
67 public static void closeQuietly(OutputStream out) {
68 if (out != null)
69 try {
70 out.close();
71 } catch (Exception e) {
72 //
73 }
74 }
75
76 public static void closeQuietly(Reader in) {
77 if (in != null)
78 try {
79 in.close();
80 } catch (Exception e) {
81 //
82 }
83 }
84
85 public static void closeQuietly(Writer out) {
86 if (out != null)
87 try {
88 out.close();
89 } catch (Exception e) {
90 //
91 }
92 }
93
94 public static String toString(Class<?> clss, String resource) {
95 return toString(clss.getResourceAsStream(resource), StandardCharsets.UTF_8);
96 }
97
98 public static String toString(InputStream in) {
99 return toString(in, StandardCharsets.UTF_8);
100 }
101
102 public static String toString(InputStream in, Charset encoding) {
103 try {
104 return new String(in.readAllBytes(), encoding);
105 } catch (IOException e) {
106 throw new UncheckedIOException(e);
107 }
108 }
109
110 public static String toString(BufferedReader reader) throws IOException {
111 StringJoiner sn = new StringJoiner("\n");
112 String line = null;
113 while ((line = reader.readLine()) != null)
114 sn.add(line);
115 return sn.toString();
116 }
117 }