Class ReadAheadBufferedStream

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

    public class ReadAheadBufferedStream
    extends java.io.FilterInputStream
    Permit to buf socket data, reading not only asked bytes, but available number of bytes when possible.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] buf  
      private static int BUF_SIZE  
      private int end  
      private int pos  
      • Fields inherited from class java.io.FilterInputStream

        in
    • Constructor Summary

      Constructors 
      Constructor Description
      ReadAheadBufferedStream​(java.io.InputStream in)
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void close()  
      private void fillingBuffer​(int minNeededBytes)
      Fill buf with required length, or available bytes.
      boolean markSupported()  
      int read()  
      int read​(byte[] externalBuf, int off, int len)
      Returning byte array, from cache of reading socket if needed.
      void reset()  
      long skip​(long n)  
      • Methods inherited from class java.io.FilterInputStream

        mark, read
      • Methods inherited from class java.lang.Object

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

      • buf

        private final byte[] buf
      • end

        private int end
      • pos

        private int pos
    • Constructor Detail

      • ReadAheadBufferedStream

        public ReadAheadBufferedStream​(java.io.InputStream in)
        Constructor

        Implementation doesn't use synchronized/semaphore because all used are already locked by Statement/PreparedStatement Reentrant lock

        Parameters:
        in - socket input stream
    • Method Detail

      • read

        public int read​(byte[] externalBuf,
                        int off,
                        int len)
                 throws java.io.IOException
        Returning byte array, from cache of reading socket if needed.
        Overrides:
        read in class java.io.FilterInputStream
        Parameters:
        externalBuf - buf to fill
        off - offset
        len - length to read
        Returns:
        number of added bytes
        Throws:
        java.io.IOException - if exception during socket reading
      • fillingBuffer

        private void fillingBuffer​(int minNeededBytes)
                            throws java.io.IOException
        Fill buf with required length, or available bytes.
        Parameters:
        minNeededBytes - asked number of bytes
        Throws:
        java.io.IOException - in case of failing reading stream.
      • markSupported

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

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close 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
      • read

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

        public long skip​(long n)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • reset

        public void reset()
                   throws java.io.IOException
        Overrides:
        reset in class java.io.FilterInputStream
        Throws:
        java.io.IOException