]> git.argeo.org Git - lgpl/argeo-commons.git/blob - basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/StreamUtils.java
Deal with null values
[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 /*
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
82 /*
83 * APACHE COMMONS CODEC (forked)
84 */
85 /**
86 * Used to build output as Hex
87 */
88 private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5',
89 '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
90
91 /**
92 * Used to build output as Hex
93 */
94 private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5',
95 '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
96
97 /**
98 * Converts an array of bytes into a String representing the hexadecimal
99 * values of each byte in order. The returned String will be double the
100 * length of the passed array, as it takes two characters to represent any
101 * given byte.
102 *
103 * @param data
104 * a byte[] to convert to Hex characters
105 * @return A String containing hexadecimal characters
106 * @since 1.4
107 */
108 public static String encodeHexString(byte[] data) {
109 return new String(encodeHex(data));
110 }
111
112 /**
113 * Converts an array of bytes into an array of characters representing the
114 * hexadecimal values of each byte in order. The returned array will be
115 * double the length of the passed array, as it takes two characters to
116 * represent any given byte.
117 *
118 * @param data
119 * a byte[] to convert to Hex characters
120 * @return A char[] containing hexadecimal characters
121 */
122 public static char[] encodeHex(byte[] data) {
123 return encodeHex(data, true);
124 }
125
126 /**
127 * Converts an array of bytes into an array of characters representing the
128 * hexadecimal values of each byte in order. The returned array will be
129 * double the length of the passed array, as it takes two characters to
130 * represent any given byte.
131 *
132 * @param data
133 * a byte[] to convert to Hex characters
134 * @param toLowerCase
135 * <code>true</code> converts to lowercase, <code>false</code> to
136 * uppercase
137 * @return A char[] containing hexadecimal characters
138 * @since 1.4
139 */
140 public static char[] encodeHex(byte[] data, boolean toLowerCase) {
141 return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
142 }
143
144 /**
145 * Converts an array of bytes into an array of characters representing the
146 * hexadecimal values of each byte in order. The returned array will be
147 * double the length of the passed array, as it takes two characters to
148 * represent any given byte.
149 *
150 * @param data
151 * a byte[] to convert to Hex characters
152 * @param toDigits
153 * the output alphabet
154 * @return A char[] containing hexadecimal characters
155 * @since 1.4
156 */
157 protected static char[] encodeHex(byte[] data, char[] toDigits) {
158 int l = data.length;
159 char[] out = new char[l << 1];
160 // two characters form the hex value.
161 for (int i = 0, j = 0; i < l; i++) {
162 out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
163 out[j++] = toDigits[0x0F & data[i]];
164 }
165 return out;
166 }
167
168 }