Class FastByteArrayInputStream

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

    public class FastByteArrayInputStream
    extends java.io.ByteArrayInputStream
    A fast, non-thread-safe implementation of ByteArrayInputStream.

    This class extends the standard ByteArrayInputStream to provide optimized performance in single-threaded scenarios. Unlike the standard implementation, it does not perform synchronization, making it faster but unsafe for concurrent use.

    Example Usage

    
     byte[] data = "Hello, World!".getBytes(StandardCharsets.UTF_8);
     FastByteArrayInputStream inputStream = new FastByteArrayInputStream(data);
    
     int c;
     while ((c = inputStream.read()) != -1) {
         System.out.print((char) c);
     }
     inputStream.close();
     

    For reading a subset of the byte array:

    
     byte[] data = "Microservices are great!".getBytes(StandardCharsets.UTF_8);
     FastByteArrayInputStream inputStream = new FastByteArrayInputStream(data, 0, 11); // Read only first 11 bytes
    
     int c;
     while ((c = inputStream.read()) != -1) {
         System.out.print((char) c);
     }
     inputStream.close();
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    ByteArrayInputStream
    • Field Summary

      • Fields inherited from class java.io.ByteArrayInputStream

        buf, count, mark, pos
    • Constructor Summary

      Constructors 
      Constructor Description
      FastByteArrayInputStream​(byte[] buf)
      Creates a FastByteArrayInputStream so that it uses buf as its buffer array.
      FastByteArrayInputStream​(byte[] buf, int offset, int length)
      Creates FastByteArrayInputStream that uses buf as its buffer array.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void close()  
      boolean equals​(java.lang.Object obj)  
      int hashCode()  
      int read()  
      int read​(byte[] b, int off, int len)  
      void reset()  
      long skip​(long n)  
      • Methods inherited from class java.io.ByteArrayInputStream

        mark, markSupported
      • Methods inherited from class java.io.InputStream

        read
      • Methods inherited from class java.lang.Object

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

      • FastByteArrayInputStream

        public FastByteArrayInputStream​(byte[] buf)
        Creates a FastByteArrayInputStream so that it uses buf as its buffer array. The buffer array is not copied. The initial value of pos is 0 and the initial value of count is the length of buf.
        Parameters:
        buf - the input buffer.
      • FastByteArrayInputStream

        public FastByteArrayInputStream​(byte[] buf,
                                        int offset,
                                        int length)
        Creates FastByteArrayInputStream that uses buf as its buffer array. The initial value of pos is offset and the initial value of count is the minimum of offset+length and buf.length. The buffer array is not copied. The buffer's mark is set to the specified offset.
        Parameters:
        buf - the input buffer.
        offset - the offset in the buffer of the first byte to read.
        length - the maximum number of bytes to read from the buffer.
    • Method Detail

      • read

        public int read()
        Overrides:
        read in class java.io.ByteArrayInputStream
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
        Overrides:
        read in class java.io.ByteArrayInputStream
      • skip

        public long skip​(long n)
        Overrides:
        skip in class java.io.ByteArrayInputStream
      • available

        public int available()
        Overrides:
        available in class java.io.ByteArrayInputStream
      • reset

        public void reset()
        Overrides:
        reset in class java.io.ByteArrayInputStream
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.ByteArrayInputStream
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object