Class BufferedRereadableContent

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class BufferedRereadableContent
    extends Object
    implements Closeable
    The BufferedRereadableContent can be used when the content of an InputStream needs to be processed multiple times. Depending on its eventual size the whole content of the input stream is buffered in-memory - up to a certain threshold - or in a temporary file.

    The BufferedRereadableContent as well as the provided input streams must be closed after use! This can be ensured by adhering to the following template:

     
     try (BufferedRereadableContent input = new BufferedRereadableContent(inputStream))
     {
       ...
       try (InputStream is = input.getInputStream())
       {
         ...
       }
       ...
       try (InputStream is = input.getInputStream())
       {
         ...
       }
       ...
     }
     
     
    Closing this BufferedRereadableContent will also close the underlying InputStream (unless it has already been read and closed in the constructor). Also created streams that have not been closed will also be closed.
    • Field Detail

      • logger

        protected final Logger logger
      • length

        protected final long length
        The size of the content.
      • createdStreams

        protected final Collection<Closeable> createdStreams
        All streams that have been created by this BufferedRereadableContent.
    • Constructor Detail

      • BufferedRereadableContent

        public BufferedRereadableContent​(InputStream inputStream)
                                  throws IOException
        Creates a new BufferedRereadableContent and immediately reads the given InputStream.
        Parameters:
        inputStream - The input stream to read. This will be closed when its content has been read. If it is already re-readable,it will be closed when closing this BufferedRereadableContent.
        Throws:
        IOException - if an I/O error occurs
      • BufferedRereadableContent

        public BufferedRereadableContent​(InputStream inputStream,
                                         int threshold,
                                         String tempFileTag,
                                         File tempDir)
                                  throws IOException
        Creates a new BufferedRereadableContent and immediately reads the given InputStream.

        ByteArrayInputStreams will receive special treatment to avoid creating a duplicate of their content.

        Parameters:
        inputStream - The input stream to read. This will be closed when its content has been read. If it is already re-readable,it will be closed when closing this BufferedRereadableContent.
        threshold - the number of bytes after which to store the content in a file instead; negative values will result in the default threshold
        tempFileTag - an optional tag that will be incorporated into the name of the temp file if one is created; may be null
        tempDir - the directory where temp files should be created; may be null to use the default temp directory of the JVM; will be created if it doesn't exist
        Throws:
        IOException - if an I/O error occurs
    • Method Detail

      • getLength

        public long getLength()
        Returns the total length of the content.
        Returns:
        the total length of the content
      • getInputStream

        public InputStream getInputStream()
                                   throws IOException
        Returns an input stream for the data. This method may be called multiple times. The returned stream must be closed properly after use!
        Returns:
        an input stream for the data
        Throws:
        IOException - if an I/O error occurs
      • close

        public void close()
        Releases all resources and deletes the temporary file if one has been created.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable