Package com.storedobject.common
Class FileBuffer
java.lang.Object
com.storedobject.common.FileBuffer
- All Implemented Interfaces:
ResourceOwner
A file-mapped data buffer with index-based access to data for writing and then, for reading.
Data is just byte arrays of arbitrary length.
- Author:
- Syam
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
begin()
This method must be called before writing anything.void
close()
Close this buffer.void
end()
This method must be called when writing is completed.Apply a filter to the buffer.Get the "resource" owned by this buffer.byte[]
read
(int index) Read data from a specified index.int
size()
Get the size of this buffer in terms of number of indices it holds.sort
(Comparator<byte[]> comparator) Sort the data according the comparator passed.void
swap
(int firstIndex, int secondIndex) Swap positions of 2 data chunks.void
write
(byte[] data) Write something at the current index.
-
Constructor Details
-
FileBuffer
public FileBuffer()Constructor.
-
-
Method Details
-
getResource
Get the "resource" owned by this buffer.- Specified by:
getResource
in interfaceResourceOwner
- Returns:
- The "resource" owned by this buffer.
-
begin
public void begin()This method must be called before writing anything. -
end
public void end()This method must be called when writing is completed. After this, the buffer will be in read-only mode and any attempt to write will cause errors. -
write
-
close
public void close()Close this buffer. No more reading or writing is possible after this. -
size
public int size()Get the size of this buffer in terms of number of indices it holds. Each index may be pointing to varying chunks of data.- Returns:
- Number of indices written available in the buffer.
-
read
public byte[] read(int index) Read data from a specified index.- Parameters:
index
- Index.- Returns:
- Data read.
-
swap
public void swap(int firstIndex, int secondIndex) Swap positions of 2 data chunks.- Parameters:
firstIndex
- First index.secondIndex
- Second index.
-
sort
Sort the data according the comparator passed.- Parameters:
comparator
- Comparator to compare the data chunks.- Returns:
- Sorter buffer.
-
filter
Apply a filter to the buffer.- Parameters:
filter
- Filter to be applied.- Returns:
- Filtered buffer (if the filter is
null
or if all the data chunks are satisfying the filter, then, the same buffer will be returned).
-