Class TailStream

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

    public class TailStream
    extends java.io.FilterInputStream

    A specialized input stream implementation which records the last portion read from an underlying stream.

    This stream implementation is useful to deal with information which is known to be located at the end of a stream (e.g. ID3 v1 tags). While reading bytes from the underlying stream, a given number of bytes is kept in an internal buffer. This buffer can then be queried after the whole stream was read. It contains the last bytes read from the original input stream.

    • Constructor Summary

      Constructors 
      Constructor Description
      TailStream​(java.io.InputStream in, int size)
      Creates a new instance of TailStream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] getTail()
      Returns an array with the last data read from the underlying stream.
      void mark​(int limit)
      This implementation saves the internal state including the content of the tail buffer so that it can be restored when ''reset()'' is called later.
      int read()
      This implementation adds the read byte to the internal tail buffer.
      int read​(byte[] buf)
      This implementation delegates to the underlying stream and then adds the correct portion of the read buffer to the internal tail buffer.
      int read​(byte[] buf, int ofs, int length)
      This implementation delegates to the underlying stream and then adds the correct portion of the read buffer to the internal tail buffer.
      void reset()
      This implementation restores this stream's state to the state when ''mark()'' was called the last time.
      long skip​(long n)
      This implementation delegates to the read() method to ensure that the tail buffer is also filled if data is skipped.
      • Methods inherited from class java.io.FilterInputStream

        available, close, markSupported
      • Methods inherited from class java.io.InputStream

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

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

      • TailStream

        public TailStream​(java.io.InputStream in,
                          int size)
        Creates a new instance of TailStream.
        Parameters:
        in - the underlying input stream
        size - the size of the tail buffer
    • Method Detail

      • read

        public int read()
                 throws java.io.IOException
        This implementation adds the read byte to the internal tail buffer.
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] buf)
                 throws java.io.IOException
        This implementation delegates to the underlying stream and then adds the correct portion of the read buffer to the internal tail buffer.
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] buf,
                        int ofs,
                        int length)
                 throws java.io.IOException
        This implementation delegates to the underlying stream and then adds the correct portion of the read buffer to the internal tail buffer.
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        This implementation delegates to the read() method to ensure that the tail buffer is also filled if data is skipped.
        Overrides:
        skip in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • mark

        public void mark​(int limit)
        This implementation saves the internal state including the content of the tail buffer so that it can be restored when ''reset()'' is called later.
        Overrides:
        mark in class java.io.FilterInputStream
      • reset

        public void reset()
        This implementation restores this stream's state to the state when ''mark()'' was called the last time. If ''mark()'' has not been called before, this method has no effect.
        Overrides:
        reset in class java.io.FilterInputStream
      • getTail

        public byte[] getTail()
        Returns an array with the last data read from the underlying stream. If the underlying stream contained more data than the ''tailSize'' constructor argument, the returned array has a length of ''tailSize''. Otherwise, its length equals the number of bytes read.
        Returns:
        an array with the last data read from the underlying stream