Package org.nuxeo.ecm.core.io.download
Class BufferingServletOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- javax.servlet.ServletOutputStream
-
- org.nuxeo.ecm.core.io.download.BufferingServletOutputStream
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
public class BufferingServletOutputStream extends javax.servlet.ServletOutputStream
AServletOutputStream
that buffers everything untilstopBuffering()
is called.There may only be one such instance per thread.
Buffering is done first in memory, then on disk if the size exceeds a limit.
-
-
Field Summary
Fields Modifier and Type Field Description protected OutputStream
file
static int
INITIAL
Initial memory buffer size.static int
MAX
Maximum memory buffer size, after this a file is used.protected ByteArrayOutputStream
memory
protected boolean
needsClose
protected boolean
needsFlush
protected javax.servlet.ServletOutputStream
outputStream
protected boolean
streaming
Have we stopped buffering to pass writes directly to the output stream.protected File
tmp
protected PrintWriter
writer
-
Constructor Summary
Constructors Constructor Description BufferingServletOutputStream(javax.servlet.ServletOutputStream outputStream)
AServletOutputStream
wrapper that buffers everything untilstopBuffering()
is called.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
This implementation does nothing, we still want to keep the buffer untilstopBuffering()
time.protected void
createTempFile()
void
flush()
This implementation does nothing, we still want to keep buffering and not flush.protected OutputStream
getOutputStream(int len)
Finds the proper output stream where we can writelen
bytes.PrintWriter
getWriter()
boolean
isCommitted()
boolean
isReady()
void
resetBuffer()
void
setWriteListener(javax.servlet.WriteListener writeListener)
void
stopBuffering()
Writes any buffered data to the underlyingOutputStream
and from now on don't buffer anymore.static void
stopBuffering(OutputStream out)
Tells the givenOutputStream
to stop buffering (if it was).void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
-
Methods inherited from class javax.servlet.ServletOutputStream
print, print, print, print, print, print, print, println, println, println, println, println, println, println, println
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
INITIAL
public static final int INITIAL
Initial memory buffer size.- See Also:
- Constant Field Values
-
MAX
public static final int MAX
Maximum memory buffer size, after this a file is used.- See Also:
- Constant Field Values
-
streaming
protected boolean streaming
Have we stopped buffering to pass writes directly to the output stream.
-
needsFlush
protected boolean needsFlush
-
needsClose
protected boolean needsClose
-
outputStream
protected final javax.servlet.ServletOutputStream outputStream
-
writer
protected PrintWriter writer
-
memory
protected ByteArrayOutputStream memory
-
file
protected OutputStream file
-
tmp
protected File tmp
-
-
Constructor Detail
-
BufferingServletOutputStream
public BufferingServletOutputStream(javax.servlet.ServletOutputStream outputStream)
AServletOutputStream
wrapper that buffers everything untilstopBuffering()
is called.stopBuffering()
MUST be called in afinally
statement in order for resources to be closed properly.- Parameters:
outputStream
- the underlying output stream
-
-
Method Detail
-
getWriter
public PrintWriter getWriter()
-
getOutputStream
protected OutputStream getOutputStream(int len) throws IOException
Finds the proper output stream where we can writelen
bytes.- Throws:
IOException
-
createTempFile
protected void createTempFile() throws IOException
- Throws:
IOException
-
write
public void write(int b) throws IOException
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
flush
public void flush() throws IOException
This implementation does nothing, we still want to keep buffering and not flush.- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
close
public void close() throws IOException
This implementation does nothing, we still want to keep the buffer untilstopBuffering()
time.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
isCommitted
public boolean isCommitted()
- Since:
- 11.5
-
resetBuffer
public void resetBuffer()
- Since:
- 11.5
-
stopBuffering
public void stopBuffering() throws IOException
Writes any buffered data to the underlyingOutputStream
and from now on don't buffer anymore.- Throws:
IOException
-
stopBuffering
public static void stopBuffering(OutputStream out) throws IOException
Tells the givenOutputStream
to stop buffering (if it was).- Throws:
IOException
-
isReady
public boolean isReady()
- Specified by:
isReady
in classjavax.servlet.ServletOutputStream
-
setWriteListener
public void setWriteListener(javax.servlet.WriteListener writeListener)
- Specified by:
setWriteListener
in classjavax.servlet.ServletOutputStream
-
-