]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/StreamUtils.java
[maven-release-plugin] prepare release argeo-commons-2.1.12
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / StreamUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo;
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 /** Utilities to be used when APache COmmons IO is not available. */
25 public class StreamUtils {
26 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
27
28 /*
29 * APACHE COMMONS IO (inspired)
30 */
31
32 /** @return the number of bytes */
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 /** @return the number of chars */
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
97 /*
98 * APACHE COMMONS CODEC (forked)
99 */
100 /**
101 * Used to build output as Hex
102 */
103 private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5',
104 '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
105
106 /**
107 * Used to build output as Hex
108 */
109 private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5',
110 '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
111
112 /**
113 * Converts an array of bytes into a String representing the hexadecimal
114 * values of each byte in order. The returned String will be double the
115 * length of the passed array, as it takes two characters to represent any
116 * given byte.
117 *
118 * @param data
119 * a byte[] to convert to Hex characters
120 * @return A String containing hexadecimal characters
121 * @since 1.4
122 */
123 public static String encodeHexString(byte[] data) {
124 return new String(encodeHex(data));
125 }
126
127 /**
128 * Converts an array of bytes into an array of characters representing the
129 * hexadecimal values of each byte in order. The returned array will be
130 * double the length of the passed array, as it takes two characters to
131 * represent any given byte.
132 *
133 * @param data
134 * a byte[] to convert to Hex characters
135 * @return A char[] containing hexadecimal characters
136 */
137 public static char[] encodeHex(byte[] data) {
138 return encodeHex(data, true);
139 }
140
141 /**
142 * Converts an array of bytes into an array of characters representing the
143 * hexadecimal values of each byte in order. The returned array will be
144 * double the length of the passed array, as it takes two characters to
145 * represent any given byte.
146 *
147 * @param data
148 * a byte[] to convert to Hex characters
149 * @param toLowerCase
150 * <code>true</code> converts to lowercase, <code>false</code> to
151 * uppercase
152 * @return A char[] containing hexadecimal characters
153 * @since 1.4
154 */
155 public static char[] encodeHex(byte[] data, boolean toLowerCase) {
156 return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
157 }
158
159 /**
160 * Converts an array of bytes into an array of characters representing the
161 * hexadecimal values of each byte in order. The returned array will be
162 * double the length of the passed array, as it takes two characters to
163 * represent any given byte.
164 *
165 * @param data
166 * a byte[] to convert to Hex characters
167 * @param toDigits
168 * the output alphabet
169 * @return A char[] containing hexadecimal characters
170 * @since 1.4
171 */
172 protected static char[] encodeHex(byte[] data, char[] toDigits) {
173 int l = data.length;
174 char[] out = new char[l << 1];
175 // two characters form the hex value.
176 for (int i = 0, j = 0; i < l; i++) {
177 out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
178 out[j++] = toDigits[0x0F & data[i]];
179 }
180 return out;
181 }
182
183 }