3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.io
.OutputStream
;
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;
14 * APACHE COMMONS IO (inspired)
17 /** @return the number of bytes */
18 public static Long
copy(InputStream in
, OutputStream out
)
21 byte[] buf
= new byte[DEFAULT_BUFFER_SIZE
];
23 int length
= in
.read(buf
);
26 out
.write(buf
, 0, length
);
27 count
= count
+ length
;
32 /** @return the number of chars */
33 public static Long
copy(Reader in
, Writer out
) throws IOException
{
35 char[] buf
= new char[DEFAULT_BUFFER_SIZE
];
37 int length
= in
.read(buf
);
40 out
.write(buf
, 0, length
);
41 count
= count
+ length
;
46 public static void closeQuietly(InputStream in
) {
50 } catch (Exception e
) {
55 public static void closeQuietly(OutputStream out
) {
59 } catch (Exception e
) {
64 public static void closeQuietly(Reader in
) {
68 } catch (Exception e
) {
73 public static void closeQuietly(Writer out
) {
77 } catch (Exception e
) {
83 * APACHE COMMONS CODEC (forked)
86 * Used to build output as Hex
88 private static final char[] DIGITS_LOWER
= { '0', '1', '2', '3', '4', '5',
89 '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
92 * Used to build output as Hex
94 private static final char[] DIGITS_UPPER
= { '0', '1', '2', '3', '4', '5',
95 '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
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
104 * a byte[] to convert to Hex characters
105 * @return A String containing hexadecimal characters
108 public static String
encodeHexString(byte[] data
) {
109 return new String(encodeHex(data
));
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.
119 * a byte[] to convert to Hex characters
120 * @return A char[] containing hexadecimal characters
122 public static char[] encodeHex(byte[] data
) {
123 return encodeHex(data
, true);
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.
133 * a byte[] to convert to Hex characters
135 * <code>true</code> converts to lowercase, <code>false</code> to
137 * @return A char[] containing hexadecimal characters
140 public static char[] encodeHex(byte[] data
, boolean toLowerCase
) {
141 return encodeHex(data
, toLowerCase ? DIGITS_LOWER
: DIGITS_UPPER
);
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.
151 * a byte[] to convert to Hex characters
153 * the output alphabet
154 * @return A char[] containing hexadecimal characters
157 protected static char[] encodeHex(byte[] data
, char[] toDigits
) {
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
]];