Package org.bedework.util.servlet.io
Class PooledBufferedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.bedework.util.servlet.io.PooledBufferedOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class PooledBufferedOutputStream extends OutputStream
See if we can't manage buffers better than the standard java classes - at least for the specific use we have. We are generating a lot of output and causing a lot of expansion and copying.This may result in a lot of JVM churn. See if a pool of buffers can improve matters.
- Author:
- Mike Douglass
-
-
Field Summary
Fields Modifier and Type Field Description protected intcountThe number of valid bytes in the virtual buffer.
-
Constructor Summary
Constructors Constructor Description PooledBufferedOutputStream()Creates a new pooled buffered output stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closing can have no effect as we need to access the buffers afterwards.InputStreamgetInputStream()Get an InputStream for the bytes in the buffer.voidrelease()This really release buffers back to the pool.intsize()Returns the current size of the buffer.byte[]toByteArray()Creates a newly allocated byte array.StringtoString()Converts the buffer's contents into a string decoding bytes using the platform's default character set.StringtoString(String charsetName)Converts the buffer's contents into a string by decoding the bytes using the specifiedcharsetName.voidwrite(byte[] b, int off, int len)voidwrite(int b)Writes the specified byte to this byte array output stream.voidwriteTo(OutputStream out)Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method usingout.write(buf, 0, count).-
Methods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
-
-
-
Method Detail
-
write
public void write(int b)
Writes the specified byte to this byte array output stream.- Specified by:
writein classOutputStream- Parameters:
b- the byte to be written.
-
write
public void write(byte[] b, int off, int len) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
writeTo
public void writeTo(OutputStream out) throws IOException
Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method usingout.write(buf, 0, count).- Parameters:
out- the output stream to which to write the data.- Throws:
IOException- if an I/O error occurs.
-
getInputStream
public InputStream getInputStream() throws IOException
Get an InputStream for the bytes in the buffer. No guarantees if writes take place after this is called.- Returns:
- InputStream
- Throws:
IOException
-
toByteArray
public byte[] toByteArray()
Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.- Returns:
- the current contents of this output stream, as a byte array.
- See Also:
ByteArrayOutputStream.size()
-
size
public int size()
Returns the current size of the buffer.- Returns:
- the value of the
countfield, which is the number of valid bytes in this output stream. - See Also:
ByteArrayOutputStream.count
-
toString
public String toString()
Converts the buffer's contents into a string decoding bytes using the platform's default character set. The length of the new String is a function of the character set, and hence may not be equal to the size of the buffer.This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform's default character set. The CharsetDecoder class should be used when more control over the decoding process is required.
-
toString
public String toString(String charsetName) throws UnsupportedEncodingException
Converts the buffer's contents into a string by decoding the bytes using the specifiedcharsetName. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. The
CharsetDecoderclass should be used when more control over the decoding process is required.- Parameters:
charsetName- the name of a supported charset- Returns:
- String decoded from the buffer's contents.
- Throws:
UnsupportedEncodingException- If the named charset is not supported- Since:
- JDK1.1
-
close
public void close() throws IOExceptionClosing can have no effect as we need to access the buffers afterwards. The methods in this class can be called after the stream has been closed without generating an IOException.The release method MUST be called for this class to release the buffers.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
release
public void release() throws IOExceptionThis really release buffers back to the pool. MUST be called to gain the benefit of pooling.- Throws:
IOException
-
-