Class FileBuffer.Segment
- java.lang.Object
-
- org.simpleframework.common.buffer.FileBuffer.Segment
-
- All Implemented Interfaces:
Buffer
- Enclosing class:
- FileBuffer
private class FileBuffer.Segment extends java.lang.Object implements Buffer
TheSegment
object is used to create a segment of the parent buffer. The segment will write to the parent however if can be read as a unique range of bytes starting with the first sequence of bytes appended to the segment. A segment can be used to create a collection of buffers backed by the same underlying file, as is require with multipart uploads.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
closed
This determines if the segment is currently open or closed.private long
first
This is the offset of the first byte within the sequence.private long
last
This is the last byte within the segment for this segment.private Buffer
parent
This is the parent buffer that bytes are to be appended to.private FileBuffer.Segment
segment
This is an internal segment created from this buffer 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.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.long
space()
This determines how much space is left in the buffer.
-
-
-
Field Detail
-
segment
private FileBuffer.Segment segment
This is an internal segment created from this buffer object.
-
parent
private Buffer parent
This is the parent buffer that bytes are to be appended to.
-
first
private long first
This is the offset of the first byte within the sequence.
-
last
private long last
This is the last byte within the segment for this segment.
-
closed
private boolean closed
This determines if the segment is currently open or closed.
-
-
Constructor Detail
-
Segment
public Segment(Buffer parent, long first)
Constructor for theSegment
object. This is used to create a segment from a parent buffer. A segment is a part of the parent buffer and appends its bytes to the parent. It can however be treated as an independent source of bytes.- Parameters:
parent
- this is the parent buffer to be appended tofirst
- this is the offset for the first byte in this
-
-
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.
-
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.
-
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.
-
space
public long space()
This determines how much space is left in the buffer. If there is no limit to the buffer size this will return the maximum long value. Typically this is the capacity minus the length.- Returns:
- this is the space that is available within the buffer
-
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.
-
-