com.partnersoft.io
Class DataCopier

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

public class DataCopier
extends java.lang.Object
implements IOCopier

Copies bytes from a DataInput to a DataOutput.

Typical usage:

 input = new DataDataInput(new FileDataInput(FileLib
                .createFile("data/copyfrom.dat")));
 output = new DataDataOutput(new FileDataOutput(FileLib
                .createFile("data/copyto.dat")));
 copier = new DataCopier(input, output);
 copier.copy();
 copier.close();
 

Copyright 2006 Partner Software, Inc.

Author:
Paul Reavis

Constructor Summary
DataCopier(java.io.DataInput source, java.io.DataOutput sink)
          Constructs a DataCopier for the given source and sink with standard buffer size and no specified data size.
DataCopier(java.io.DataInput source, java.io.DataOutput sink, int bufferSize)
          Constructs a DataCopier for the given source and sink with the given buffer size and no specified data size.
DataCopier(java.io.DataInput source, long dataSize, java.io.DataOutput sink)
          Constructs a DataCopier for the given source, data size and sink with standard buffer size.
DataCopier(java.io.DataInput source, long dataSize, java.io.DataOutput sink, int bufferSize)
          Constructs a DataCopier for the given source, sink, data size, and buffer size.
 
Method Summary
 void close()
          DataInput and DataOutput do not define flushing or closing methods, so this method does nothing.
 long copy()
          If dataSize is set, copies that amount.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataCopier

public DataCopier(java.io.DataInput source,
                  long dataSize,
                  java.io.DataOutput sink,
                  int bufferSize)
Constructs a DataCopier for the given source, sink, data size, and buffer size. The data size is required because DataInput does not support detection of EOF the same way InputStream does. Therefore, copy() doesn't know when to stop without causing an exception. The data size can be set to java.lang.Long.MAX_VALUE if you don't plan on using copy().


DataCopier

public DataCopier(java.io.DataInput source,
                  long dataSize,
                  java.io.DataOutput sink)
Constructs a DataCopier for the given source, data size and sink with standard buffer size.

See Also:
IOConstants.BUFFER_SIZE

DataCopier

public DataCopier(java.io.DataInput source,
                  java.io.DataOutput sink,
                  int bufferSize)
Constructs a DataCopier for the given source and sink with the given buffer size and no specified data size.


DataCopier

public DataCopier(java.io.DataInput source,
                  java.io.DataOutput sink)
Constructs a DataCopier for the given source and sink with standard buffer size and no specified data size.

See Also:
IOConstants.BUFFER_SIZE
Method Detail

copy

public long copy()
          throws java.io.IOException
If dataSize is set, copies that amount. If not, this will cause an EOFException.

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
DataInput and DataOutput do not define flushing or closing methods, so this method does nothing.

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