com.partnersoft.io
Class StreamCopier

java.lang.Object
  extended by com.partnersoft.io.StreamCopier
All Implemented Interfaces:
IOCopier

public class StreamCopier
extends java.lang.Object
implements IOCopier

Copies bytes from a InputStream to a OutputStream.

Typical usage:

 input = new FileInputStream(FileLib.createFile("data/copyfrom.dat");
 output = new FileOutputStream(FileLib.createFile("data/copyto.dat");
 copier = new StreamCopier(input, output);
 copier.copy();
 copier.close();
 

Copyright 2006 Partner Software, Inc.

Author:
Paul Reavis

Constructor Summary
StreamCopier(java.io.InputStream source, java.io.OutputStream sink)
          Constructs a StreamCopier for the given source and sink with standard buffer size.
StreamCopier(java.io.InputStream source, java.io.OutputStream sink, byte[] buffer)
          Constructs a StreamCopier for the given source, sink, and buffer.
StreamCopier(java.io.InputStream source, java.io.OutputStream sink, int bufferSize)
          Constructs a StreamCopier for the given source, sink, and buffer size.
 
Method Summary
 void close()
          Closes input source and output sink.
 long copy()
          Copies all available data from the source to the sink.
 long copy(long howMuch)
          Copies the specified amount of data from the source to the sink.
 long copyBlockingOnce()
          Block only once, copying available data, but quit as soon as a block would occur.
 long copyWithoutBlocking()
          Copies only available data.
 long getCopiedByteCount()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StreamCopier

public StreamCopier(java.io.InputStream source,
                    java.io.OutputStream sink,
                    byte[] buffer)
Constructs a StreamCopier for the given source, sink, and buffer. Use this constructor if you plan on doing a lot of stream copies in a row and want to recycle your buffer to improve performance.


StreamCopier

public StreamCopier(java.io.InputStream source,
                    java.io.OutputStream sink,
                    int bufferSize)
Constructs a StreamCopier for the given source, sink, and buffer size.


StreamCopier

public StreamCopier(java.io.InputStream source,
                    java.io.OutputStream sink)
Constructs a StreamCopier for the given source and sink with standard buffer size.

See Also:
IOConstants.BUFFER_SIZE
Method Detail

copy

public long copy()
          throws java.io.IOException
Description copied from interface: IOCopier
Copies all available data from the source to the sink. Flushes the output when done but does not close either source or sink.

Specified by:
copy in interface IOCopier
Returns:
total number of items (bytes, characters, etc.) copied
Throws:
java.io.IOException - if something prevents the copy

copy

public long copy(long howMuch)
          throws java.io.IOException
Description copied from interface: IOCopier
Copies the specified amount of data from the source to the sink. Flushes the output when done but does not close either source or sink. Can be called multiple times, as long as there is available data.

Specified by:
copy in interface IOCopier
Parameters:
howMuch - how many items (bytes, characters, etc.) to copy
Returns:
total number of items (bytes, characters, etc.) copied
Throws:
java.io.IOException - if something prevents the copy or if there are not enough items to copy

copyWithoutBlocking

public long copyWithoutBlocking()
                         throws java.io.IOException
Description copied from interface: IOCopier
Copies only available data. No blocking is done; if no data is available, stop.

This method is optional, if it's not appropriate for the kind of copy, throws an UnsupportedOperationException.

Specified by:
copyWithoutBlocking in interface IOCopier
Throws:
java.io.IOException

copyBlockingOnce

public long copyBlockingOnce()
                      throws java.io.IOException
Description copied from interface: IOCopier
Block only once, copying available data, but quit as soon as a block would occur.

This method is optional, if it's not appropriate for the kind of copy, throws an UnsupportedOperationException.

Specified by:
copyBlockingOnce in interface IOCopier
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Description copied from interface: IOCopier
Closes input source and output sink. Optional; in some cases you may want to leave the streams open for other purposes.

Specified by:
close in interface IOCopier
Throws:
java.io.IOException - if anything prevents closing.

getCopiedByteCount

public long getCopiedByteCount()