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
AbstractBufferedDataFetcher
is a sensible base implementation ofDataFetcher
that uses abyte[]
as buffer, to while copying all bytes from anInputStream
to anOutputStream
by sequentially reading from theInputStream
into 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 int
DEFAULT_BUFEFR_SIZE
The 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 void
doCopy(InputStream in, OutputStream out, DataFetchProgressListener listener)
Copies the content of a givenInputStream
into a givenOutputStream
.protected abstract byte[]
obtainBuffer()
Called bydoCopy(InputStream, OutputStream, DataFetchProgressListener)
to obtain abyte[]
to be used as a buffer.protected abstract void
returnBuffer(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 thedefault
buffer 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:AbstractDataFetcher
Copies the content of a givenInputStream
into a givenOutputStream
.It is guaranteed that neither the given
InputStream
nor the givenOutputStream
nor the givenDataFetchProgressListener
is null.Implementers must not close either of the given streams.
- Specified by:
doCopy
in classAbstractDataFetcher
- Parameters:
in
- TheInputStream
to read from.out
- TheOutputStream
to write to.listener
- TheDataFetchProgressListener
to report to.- Throws:
DataFetchException
- If anything went wrong while reading from the givenInputStream
or 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.
-
-