001    /*
002            Copyright (c) 2009 Olivier Chafik, All Rights Reserved
003            
004            This file is part of JNAerator (http://jnaerator.googlecode.com/).
005            
006            JNAerator is free software: you can redistribute it and/or modify
007            it under the terms of the GNU General Public License as published by
008            the Free Software Foundation, either version 3 of the License, or
009            (at your option) any later version.
010            
011            JNAerator is distributed in the hope that it will be useful,
012            but WITHOUT ANY WARRANTY; without even the implied warranty of
013            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014            GNU General Public License for more details.
015            
016            You should have received a copy of the GNU General Public License
017            along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
018    */
019    package com.ochafik.io;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.OutputStream;
024    import java.io.Reader;
025    import java.io.Writer;
026    
027    import com.ochafik.util.progress.ProgressModel;
028    
029    
030    public class IOUtils {
031    
032            public static final long readWrite(InputStream in, OutputStream out) throws IOException {
033                    byte[] b=new byte[4096];
034                    int len;
035                    long total=0;
036                    while ((len=in.read(b))>0) {
037                            out.write(b, 0, len);
038                            total+=len;
039                    }
040                    return total;
041            }
042            
043            public static final long readWrite(InputStream in, OutputStream out, int maxLen) throws IOException {
044                    int bLen = 4096;
045                    byte[] b=new byte[bLen];
046                    int len;
047                    long total=0;
048                    
049                    int allowed = bLen <= maxLen ? bLen : maxLen;  
050                    while (allowed > 0 && (len=in.read(b, 0, allowed)) > 0) {
051                            out.write(b, 0, len);
052                            total+=len;
053                            maxLen -= len;
054                            allowed = bLen <= maxLen ? bLen : maxLen;
055                    }
056                    return total;
057            }
058            
059    
060            public static final long readWrite(InputStream in, OutputStream out, ProgressModel progressModel) throws IOException {
061                    byte[] b=new byte[1024];
062                    int len;
063                    long total=0;
064                    while ((len=in.read(b))>0) {
065                            out.write(b, 0, len);
066                            total+=len;
067                            progressModel.addProgress(len);
068                    }
069                    return total;
070            }
071    
072            public static void readWrite(Reader in, Writer out) throws IOException {
073                    char[] b = new char[1024];
074                    int len;
075                    long total=0;
076                    while ((len = in.read(b))>0) {
077                            out.write(b, 0, len);
078                            total+=len;
079                    }
080            }
081            
082    }