Class FileUploadConsumer.ByteCounter

  • All Implemented Interfaces:
    ByteCursor
    Enclosing class:
    FileUploadConsumer

    private class FileUploadConsumer.ByteCounter
    extends java.lang.Object
    implements ByteCursor
    The ByteCounter is a wrapper for a cursor that can be used to restrict the number of bytes consumed. This will count the bytes consumed and ensure that any requested data is restricted to a chunk less than or equal to the remaining bytes.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private ByteCursor cursor
      This is the cursor that this counter will delegate to.
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteCounter​(ByteCursor cursor)
      Constructor for the Counter object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isOpen()
      Determines whether the cursor is still open.
      boolean isReady()
      Determines whether the cursor is ready for reading.
      void push​(byte[] data)
      Pushes the provided data on to the cursor.
      void push​(byte[] data, int off, int len)
      Pushes the provided data on to the cursor.
      int read​(byte[] data)
      Reads a block of bytes from the underlying stream.
      int read​(byte[] data, int off, int len)
      Reads a block of bytes from the underlying stream.
      int ready()
      Provides the number of bytes that can be read from the stream without blocking.
      int reset​(int len)
      Moves the cursor backward within the stream.
      • Methods inherited from class java.lang.Object

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

      • cursor

        private final ByteCursor cursor
        This is the cursor that this counter will delegate to.
    • Constructor Detail

      • ByteCounter

        public ByteCounter​(ByteCursor cursor)
        Constructor for the Counter object. This is used to create a special cursor that counts the bytes read and limits reads to the remaining bytes left in the part series.
        Parameters:
        cursor - this is the cursor that is delegated to
    • Method Detail

      • isOpen

        public boolean isOpen()
                       throws java.io.IOException
        Determines whether the cursor is still open. The cursor is considered open if there are still bytes to read. If there is still bytes buffered and the underlying transport is closed then the cursor is still considered open.
        Specified by:
        isOpen in interface ByteCursor
        Returns:
        true if the read method does not return a -1 value
        Throws:
        java.io.IOException
      • isReady

        public boolean isReady()
                        throws java.io.IOException
        Determines whether the cursor is ready for reading. When the cursor is ready then it guarantees that some amount of bytes can be read from the underlying stream without blocking.
        Specified by:
        isReady in interface ByteCursor
        Returns:
        true if some data can be read without blocking
        Throws:
        java.io.IOException
      • ready

        public int ready()
                  throws java.io.IOException
        Provides the number of bytes that can be read from the stream without blocking. This is typically the number of buffered or available bytes within the stream. When this reaches zero then the cursor may perform a blocking read.
        Specified by:
        ready in interface ByteCursor
        Returns:
        the number of bytes that can be read without blocking
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] data)
                 throws java.io.IOException
        Reads a block of bytes from the underlying stream. This will read up to the requested number of bytes from the underlying stream. If there are no ready bytes on the stream this can return zero, representing the fact that nothing was read.
        Specified by:
        read in interface ByteCursor
        Parameters:
        data - this is the array to read the bytes in to
        Returns:
        this returns the number of bytes read from the stream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] data,
                        int off,
                        int len)
                 throws java.io.IOException
        Reads a block of bytes from the underlying stream. This will read up to the requested number of bytes from the underlying stream. If there are no ready bytes on the stream this can return zero, representing the fact that nothing was read.
        Specified by:
        read in interface ByteCursor
        Parameters:
        data - this is the array to read the bytes in to
        off - this is the offset to begin writing the bytes to
        len - this is the number of bytes that are requested
        Returns:
        this returns the number of bytes read from the stream
        Throws:
        java.io.IOException
      • push

        public void push​(byte[] data)
                  throws java.io.IOException
        Pushes the provided data on to the cursor. Data pushed on to the cursor will be the next data read from the cursor. This complements the reset method which will reset the cursors position on a stream. Allowing data to be pushed on to the cursor allows more flexibility.
        Specified by:
        push in interface ByteCursor
        Parameters:
        data - this is the data to be pushed on to the cursor
        Throws:
        java.io.IOException
      • push

        public void push​(byte[] data,
                         int off,
                         int len)
                  throws java.io.IOException
        Pushes the provided data on to the cursor. Data pushed on to the cursor will be the next data read from the cursor. This complements the reset method which will reset the cursors position on a stream. Allowing data to be pushed on to the cursor allows more flexibility.
        Specified by:
        push in interface ByteCursor
        Parameters:
        data - this is the data to be pushed on to the cursor
        off - this is the offset to begin reading the bytes
        len - this is the number of bytes that are to be used
        Throws:
        java.io.IOException
      • reset

        public int reset​(int len)
                  throws java.io.IOException
        Moves the cursor backward within the stream. This ensures that any bytes read from the last read can be pushed back in to the stream so that they can be read again. This will throw an exception if the reset can not be performed.
        Specified by:
        reset in interface ByteCursor
        Parameters:
        len - this is the number of bytes to reset back
        Returns:
        this is the number of bytes that have been reset
        Throws:
        java.io.IOException