View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU General Public License for more details.
15  	
16  	You should have received a copy of the GNU General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
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  }