public class FormattedDataSource extends Object
After constructing a new and empty FormattedDataSource use one of the set() methods to specify the source of the data bytes. This can be a File, an already open InputStream, a named resource or bytes in memory. After calling set() you can then read the byte data from the FormattedDataSource in a variety of ways. You can call getInputStream()/getRandomStream() to get the data as a stream, or you can call getByteArray()/getAllBytes() to get the data as bytes in memory (this may involved reading in the data from the source if the source was an InputStream).
Modifier and Type | Field and Description |
---|---|
String |
name
This is an optional name that you can give to the FormattedDataSource.
|
Constructor and Description |
---|
FormattedDataSource()
Create a new FormattedDataSource.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear the input source - but without closing any InputStreams.
|
void |
close()
Close all open InputStreams and clear() the source, ignoring any
IOExceptions that may occur.
|
ArraySection |
exposeBytes()
Expose the source bytes as an ArraySection ONLY if the data was provided as an
array of bytes.
|
void |
free()
This calls clear() but will also erase the internal ByteArray or ArraySection buffer.
|
ArraySection |
getAllBytes(Handle handle)
Read in all the data, place it in a byte array and then place the result in an
ArraySection.
|
ArraySection |
getAllBytes(Handle handle,
ArraySection destination)
Read in all the data, place it in a byte array and then place the result in an
ArraySection.
|
ByteArray |
getByteArray(Handle handle)
Read in all the bytes and place in a ByteArray.
|
ByteArray |
getByteArray(Handle handle,
ByteArray destination)
Read in all the bytes and place in a ByteArray.
|
InputStream |
getInputStream()
Return an InputStream for reading the formatted data bytes.
|
RandomStream |
getRandomStream()
Get a true RandomStream to read the formatted data bytes from.
|
RandomStream |
getRewindableStream()
Return a RewindableStream for reading the data bytes.
|
boolean |
hasData()
This checks to see if one of the set() methods has been called.
|
FormattedDataSource |
readIntoMemory()
If the FormattedDataSource is not already reading from bytes then read them all into
memory - closing the original data source.
|
void |
rewind()
Call this only once after calling getRewindableStream().
|
FormattedDataSource |
set(byte[] data,
int offset,
int length)
This sets the source of the bytes to be from an array of bytes.
|
FormattedDataSource |
set(ByteArray data)
This sets the source of the bytes to be from an array of bytes.
|
FormattedDataSource |
set(File src)
This sets the source of the bytes to be from the specified file.
|
FormattedDataSource |
set(InputStream source)
This sets the source of the bytes to be an InputStream.
|
FormattedDataSource |
set(String resourceName)
This sets the source of the bytes to be from a named resource file or named resource data.
|
public String name
public FormattedDataSource()
public FormattedDataSource readIntoMemory() throws IOException
IOException
public void clear()
public void free()
public void close()
public FormattedDataSource set(File src) throws IOException
src
- the file to retrieve the bytes from..IOException
- if the file could not be opened to read its bytes.public FormattedDataSource set(String resourceName) throws IOException
resourceName
- the name of the resource.IOException
- if the resource could not be found or opened.public FormattedDataSource set(InputStream source)
source
- the InputStream to read the formatted data from.public FormattedDataSource set(byte[] data, int offset, int length)
data
- the array containing the bytes.offset
- the start offset within the array.length
- the number of bytes in the data.public FormattedDataSource set(ByteArray data)
data
- the ByteArray containing the bytes.public boolean hasData()
public InputStream getInputStream()
public RandomStream getRewindableStream()
public RandomStream getRandomStream() throws IOException
IOException
- if there is an IO error reading the data bytes into memory
if that was necessary.public void rewind() throws IOException, IllegalStateException
IOException
- if an IO error occurs.IllegalStateException
- if getRewindableStream() was not called first.public ByteArray getByteArray(Handle handle, ByteArray destination) throws IOException
handle
- an optional Handle that can be used to stop the process if another
Thread calls stop() on the Handle.destination
- an optional destination ByteArray. If this is null a new one
will be created and returned.IOException
- if an IO error occured reading in the bytes.public ByteArray getByteArray(Handle handle) throws IOException
handle
- an optional Handle that can be used to stop the process if another
Thread calls stop() on the Handle.IOException
- if an IO error occurs reading in the bytes.public ArraySection getAllBytes(Handle handle, ArraySection destination) throws IOException
handle
- an optional Handle that can be used to stop the process if another
Thread calls stop() on the Handle.destination
- an optional destination ArraySection. If this is null a new one
will be created and returned.IOException
- if an IO error occurs reading in the bytes.public ArraySection getAllBytes(Handle handle) throws IOException
handle
- an optional Handle that can be used to stop the process if another
Thread calls stop() on the Handle.IOException
- if an IO error occurs reading in the bytes.public ArraySection exposeBytes()