1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.ochafik.io;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.Reader;
25 import java.io.Writer;
26
27 import com.ochafik.util.progress.ProgressModel;
28
29
30 public class IOUtils {
31
32 public static final long readWrite(InputStream in, OutputStream out) throws IOException {
33 byte[] b=new byte[4096];
34 int len;
35 long total=0;
36 while ((len=in.read(b))>0) {
37 out.write(b, 0, len);
38 total+=len;
39 }
40 return total;
41 }
42
43 public static final long readWrite(InputStream in, OutputStream out, int maxLen) throws IOException {
44 int bLen = 4096;
45 byte[] b=new byte[bLen];
46 int len;
47 long total=0;
48
49 int allowed = bLen <= maxLen ? bLen : maxLen;
50 while (allowed > 0 && (len=in.read(b, 0, allowed)) > 0) {
51 out.write(b, 0, len);
52 total+=len;
53 maxLen -= len;
54 allowed = bLen <= maxLen ? bLen : maxLen;
55 }
56 return total;
57 }
58
59
60 public static final long readWrite(InputStream in, OutputStream out, ProgressModel progressModel) throws IOException {
61 byte[] b=new byte[1024];
62 int len;
63 long total=0;
64 while ((len=in.read(b))>0) {
65 out.write(b, 0, len);
66 total+=len;
67 progressModel.addProgress(len);
68 }
69 return total;
70 }
71
72 public static void readWrite(Reader in, Writer out) throws IOException {
73 char[] b = new char[1024];
74 int len;
75 long total=0;
76 while ((len = in.read(b))>0) {
77 out.write(b, 0, len);
78 total+=len;
79 }
80 }
81
82 }