Class FastByteArrayInputStream

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

    public class FastByteArrayInputStream
    extends MeasurableInputStream
    implements RepositionableStream
    Simple, fast and repositionable byte-array input stream.

    Warning: this class implements the correct semantics of read(byte[], int, int) as described in InputStream. The implementation given in ByteArrayInputStream is broken, but it will never be fixed because it's too late.

    Author:
    Sebastiano Vigna
    • Field Summary

      Fields 
      Modifier and Type Field Description
      byte[] array
      The array backing the input stream.
      int length
      The number of valid bytes in array starting from offset.
      int offset
      The first valid entry.
    • Constructor Summary

      Constructors 
      Constructor Description
      FastByteArrayInputStream​(byte[] array)
      Creates a new array input stream using a given array.
      FastByteArrayInputStream​(byte[] array, int offset, int length)
      Creates a new array input stream using a given array fragment.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void close()
      Closing a fast byte array input stream has no effect.
      long length()
      Returns the overall length of this stream (optional operation).
      void mark​(int dummy)  
      boolean markSupported()  
      long position()
      Returns the current stream position.
      void position​(long newPosition)
      Sets the current stream position.
      int read()  
      int read​(byte[] b, int offset, int length)
      Reads bytes from this byte-array input stream as specified in InputStream.read(byte[], int, int).
      void reset()  
      long skip​(long n)  
      • Methods inherited from class java.io.InputStream

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

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

      • array

        public byte[] array
        The array backing the input stream.
      • offset

        public int offset
        The first valid entry.
      • length

        public int length
        The number of valid bytes in array starting from offset.
    • Constructor Detail

      • FastByteArrayInputStream

        public FastByteArrayInputStream​(byte[] array,
                                        int offset,
                                        int length)
        Creates a new array input stream using a given array fragment.
        Parameters:
        array - the backing array.
        offset - the first valid entry of the array.
        length - the number of valid bytes.
      • FastByteArrayInputStream

        public FastByteArrayInputStream​(byte[] array)
        Creates a new array input stream using a given array.
        Parameters:
        array - the backing array.
    • Method Detail

      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.InputStream
      • reset

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

        public void close()
        Closing a fast byte array input stream has no effect.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
      • mark

        public void mark​(int dummy)
        Overrides:
        mark in class java.io.InputStream
      • available

        public int available()
        Overrides:
        available in class java.io.InputStream
      • skip

        public long skip​(long n)
        Overrides:
        skip in class java.io.InputStream
      • read

        public int read()
        Specified by:
        read in class java.io.InputStream
      • read

        public int read​(byte[] b,
                        int offset,
                        int length)
        Reads bytes from this byte-array input stream as specified in InputStream.read(byte[], int, int). Note that the implementation given in ByteArrayInputStream.read(byte[], int, int) will return -1 on a zero-length read at EOF, contrarily to the specification. We won't.
        Overrides:
        read in class java.io.InputStream
      • position

        public void position​(long newPosition)
        Description copied from interface: RepositionableStream
        Sets the current stream position.
        Specified by:
        position in interface RepositionableStream
        Parameters:
        newPosition - the new stream position.
      • length

        public long length()
        Description copied from interface: MeasurableStream
        Returns the overall length of this stream (optional operation). In most cases, this will require the stream to perform some extra action, possibly changing the state of the input stream itself (typically, reading all the bytes up to the end, or flushing on output stream). Implementing classes should always document what state will the input stream be in after calling this method, and which kind of exception could be thrown.
        Specified by:
        length in interface MeasurableStream