|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.mortbay.util.TempByteHolder
Temporary buffer for bytes to be used in situations where bytes need to be buffered
but total size of data is not known in advance and may potentially be very large.
Provides easy way to access small buffered data as byte[] or String.
Enables efficient memory-only handling of small data while automatically switching
to temporary file storage when data gets too big to fit in memory buffer.
It is highly efficient for both byte-per-byte and block I/O.
This class is not a FIFO - you can't mix reading and writing infinitely as all data
keep being buffered, not just unread data.
Mixing reads and writes may be inefficient in some situations but is fully supported.
Overall usage strategy: You first write data to the buffer using OutputStream
returned by getOutputStream(), then examine data size using getLength()
and isLarge() and either call getBytes() to get byte[],
getString() to get data as String or getInputStream() to read data using stream.
Instance of TempByteHolder can be safely and efficiently reused by calling clear().
When TempByteHolder is no longer needed you must call close() to ensure underlying
temporary file is closed and deleted.
NOTE: For performance, this class is not synchronized. If you need thread safety,
use synchronized wrapper.
This class can hold up to 2GB of data.
SECURITY NOTE: As data may be written to disk, don't use this for sensitive information.
Constructor Summary | |
TempByteHolder(byte[] byte_array)
Creates a new instance of TempByteHolder using passed byte[] as memory buffer. |
|
TempByteHolder(byte[] byte_array,
int offset,
int prefilled_data_size)
Creates a new instance of TempByteHolder using passed byte[] which contains prefilled data as memory buffer. |
|
TempByteHolder(int in_memory_capacity)
Creates a new instance of TempByteHolder allocating memory buffer of given capacity. |
Method Summary | |
void |
clear()
Erases all unread buffered data and prepares for next use cycle. |
void |
close()
Clears all data and closes/deletes backing temporary file if used. |
void |
finalize()
|
byte[] |
getBytes()
Returns byte[] that holds all buffered data in its first getLength() bytes. |
java.io.InputStream |
getInputStream()
Returns InputSream for reading buffered data. |
int |
getLength()
Returns number of bytes buffered so far. |
java.io.OutputStream |
getOutputStream()
Returns OutputStream filling this buffer. |
java.lang.String |
getString(java.lang.String character_encoding)
Returns buffered data as String using given character encoding. |
boolean |
isLarge()
Tells whether buffered data is small enough to fit in memory buffer so that it can be returned as byte[]. |
void |
readFrom(java.io.InputStream is)
Reads all available data from input stream. |
void |
seek(int offset)
Repositions InputStream at given offset within buffered data. |
void |
setTempDirectory(java.io.File dir)
Override directory to create temporary file in. |
void |
truncate(int offset)
Truncates buffered data to specified size. |
void |
writeTo(java.io.OutputStream os)
Writes efficiently whole content to output stream. |
void |
writeTo(java.io.OutputStream os,
int start_offset,
int length)
Writes efficiently part of the content to output stream. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public TempByteHolder(int in_memory_capacity)
in_memory_capacity
- Size in bytes of memory buffer to allocate.public TempByteHolder(byte[] byte_array)
byte_array
- byte array to be used as memory buffer.public TempByteHolder(byte[] byte_array, int offset, int prefilled_data_size)
byte_array
- byte array to be used as memory buffer.offset
- offset of prefilled data in buffer.prefilled_data_size
- number of bytes that contain valid data.Method Detail |
public void finalize()
finalize
in class java.lang.Object
public void clear()
public void close() throws java.io.IOException
java.io.IOException
- when something goes wrong.public void seek(int offset) throws java.io.IOException
java.io.IOException
- when something goes wrong.public void truncate(int offset) throws java.io.IOException
java.io.IOException
public void setTempDirectory(java.io.File dir) throws java.io.IOException
dir
- File object representing temporary directory.
May be null which means that system default
(java.io.tmpdir system property) should be used.
java.io.IOException
public int getLength()
public boolean isLarge()
getBytes()
,
getString(String)
public byte[] getBytes()
java.lang.IllegalStateException
- when data is too big to be read this way.isLarge()
,
getLength()
,
getString(String)
,
getInputStream()
public java.lang.String getString(java.lang.String character_encoding) throws java.io.UnsupportedEncodingException
character_encoding
- Name of character encoding to use for
converting bytes to String.
java.lang.IllegalStateException
- when data is too large to be read this way.
java.io.UnsupportedEncodingException
- when this encoding is not supported.public java.io.OutputStream getOutputStream()
public java.io.InputStream getInputStream()
public void writeTo(java.io.OutputStream os) throws java.io.IOException
os
- OutputStream to write to
java.io.IOException
public void writeTo(java.io.OutputStream os, int start_offset, int length) throws java.io.IOException
os
- OutputStream to write tostart_offset
- Offset of data fragment to be writtenlength
- Length of data fragment to be written
java.io.IOException
public void readFrom(java.io.InputStream is) throws java.io.IOException
is
-
java.io.IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |