Package org.jboss.resteasy.spi
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 theresulting input streamis no longer referenced.Please note that if
toInputStream()is not invoked the file must be manually deleted.- Author:
- James R. Perkins
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classEntityOutputStream.FileCleaner
-
Constructor Summary
Constructors Constructor Description EntityOutputStream()Creates a new entity stream with the maximum in memory threshold of a default value.EntityOutputStream(Threshold memoryThreshold)Creates a new entity stream with the maximum in memory threshold of the supplied value.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.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.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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckExported(Supplier<? extends RuntimeException> errorMessage)voidclose()voidflush()protected byte[]getAndClearMemory()Returns the data in memory ifgetFile()returnsnull.longgetContentLength()The length of the length of the content written.protected PathgetFile()Returns the file if one was written to.booleanisClosed()Checks if the output stream has been closed.InputStreamtoInputStream()Creates an input stream from this output stream.voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
lock
protected final Object lock
-
-
Constructor Detail
-
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 memoryfilePrefix- 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 memorytmpDir- the temporary directory used for files, can benullto use the defaultfilePrefix- 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 memorytmpDir- the temporary directory used for files, can benullto use the defaultfilePrefix- the file prefix if a file is created
-
-
Method Detail
-
write
public void write(int b) throws IOException- Specified by:
writein classOutputStream- Throws:
IOException
-
write
public void write(byte[] b) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
flush
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
isClosed
public boolean isClosed()
Checks if the output stream has been closed.- Returns:
trueif the output stream has been closed, otherwisefalse
-
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 returnnull.Important: You must use the
lockwhen accessing this method.- Returns:
- the file, if it exists, or
null
-
getAndClearMemory
protected byte[] getAndClearMemory()
Returns the data in memory ifgetFile()returnsnull.Important: You must use the
lockwhen 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 IOExceptionThe 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
-
-