2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
;
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;
29 * APACHE COMMONS IO (inspired)
32 /** @return the number of bytes */
33 public static Long
copy(InputStream in
, OutputStream out
)
36 byte[] buf
= new byte[DEFAULT_BUFFER_SIZE
];
38 int length
= in
.read(buf
);
41 out
.write(buf
, 0, length
);
42 count
= count
+ length
;
47 /** @return the number of chars */
48 public static Long
copy(Reader in
, Writer out
) throws IOException
{
50 char[] buf
= new char[DEFAULT_BUFFER_SIZE
];
52 int length
= in
.read(buf
);
55 out
.write(buf
, 0, length
);
56 count
= count
+ length
;
61 public static void closeQuietly(InputStream in
) {
65 } catch (Exception e
) {
70 public static void closeQuietly(OutputStream out
) {
74 } catch (Exception e
) {
79 public static void closeQuietly(Reader in
) {
83 } catch (Exception e
) {
88 public static void closeQuietly(Writer out
) {
92 } catch (Exception e
) {
98 * APACHE COMMONS CODEC (forked)
101 * Used to build output as Hex
103 private static final char[] DIGITS_LOWER
= { '0', '1', '2', '3', '4', '5',
104 '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
107 * Used to build output as Hex
109 private static final char[] DIGITS_UPPER
= { '0', '1', '2', '3', '4', '5',
110 '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
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
119 * a byte[] to convert to Hex characters
120 * @return A String containing hexadecimal characters
123 public static String
encodeHexString(byte[] data
) {
124 return new String(encodeHex(data
));
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.
134 * a byte[] to convert to Hex characters
135 * @return A char[] containing hexadecimal characters
137 public static char[] encodeHex(byte[] data
) {
138 return encodeHex(data
, true);
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.
148 * a byte[] to convert to Hex characters
150 * <code>true</code> converts to lowercase, <code>false</code> to
152 * @return A char[] containing hexadecimal characters
155 public static char[] encodeHex(byte[] data
, boolean toLowerCase
) {
156 return encodeHex(data
, toLowerCase ? DIGITS_LOWER
: DIGITS_UPPER
);
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.
166 * a byte[] to convert to Hex characters
168 * the output alphabet
169 * @return A char[] containing hexadecimal characters
172 protected static char[] encodeHex(byte[] data
, char[] toDigits
) {
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
]];