IoUtils.java
2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.taover.easyexcel.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* IO Utils
*
* @author Jiaju Zhuang
*/
public class IoUtils {
public static final int EOF = -1;
/**
* The default buffer size ({@value}) to use for
*/
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
private IoUtils() {}
/**
* Gets the contents of an InputStream as a byte[].
*
* @param input
* @return
* @throws IOException
*/
public static byte[] toByteArray(final InputStream input) throws IOException {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
copy(input, output);
return output.toByteArray();
} finally {
output.toByteArray();
}
}
/**
* Gets the contents of an InputStream as a byte[].
*
* @param input
* @param size
* @return
* @throws IOException
*/
public static byte[] toByteArray(final InputStream input, final int size) throws IOException {
if (size < 0) {
throw new IllegalArgumentException("Size must be equal or greater than zero: " + size);
}
if (size == 0) {
return new byte[0];
}
final byte[] data = new byte[size];
int offset = 0;
int read;
while (offset < size && (read = input.read(data, offset, size - offset)) != EOF) {
offset += read;
}
if (offset != size) {
throw new IOException("Unexpected read size. current: " + offset + ", expected: " + size);
}
return data;
}
/**
* Copies bytes
*
* @param input
* @param output
* @return
* @throws IOException
*/
public static int copy(final InputStream input, final OutputStream output) throws IOException {
long count = 0;
int n;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int)count;
}
}