Class EntityOutputStream

java.lang.Object
java.io.OutputStream
org.jboss.resteasy.spi.EntityOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class EntityOutputStream extends OutputStream
A stream used for entities. This may buffer, given the threshold value, in memory or be written to a file. The file should be deleted after the resulting input stream is no longer referenced.

Please note that if toInputStream() is not invoked the file must be manually deleted.

Author:
James R. Perkins
  • Field Details

    • lock

      protected final Object lock
  • Constructor Details

    • EntityOutputStream

      public EntityOutputStream()
      Creates a new entity stream with the maximum in memory threshold of a default value.
    • EntityOutputStream

      public EntityOutputStream(Threshold memoryThreshold)
      Creates a new entity stream with the maximum in memory threshold of the supplied value.
      Parameters:
      memoryThreshold - the maximum number of bytes to hold in memory
    • EntityOutputStream

      public EntityOutputStream(Threshold memoryThreshold, Supplier<String> filePrefix)
      Creates a new entity stream with the maximum in memory threshold and a file prefix to be used if the stream needs to be written to a file due to the threshold.
      Parameters:
      memoryThreshold - the maximum number of bytes to hold in memory
      filePrefix - the file prefix if a file is created
    • EntityOutputStream

      public EntityOutputStream(Threshold memoryThreshold, Path tmpDir, Supplier<String> filePrefix)
      Creates a new entity stream with the maximum in memory threshold and a file prefix to be used if the stream needs to be written to a file due to the threshold.
      Parameters:
      memoryThreshold - the maximum number of bytes to hold in memory
      tmpDir - the temporary directory used for files, can be null to use the default
      filePrefix - the file prefix if a file is created
    • EntityOutputStream

      public EntityOutputStream(Threshold memoryThreshold, Path tmpDir, Threshold fileThreshold, Supplier<String> filePrefix)
      Creates a new entity stream with the maximum in memory threshold and a file prefix to be used if the stream needs to be written to a file due to the threshold.
      Parameters:
      memoryThreshold - the maximum number of bytes to hold in memory
      tmpDir - the temporary directory used for files, can be null to use the default
      filePrefix - the file prefix if a file is created
  • Method Details

    • write

      public void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • isClosed

      public boolean isClosed()
      Checks if the output stream has been closed.
      Returns:
      true if the output stream has been closed, otherwise false
    • toInputStream

      public InputStream toInputStream() throws IOException
      Creates an input stream from this output stream.

      Note that when invoking this method close() is invoked first.

      Returns:
      an input stream for reading the output
      Throws:
      IOException - if an error occurs creating the input stream
    • getFile

      protected Path getFile()
      Returns the file if one was written to. If this is in memory, this method will return null.

      Important: You must use the lock when accessing this method.

      Returns:
      the file, if it exists, or null
    • getAndClearMemory

      protected byte[] getAndClearMemory()
      Returns the data in memory if getFile() returns null.

      Important: You must use the lock when accessing this method.

      Returns:
      the data in memory or an empty array
    • checkExported

      protected void checkExported(Supplier<? extends RuntimeException> errorMessage)
    • getContentLength

      public long getContentLength() throws IOException
      The length of the length of the content written.
      Returns:
      the length of the content written
      Throws:
      IOException - if there is an error determining the length of the content