Class AbstractBufferedDataFetcher
- java.lang.Object
-
- net.markenwerk.utils.data.fetcher.AbstractDataFetcher
-
- net.markenwerk.utils.data.fetcher.AbstractBufferedDataFetcher
-
- All Implemented Interfaces:
DataFetcher
- Direct Known Subclasses:
BufferedDataFetcher
public abstract class AbstractBufferedDataFetcher extends AbstractDataFetcher
AbstractBufferedDataFetcheris a sensible base implementation ofDataFetcherthat uses abyte[]as buffer, to while copying all bytes from anInputStreamto anOutputStreamby sequentially reading from theInputStreaminto the buffer and then writing from the buffer to theOutputStream.Implementers must only implement the methods
obtainBuffer()andreturnBuffer(byte[])that manage abyte[]to be used as a buffer indoCopy(InputStream, OutputStream, DataFetchProgressListener).Implementers may also override
returnBuffer(byte[]), which is called afterdoCopy(InputStream, OutputStream, DataFetchProgressListener)has finished using it.- Since:
- 4.0.0
- Author:
- Torsten Krause (tk at markenwerk dot net)
-
-
Field Summary
Fields Modifier and Type Field Description protected static intDEFAULT_BUFEFR_SIZEThe default buffer size of 1024 bytes.
-
Constructor Summary
Constructors Constructor Description AbstractBufferedDataFetcher()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected static byte[]createBuffer(int bufferSize)Safely creates a new byte[] to be used as a buffer.protected voiddoCopy(InputStream in, OutputStream out, DataFetchProgressListener listener)Copies the content of a givenInputStreaminto a givenOutputStream.protected abstract byte[]obtainBuffer()Called bydoCopy(InputStream, OutputStream, DataFetchProgressListener)to obtain abyte[]to be used as a buffer.protected abstract voidreturnBuffer(byte[] buffer)Called bydoCopy(InputStream, OutputStream, DataFetchProgressListener)to return abyte[]that has previously been obtained fromobtainBuffer().
-
-
-
Field Detail
-
DEFAULT_BUFEFR_SIZE
protected static final int DEFAULT_BUFEFR_SIZE
The default buffer size of 1024 bytes.- See Also:
- Constant Field Values
-
-
Method Detail
-
createBuffer
protected static final byte[] createBuffer(int bufferSize)
Safely creates a new byte[] to be used as a buffer.- Parameters:
bufferSize- The size of the byte[] to be created. Defaults to thedefaultbuffer size, if the given buffer size is not positive.- Returns:
- The new byte[].
-
doCopy
protected final void doCopy(InputStream in, OutputStream out, DataFetchProgressListener listener) throws DataFetchException
Description copied from class:AbstractDataFetcherCopies the content of a givenInputStreaminto a givenOutputStream.It is guaranteed that neither the given
InputStreamnor the givenOutputStreamnor the givenDataFetchProgressListeneris null.Implementers must not close either of the given streams.
- Specified by:
doCopyin classAbstractDataFetcher- Parameters:
in- TheInputStreamto read from.out- TheOutputStreamto write to.listener- TheDataFetchProgressListenerto report to.- Throws:
DataFetchException- If anything went wrong while reading from the givenInputStreamor writing to the givenOutputStream.
-
obtainBuffer
protected abstract byte[] obtainBuffer()
Called bydoCopy(InputStream, OutputStream, DataFetchProgressListener)to obtain abyte[]to be used as a buffer.Every
byte[]that is returned by this method will be passed as an argument ofreturnBuffer(byte[])afterdoCopy(InputStream, OutputStream, DataFetchProgressListener)has finished using it.- Returns:
- The a
byte[]to be used as a buffer.
-
returnBuffer
protected abstract void returnBuffer(byte[] buffer)
Called bydoCopy(InputStream, OutputStream, DataFetchProgressListener)to return abyte[]that has previously been obtained fromobtainBuffer().- Parameters:
buffer- Thebyte[]to be returned.
-
-