A BPositionIO derived class that creates a memory buffer. More...
Inherits BPositionIO.
Public Member Functions | |
BMallocIO () | |
Create a new memory buffer with block size 256. | |
virtual | ~BMallocIO () |
Destroy the object and free the internal buffer. | |
const void * | Buffer () const |
Return a pointer to the internal buffer. | |
size_t | BufferLength () const |
Return the number of bytes in the buffer. | |
virtual off_t | Position () const |
Return the position of the cursor. | |
virtual ssize_t | ReadAt (off_t position, void *buffer, size_t size) |
Read data at a certain position. | |
virtual off_t | Seek (off_t position, uint32 seekMode) |
Move the cursor to a given position. | |
void | SetBlockSize (size_t blockSize) |
Change the block size to a certain value. | |
virtual status_t | SetSize (off_t size) |
Change the size of the buffer. | |
virtual ssize_t | WriteAt (off_t position, const void *buffer, size_t size) |
Write data to a certain position. | |
![]() | |
BPositionIO () | |
This constructor does nothing. | |
virtual | ~BPositionIO () |
This destructor does nothing. | |
virtual status_t | GetSize (off_t *size) const |
Get the size of the object or data. | |
virtual off_t | Position () const =0 |
Pure virtual to return the current position of the cursor. | |
virtual ssize_t | Read (void *buffer, size_t size) |
Read data from current position. | |
virtual ssize_t | ReadAt (off_t position, void *buffer, size_t size)=0 |
Pure virtual to read data from a certain position. | |
status_t | ReadAtExactly (off_t position, void *buffer, size_t size, size_t *_bytesRead=NULL) |
Reads an exact amount of data from the object at the specified position into a buffer. | |
virtual off_t | Seek (off_t position, uint32 seekMode)=0 |
Pure virtual to move the cursor to a certain position. | |
virtual status_t | SetSize (off_t size) |
Set the size of the object or data. | |
virtual ssize_t | Write (const void *buffer, size_t size) |
Write data to the current position. | |
virtual ssize_t | WriteAt (off_t position, const void *buffer, size_t size)=0 |
Pure virtual to write data to a certain position. | |
status_t | WriteAtExactly (off_t position, const void *buffer, size_t size, size_t *_bytesWritten=NULL) |
Writes an exact amount of data from a buffer to the object at the specified position. | |
![]() | |
BDataIO () | |
This constructor does nothing. | |
virtual | ~BDataIO () |
This destructor does nothing. | |
virtual status_t | Flush () |
Writes pending data to underlying storage. | |
virtual ssize_t | Read (void *buffer, size_t size) |
Reads data from the object into a buffer. | |
status_t | ReadExactly (void *buffer, size_t size, size_t *_bytesRead=NULL) |
Reads an exact amount of data from the object into a buffer. | |
virtual ssize_t | Write (const void *buffer, size_t size) |
Writes data from a buffer to the object. | |
status_t | WriteExactly (const void *buffer, size_t size, size_t *_bytesWritten=NULL) |
Writes an exact amount of data from a buffer to the object. | |
A BPositionIO derived class that creates a memory buffer.
This class creates a memory buffer and provides a BPositionIO interface to work on it. The memory buffer grows and shrinks automatically. This is especially useful if you want to use a method or function that works on an object derived from BPositionIO and you want to do something with the resulting data, or it could be useful if you want to read and write to memory in a safe way, since this class has boundary checking.
BMallocIO allocates a buffer based on a certain block size. This provides a mechanism that will prevent it from needing to allocate new memory too often. The default block size is 256 bytes, you can change it with SetBlockSize(). If you are sure you are going to use a bigger buffer, change the block size so that you won't have to allocate more memory too often, especially if you use this class in performance-critical code.
If you require a BPositionIO derived object that works on buffers you provide, have a look at BMemoryIO.
BMallocIO::BMallocIO | ( | ) |
|
virtual |
Destroy the object and free the internal buffer.
const void * BMallocIO::Buffer | ( | ) | const |
Return a pointer to the internal buffer.
As with any pointer to internal buffers the Haiku API exposes, make sure you don't change anything since it doesn't belong to you.
size_t BMallocIO::BufferLength | ( | ) | const |
Return the number of bytes in the buffer.
This number doesn't have to be the same size as the buffer is. Because memory is allocated in blocks the actual size of the buffer may be greater, but this method only returns the number of bytes that are actually used.
|
virtual |
|
virtual |
Read data at a certain position.
[in] | pos | Offset into the data where to read from. |
[out] | buffer | The buffer to copy the read bytes in. |
[in] | size | Size of the buffer. |
B_BAD_VALUE
if the provided buffer is invalid.Implements BPositionIO.
|
virtual |
Move the cursor to a given position.
position | The position to move the cursor to. |
seekMode | The mode determines where the cursor is placed. Possibilities:
|
Implements BPositionIO.
void BMallocIO::SetBlockSize | ( | size_t | blockSize | ) |
Change the block size to a certain value.
This class allocates memory in blocks. If you are in performance-critical code you might want to tweak this setting to create a better performance in case you know you are going to allocate more than the default block size of 256.
blockSize | The new block size. |
|
virtual |
Change the size of the buffer.
This method changes the size of the current buffer. If size is smaller than the current size, the data will be cleared.
size | The new size of the buffer. |
B_OK | Resizing the data succeeded. |
B_NO_MEMORY | Failed to allocate the necessary memory. |
Reimplemented from BPositionIO.
|
virtual |
Write data to a certain position.
pos | Offset into the data where to write to. |
buffer | The buffer to copy from. |
size | The size of the buffer. |
B_BAD_VALUE
if the provided. buffer is invalid.Implements BPositionIO.