Class FileBuffer
- java.lang.Object
-
- org.simpleframework.common.buffer.FileBuffer
-
- All Implemented Interfaces:
Buffer
class FileBuffer extends java.lang.Object implements Buffer
TheFileBuffer
object is used to create a buffer which will write the appended data to an underlying file. This is typically used for buffers that are too large for to allocate in memory. Data appended to the buffer can be retrieved at a later stage by acquiring theInputStream
for the underlying file. To ensure that excessive file system space is not occupied the buffer files are cleaned every five minutes.- See Also:
FileAllocator
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
FileBuffer.Range
TheRange
object is used to provide a stream that can read a range of bytes from a provided input stream.private class
FileBuffer.Segment
TheSegment
object is used to create a segment of the parent buffer.
-
Field Summary
Fields Modifier and Type Field Description private java.io.OutputStream
buffer
This is the file output stream used for this buffer object.private boolean
closed
This is used to determine if this buffer has been closed.private long
count
This is the number of bytes currently appended to the buffer.private java.io.File
file
This is the path for the file that this buffer appends to.private FileBuffer.Segment
segment
This represents the last file segment that has been created.
-
Constructor Summary
Constructors Constructor Description FileBuffer(java.io.File file)
Constructor for theFileBuffer
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Buffer
allocate()
This is used to allocate a segment within this buffer.Buffer
append(byte[] array)
This is used to append the specified data to the underlying file.Buffer
append(byte[] array, int off, int size)
This is used to append the specified data to the underlying file.void
clear()
This will clear all data from the buffer.void
close()
This method is used to ensure the buffer can be closed.private java.lang.String
convert(java.io.InputStream source, java.lang.String charset, int count)
This method is used to acquire the buffered bytes as a string.java.lang.String
encode()
This method is used to acquire the buffered bytes as a string.java.lang.String
encode(java.lang.String charset)
This method is used to acquire the buffered bytes as a string.long
length()
This is used to provide the number of bytes that have been written to the buffer.java.io.InputStream
open()
This method is used so that a buffer can be represented as a stream of bytes.private java.io.InputStream
open(java.io.File file)
This method is used so that a buffer can be represented as a stream of bytes.
-
-
-
Field Detail
-
buffer
private java.io.OutputStream buffer
This is the file output stream used for this buffer object.
-
segment
private FileBuffer.Segment segment
This represents the last file segment that has been created.
-
file
private java.io.File file
This is the path for the file that this buffer appends to.
-
count
private long count
This is the number of bytes currently appended to the buffer.
-
closed
private boolean closed
This is used to determine if this buffer has been closed.
-
-
Constructor Detail
-
FileBuffer
public FileBuffer(java.io.File file) throws java.io.IOException
Constructor for theFileBuffer
object. This will create a buffer using the provided file. All data appended to this buffer will effectively written to the underlying file. If the appended data needs to be retrieved at a later stage then it can be acquired using the buffers input stream.- Parameters:
file
- this is the file used for the file buffer- Throws:
java.io.IOException
-
-
Method Detail
-
allocate
public Buffer allocate() throws java.io.IOException
This is used to allocate a segment within this buffer. If the buffer is closed this will throw an exception, if however the buffer is still open then a segment is created which will write all appended data to this buffer. However it can be treated as an independent source of data.
-
append
public Buffer append(byte[] array) throws java.io.IOException
This is used to append the specified data to the underlying file. All bytes appended to the file can be consumed at a later stage by acquiring theInputStream
from this buffer. Also if require the data can be encoded as a string object in a required character set.
-
append
public Buffer append(byte[] array, int off, int size) throws java.io.IOException
This is used to append the specified data to the underlying file. All bytes appended to the file can be consumed at a later stage by acquiring theInputStream
from this buffer. Also if require the data can be encoded as a string object in a required character set.
-
encode
public java.lang.String encode() throws java.io.IOException
This method is used to acquire the buffered bytes as a string. This is useful if the contents need to be manipulated as a string or transferred into another encoding. If the UTF-8 content encoding is not supported the platform default is used, however this is unlikely as UTF-8 should be supported.
-
encode
public java.lang.String encode(java.lang.String charset) throws java.io.IOException
This method is used to acquire the buffered bytes as a string. This is useful if the contents need to be manipulated as a string or transferred into another encoding. This will convert the bytes using the specified character encoding format.
-
convert
private java.lang.String convert(java.io.InputStream source, java.lang.String charset, int count) throws java.io.IOException
This method is used to acquire the buffered bytes as a string. This is useful if the contents need to be manipulated as a string or transferred into another encoding. This will convert the bytes using the specified character encoding format.- Parameters:
source
- this is the source stream that is to be encodedcharset
- this is the charset to encode the data withcount
- this is the number of bytes to be encoded- Returns:
- this returns the encoding of the buffer contents
- Throws:
java.io.IOException
-
open
public java.io.InputStream open() throws java.io.IOException
This method is used so that a buffer can be represented as a stream of bytes. This provides a quick means to access the data that has been written to the buffer. It wraps the buffer within an input stream so that it can be read directly.
-
open
private java.io.InputStream open(java.io.File file) throws java.io.IOException
This method is used so that a buffer can be represented as a stream of bytes. This provides a quick means to access the data that has been written to the buffer. It wraps the buffer within an input stream so that it can be read directly.- Parameters:
file
- this is the file used to create the input stream- Returns:
- a stream that can be used to read the buffered bytes
- Throws:
java.io.IOException
-
clear
public void clear() throws java.io.IOException
This will clear all data from the buffer. This simply sets the count to be zero, it will not clear the memory occupied by the instance as the internal buffer will remain. This allows the memory occupied to be reused as many times as is required.
-
close
public void close() throws java.io.IOException
This method is used to ensure the buffer can be closed. Once the buffer is closed it is an immutable collection of bytes and can not longer be modified. This ensures that it can be passed by value without the risk of modification of the bytes.
-
length
public long length()
This is used to provide the number of bytes that have been written to the buffer. This increases as bytes are appended to the buffer. if the buffer is cleared this resets to zero.
-
-