Class SeekableByteChannelHelper

    • 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

      • 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
      • fill

        public static SeekableByteChannelFactory fill​(long size,
                                                      byte value)
        Create a fill SBC factory.
        Parameters:
        size - of the SeekableByteChannel
        value - 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 SeekableByteChannel
        inputStreamFactory - 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 from
        maxSize - 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 from
        maxSize - 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 from
        byteBuffer - to read into
        bytesToSkip - within the is to get to the next read location
        Throws:
        IOException - if an error occurs