-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathStreamUtil.java
More file actions
53 lines (43 loc) · 1.45 KB
/
StreamUtil.java
File metadata and controls
53 lines (43 loc) · 1.45 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
package at.tomtasche.reader.background;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamUtil {
public static final String ENCODING = "UTF-8";
public static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
copy(in, dst);
}
public static void copy(File src, OutputStream out) throws IOException {
InputStream in = new FileInputStream(src);
copy(in, out);
}
public static void copy(InputStream in, File dst) throws IOException {
OutputStream out = new FileOutputStream(dst);
copy(in, out);
out.close();
}
public static String readFully(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(in, out);
return out.toString(ENCODING);
}
// taken from: https://stackoverflow.com/a/9293885/198996
public static void copy(InputStream in, OutputStream out) throws IOException {
try {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.flush();
} finally {
in.close();
}
}
}