commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Carman <jcar...@carmanconsulting.com>
Subject Re: [IO] Copy Streams with length and bufferSize
Date Tue, 06 Mar 2012 11:32:12 GMT
Neat ideas.  Can you submit a patch (with unit tests)?   Just file a jira
issue and attach it.
On Mar 6, 2012 2:12 AM, "manoj mokashi" <manojmokashi@gmail.com> wrote:

> Hi,
>
> would like to suggest a copy method for streams that takes a length
> and buffer size as arguments.
> Something like the below. Maybe we could also have a start offset and
> use the skip method to reach it.
>
> -----------------------
>
> import java.io.FileInputStream;
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.OutputStream;
>
> public class IOUtil {
>
>        public static void main(String[] args) throws IOException {
>                FileInputStream fis = null;
>                FileOutputStream fos = null;
>                try {
>                        fis = new FileInputStream( "c:/temp/tarlog.txt");
>                        fos = new FileOutputStream(
> "c:/temp/tarlog_copy.txt");
>                        copyStream( fis, fos, 100, 100);
>                }
>                finally {
>                        fis.close();
>                        fos.close();
>                }
>        }
>
>        // copy the specified len bytes of inputstream to the outputstream,
>        // using the bufferSize.
>        public static void copyStream( InputStream is, OutputStream os, int
> len, int bufferSize) throws IOException {
>                int totalread = 0;
>                byte[] barr = new byte[ bufferSize];
>                while( (len == -1 || totalread < len ) ){
>                        int read = is.read( barr, 0, len ==-1 ? bufferSize :
> Math.min(len-totalread, bufferSize) );
>                        if( read == -1){
>                                break;
>                        }
>                        else {
>                                os.write( barr, 0, read);
>                                totalread += read;
>                        }
>                }
>        }
>
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message