You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by manoj mokashi <ma...@gmail.com> on 2012/03/06 08:12:03 UTC
[IO] Copy Streams with length and bufferSize
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
Re: [IO] Copy Streams with length and bufferSize
Posted by James Carman <jc...@carmanconsulting.com>.
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" <ma...@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
>
>
Re: [IO] Copy Streams with length and bufferSize
Posted by manoj mokashi <ma...@gmail.com>.
Have created JIRA issue IO-305, and attached the code and unit tests.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org