Package emissary.core.channels
Class SeekableByteChannelHelper
- java.lang.Object
-
- emissary.core.channels.SeekableByteChannelHelper
-
public final class SeekableByteChannelHelper extends Object
Helper methods to handleSeekableByteChannel
objects
-
-
Field Summary
Fields Modifier and Type Field Description static SeekableByteChannelFactory
EMPTY_CHANNEL_FACTORY
Channel factory backed by an empty byte array.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
available(InputStream inputStream)
Provided with an existing input stream, check how far we can read into it.static SeekableByteChannelFactory
file(Path path)
Create a file SBC factory.static SeekableByteChannelFactory
fill(long size, byte value)
Create a fill SBC factory.static byte[]
getByteArrayFromBdo(IBaseDataObject ibdo, int maxSize)
Given a BDO, create a byte array with as much data as possible.static byte[]
getByteArrayFromChannel(SeekableByteChannelFactory sbcf, int maxSize)
Given a channel factory, create a byte array with as much data as possible.static int
getFromInputStream(InputStream inputStream, ByteBuffer byteBuffer, long bytesToSkip)
Reads data from an input stream into a bufferstatic SeekableByteChannelFactory
immutable(SeekableByteChannelFactory sbcf)
Make an existing factory immutable.static SeekableByteChannelFactory
inputStream(long size, InputStreamFactory inputStreamFactory)
Create an InputStream SBC factory.static SeekableByteChannelFactory
memory(byte[] bytes)
Create an in memory SBC factory which can be used to create any number of channels based on the provided bytes without storing them multiple times.
-
-
-
Field Detail
-
EMPTY_CHANNEL_FACTORY
public static final SeekableByteChannelFactory EMPTY_CHANNEL_FACTORY
Channel factory backed by an empty byte array. Used for situations when a BDO should have its payload discarded.
-
-
Method Detail
-
immutable
public static SeekableByteChannelFactory immutable(SeekableByteChannelFactory sbcf)
Make an existing factory immutable.- Parameters:
sbcf
- to make immutable- Returns:
- the wrapped factory
-
memory
public static SeekableByteChannelFactory memory(byte[] bytes)
Create an in memory SBC factory which can be used to create any number of channels based on the provided bytes without storing them multiple times.- Parameters:
bytes
- to use with the channel- Returns:
- the factory
-
file
public static SeekableByteChannelFactory file(Path path)
Create a file SBC factory.- Parameters:
path
- to the file.- Returns:
- the factory
-
fill
public static SeekableByteChannelFactory fill(long size, byte value)
Create a fill SBC factory.- Parameters:
size
- of the SeekableByteChannelvalue
- of each element in the SeekableByteChannel.- Returns:
- the factory
-
inputStream
public static SeekableByteChannelFactory inputStream(long size, InputStreamFactory inputStreamFactory)
Create an InputStream SBC factory.- Parameters:
size
- of the SeekableByteChannelinputStreamFactory
- creates the needed InputStreams.- Returns:
- the factory
-
getByteArrayFromBdo
public static byte[] getByteArrayFromBdo(IBaseDataObject ibdo, int maxSize)
Given a BDO, create a byte array with as much data as possible.- Parameters:
ibdo
- to get the data frommaxSize
- to limit the byte array to- Returns:
- a byte array of the data from the BDO sized up to maxSize (so could truncate data)
-
getByteArrayFromChannel
public static byte[] getByteArrayFromChannel(SeekableByteChannelFactory sbcf, int maxSize) throws IOException
Given a channel factory, create a byte array with as much data as possible.- Parameters:
sbcf
- to get the data frommaxSize
- to limit the byte array to- Returns:
- a byte array of the data from the factory sized up to maxSize (so could truncate data)
- Throws:
IOException
- if we couldn't read all the data
-
available
public static long available(InputStream inputStream)
Provided with an existing input stream, check how far we can read into it. Note that the inputStream is read as-is, so if the stream is not at the start, this method won't take that into account. If we can successfully read the stream, the position of the provided stream will of course change. Don't wrap the provided stream with anything such as BufferedInputStream as this will cause read errors prematurely, unless this is acceptable.- Parameters:
inputStream
- to read - caller must handle closing this object- Returns:
- position of last successful read (which could be the size of the stream)
-
getFromInputStream
public static int getFromInputStream(InputStream inputStream, ByteBuffer byteBuffer, long bytesToSkip) throws IOException
Reads data from an input stream into a buffer- Parameters:
inputStream
- to read frombyteBuffer
- to read intobytesToSkip
- within theis
to get to the next read location- Throws:
IOException
- if an error occurs
-
-