Class LengthMarkedBufferedInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public final class LengthMarkedBufferedInputStream
    extends java.io.FilterInputStream
    Reads a command block on the underlying stream that is constrained by a length marker preceeding the command. This can be used as a hack work around for non-blocking IO because we know ahead of time how much data makes up the next block of information over the stream.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] buf
      The buffer that is used to read in whatever is on the stream.
      private int count
      The number of valid bytes in the buffer.
      private java.io.InputStream in
      The chained InputStream that is underneath this object.
      private static int INITIAL_BUFFER_SIZE
      The initial buffer size of the internal input buffer.
      private int marked_index
      The current index of the marked area that is being read.
      private int marked_length
      The area of the buffer that is marked as being an available command.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void blockForCommand()
      Blocks until a complete command has been read in.
      private void ensureCapacity​(int new_size)
      Ensures that the buffer is large enough to store the given value.
      private void handleEndReached()
      Private method, it is called when the end of the marked length is reached.
      boolean markSupported()  
      boolean pollForCommand​(int max_size)
      Checks to see if there is a complete command waiting on the input stream.
      int read()  
      int read​(byte[] b, int off, int len)  
      • Methods inherited from class java.io.FilterInputStream

        close, mark, read, reset, skip
      • Methods inherited from class java.io.InputStream

        nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

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

      • INITIAL_BUFFER_SIZE

        private static int INITIAL_BUFFER_SIZE
        The initial buffer size of the internal input buffer.
      • in

        private java.io.InputStream in
        The chained InputStream that is underneath this object.
      • buf

        private byte[] buf
        The buffer that is used to read in whatever is on the stream.
      • count

        private int count
        The number of valid bytes in the buffer.
      • marked_length

        private int marked_length
        The area of the buffer that is marked as being an available command. If it's -1 then there is no area marked.
      • marked_index

        private int marked_index
        The current index of the marked area that is being read.
    • Constructor Detail

      • LengthMarkedBufferedInputStream

        public LengthMarkedBufferedInputStream​(java.io.InputStream in)
        The Constructor.
    • Method Detail

      • ensureCapacity

        private void ensureCapacity​(int new_size)
        Ensures that the buffer is large enough to store the given value. If it's not then it grows the buffer so it is big enough.
      • handleEndReached

        private void handleEndReached()
        Private method, it is called when the end of the marked length is reached. It performs various maintenance operations to ensure the buffer consistency is maintained. Assumes we are calling from a synchronized method.
      • read

        public int read()
                 throws java.io.IOException
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.FilterInputStream
      • pollForCommand

        public boolean pollForCommand​(int max_size)
                               throws java.io.IOException
        Checks to see if there is a complete command waiting on the input stream. Returns true if there is. If this method returns true then it is safe to go ahead and process a single command from this stream. This will return true only once while there is a command pending until that command is completely read in.

        'max_size' is the maximum number of bytes we are allowing before an IOException is thrown.

        Throws:
        java.io.IOException
      • blockForCommand

        public void blockForCommand()
                             throws java.io.IOException
        Blocks until a complete command has been read in.
        Throws:
        java.io.IOException