Class SeekableInMemoryByteChannel

  • 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 SeekableInMemoryByteChannel
    extends java.lang.Object
    implements java.nio.channels.SeekableByteChannel
    SeekableByteChannel implementation backed by an auto-resizing byte array; thread-safe. Can hold a maxiumum of Integer.MAX_VALUE bytes.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] contents
      Internal buffer for contents; guarded by "this"
      private boolean open
      Whether or not this SeekableByteChannel is open; volatile instead of sync is acceptable because this field participates in no compound computations or invariants with other instance members.
      private int position
      Current position; guarded by "this"
    • Constructor Summary

      Constructors 
      Constructor Description
      SeekableInMemoryByteChannel()
      Creates a new instance with 0 size and 0 position, and open.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void checkClosed()
      Throws a ClosedChannelException if this SeekableByteChannel is closed.
      void close()
      private byte[] concat​(byte[] input1, byte[] input2, int position)
      Creates a new array which is the concatenated result of the two inputs, at the designated position (to be filled with 0x00) in the case of a gap).
      (package private) java.io.InputStream getContents()
      Obtain a copy of the contents of this Channel as an InputStream
      boolean isOpen()
      long position()
      java.nio.channels.SeekableByteChannel position​(long newPosition)
      int read​(java.nio.ByteBuffer destination)
      long size()
      java.nio.channels.SeekableByteChannel truncate​(long size)
      int write​(java.nio.ByteBuffer source)
      • Methods inherited from class java.lang.Object

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

      • position

        private int position
        Current position; guarded by "this"
      • open

        private volatile boolean open
        Whether or not this SeekableByteChannel is open; volatile instead of sync is acceptable because this field participates in no compound computations or invariants with other instance members.
      • contents

        private byte[] contents
        Internal buffer for contents; guarded by "this"
    • Constructor Detail

      • SeekableInMemoryByteChannel

        public SeekableInMemoryByteChannel()
        Creates a new instance with 0 size and 0 position, and open.
    • Method Detail

      • isOpen

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

        public void close()
                   throws java.io.IOException
        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
        Throws:
        java.io.IOException
        See Also:
        Channel.close()
      • read

        public int read​(java.nio.ByteBuffer destination)
                 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
        See Also:
        SeekableByteChannel.read(java.nio.ByteBuffer)
      • write

        public int write​(java.nio.ByteBuffer source)
                  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
        See Also:
        SeekableByteChannel.write(java.nio.ByteBuffer)
      • concat

        private byte[] concat​(byte[] input1,
                              byte[] input2,
                              int position)
        Creates a new array which is the concatenated result of the two inputs, at the designated position (to be filled with 0x00) in the case of a gap).
        Parameters:
        input1 -
        input2 -
        position -
        Returns:
      • position

        public long position()
                      throws java.io.IOException
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
        See Also:
        SeekableByteChannel.position()
      • 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
        See Also:
        SeekableByteChannel.position(long)
      • size

        public long size()
                  throws java.io.IOException
        Specified by:
        size in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
        See Also:
        SeekableByteChannel.size()
      • truncate

        public java.nio.channels.SeekableByteChannel truncate​(long size)
                                                       throws java.io.IOException
        Specified by:
        truncate in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
        See Also:
        SeekableByteChannel.truncate(long)
      • getContents

        java.io.InputStream getContents()
        Obtain a copy of the contents of this Channel as an InputStream
        Returns:
      • checkClosed

        private void checkClosed()
                          throws java.nio.channels.ClosedChannelException
        Throws a ClosedChannelException if this SeekableByteChannel is closed.
        Throws:
        java.nio.channels.ClosedChannelException