Class ByteArraySeekableByteChannel

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.SeekableByteChannel, java.nio.channels.WritableByteChannel

    public class ByteArraySeekableByteChannel
    extends java.lang.Object
    implements java.nio.channels.SeekableByteChannel
    A SeekableByteChannel implementation backed by a byte array.

    When used for writing, the internal buffer grows to accommodate incoming data. The natural size limit is the value of IOUtils.SOFT_MAX_ARRAY_LENGTH and it's not possible to set the position or truncate to a value bigger than that. The raw internal buffer is accessed via array().

    Since:
    2.21.0
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteArraySeekableByteChannel()
      Constructs a new instance, with a default internal buffer capacity.
      ByteArraySeekableByteChannel​(int size)
      Constructs a new instance, with an internal buffer of the given capacity, in bytes.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] array()
      Gets the raw byte array backing this channel, this is not a copy.
      void close()  
      long getSize()
      Like size() but never throws ClosedChannelException.
      boolean isOpen()  
      long position()  
      java.nio.channels.SeekableByteChannel position​(long newPosition)  
      int read​(java.nio.ByteBuffer buf)  
      long size()  
      byte[] toByteArray()
      Gets a copy of the data stored in this channel.
      java.nio.channels.SeekableByteChannel truncate​(long newSize)  
      static ByteArraySeekableByteChannel wrap​(byte[] bytes)
      Constructs a new channel backed directly by the given byte array.
      int write​(java.nio.ByteBuffer b)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ByteArraySeekableByteChannel

        public ByteArraySeekableByteChannel()
        Constructs a new instance, with a default internal buffer capacity.

        The initial size and position of the channel are 0.

        See Also:
        ByteArrayOutputStream()
      • ByteArraySeekableByteChannel

        public ByteArraySeekableByteChannel​(int size)
        Constructs a new instance, with an internal buffer of the given capacity, in bytes.

        The initial size and position of the channel are 0.

        Parameters:
        size - Capacity of the internal buffer to allocate, in bytes.
        See Also:
        ByteArrayOutputStream(int)
    • Method Detail

      • wrap

        public static ByteArraySeekableByteChannel wrap​(byte[] bytes)
        Constructs a new channel backed directly by the given byte array.

        The channel initially contains the full contents of the array, with its size set to bytes.length and its position set to 0.

        Reads and writes operate on the shared array. If a write operation extends beyond the current capacity, the channel will automatically allocate a larger backing array and copy the existing contents.

        Parameters:
        bytes - The byte array to wrap, must not be null
        Returns:
        A new channel that uses the given array as its initial backing store
        Throws:
        java.lang.NullPointerException - If bytes is null
        See Also:
        array(), ByteArrayInputStream(byte[])
      • array

        public byte[] array()
        Gets the raw byte array backing this channel, this is not a copy.

        NOTE: The returned buffer is not aligned with containing data, use size() to obtain the size of data stored in the buffer.

        Returns:
        internal byte array.
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
      • getSize

        public long getSize()
        Like size() but never throws ClosedChannelException.
        Returns:
        See size().
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
      • position

        public long position()
                      throws java.nio.channels.ClosedChannelException
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.nio.channels.ClosedChannelException
      • position

        public java.nio.channels.SeekableByteChannel position​(long newPosition)
                                                       throws java.io.IOException
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • read

        public int read​(java.nio.ByteBuffer buf)
                 throws java.io.IOException
        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        Specified by:
        read in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • size

        public long size()
                  throws java.nio.channels.ClosedChannelException
        Specified by:
        size in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.nio.channels.ClosedChannelException
      • toByteArray

        public byte[] toByteArray()
        Gets a copy of the data stored in this channel.

        The returned array is a copy of the internal buffer, sized to the actual data stored in this channel.

        Returns:
        a new byte array containing the data stored in this channel.
      • truncate

        public java.nio.channels.SeekableByteChannel truncate​(long newSize)
                                                       throws java.nio.channels.ClosedChannelException
        Specified by:
        truncate in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.nio.channels.ClosedChannelException
      • write

        public int write​(java.nio.ByteBuffer b)
                  throws java.io.IOException
        Specified by:
        write in interface java.nio.channels.SeekableByteChannel
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Throws:
        java.io.IOException