A buffered adapter for BPositionIO objects. More...
Inherits BPositionIO.
Public Member Functions | |
BBufferIO (BPositionIO *stream, size_t bufferSize=65536L, bool ownsStream=true) | |
Initialize a BBufferIO object. | |
virtual | ~BBufferIO () |
Free the resources allocated by the object. | |
size_t | BufferSize () const |
Return the size of the internal buffer. | |
virtual status_t | Flush () |
Write pending modifications to the stream. | |
bool | OwnsStream () const |
Return whether or not the BBufferIO object "owns" the stream. | |
virtual off_t | Position () const |
Return the current position in the stream. | |
void | PrintToStream () const |
Print the object to standard output. | |
virtual ssize_t | ReadAt (off_t pos, void *buffer, size_t size) |
Read the specified amount of bytes at the given position. | |
virtual off_t | Seek (off_t position, uint32 seekMode) |
Set the position in the stream. | |
void | SetOwnsStream (bool ownsStream) |
Set the owns_stream property of the object. | |
virtual status_t | SetSize (off_t size) |
Call the SetSize() function of the assigned BPositionIO stream. | |
BPositionIO * | Stream () const |
Return a pointer to the stream specified on construction. | |
virtual ssize_t | WriteAt (off_t pos, const void *buffer, size_t size) |
Write the specified amount of bytes at the given position. | |
Public Member Functions inherited from BPositionIO | |
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. | |
Public Member Functions inherited from BDataIO | |
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 buffered adapter for BPositionIO objects.
This class differs from other classes derived from BPositionIO in a sense that it does not actually provide an actual entity to be read or written to, but rather acts like a "frontend" to a stream. This class especially comes in handy when working with files that are constantly written and rewritten and where you want do this writing buffered so that the hard disk or the network will not have to be accessed so frequently.
This class works as follows. After constructing a BBufferIO object that you want to be buffered, you can create this object. The constructor takes a stream parameter that points to the object to be buffered.
You then use this object as a proxy to the resource you want to read of or write to. As soon as you use ReadAt(), the buffer will be initialized to the contents of the original stream, and subsequent calls to the positions within the buffer will not be routed to the original stream. In the same way WriteAt() will change the data in the buffer, but not in the actual stream. In order to flush the changes to the original stream, use the Flush() method. Deleting the object when you are done with it will also flush the stream and update the original stream.
BBufferIO::BBufferIO | ( | BPositionIO * | stream, |
size_t | bufferSize = 65536L , |
||
bool | ownsStream = true |
||
) |
Initialize a BBufferIO object.
The constructor will create a buffer of the given size and associate the object with the given BPositionIO stream.
stream | A pointer to a BPositionIO object. |
bufferSize | The size of the buffer that the object will allocate and use. |
ownsStream | Specifies if the object will delete the stream on destruction. |
|
virtual |
Free the resources allocated by the object.
Flush pending changes to the stream and free the allocated memory. If the owns_stream
property is true
, the destructor also deletes the stream associated with the BBufferIO object.
size_t BBufferIO::BufferSize | ( | ) | const |
Return the size of the internal buffer.
|
virtual |
Write pending modifications to the stream.
Reimplemented from BDataIO.
bool BBufferIO::OwnsStream | ( | ) | const |
Return whether or not the BBufferIO object "owns" the stream.
true | The object "owns" the stream and will destroy it upon destruction. |
false | The object does not own the stream. |
|
virtual |
Return the current position in the stream.
B_NO_INIT | The object is not associated with a valid BPositionIO stream. |
Implements BPositionIO.
void BBufferIO::PrintToStream | ( | ) | const |
Print the object to standard output.
|
virtual |
Read the specified amount of bytes at the given position.
pos | The offset into the stream where to read. |
buffer | A pointer to a buffer where to copy the read data. |
size | The amount of bytes to read. |
B_NO_INIT | The object is not associated with a valid BPositionIO stream. |
B_BAD_VALUE | The buffer parameter is not valid. |
Implements BPositionIO.
|
virtual |
Set the position in the stream.
Set the position in the stream where the Read() and Write() functions (inherited from BPositionIO) begin reading and writing. How the position argument is understood depends on the seek_mode flag.
position | The position where you want to seek. |
seekMode | Can have three values:
|
B_NO_INIT | The object is not associated with a valid BPositionIO stream. |
Implements BPositionIO.
void BBufferIO::SetOwnsStream | ( | bool | owns_stream | ) |
Set the owns_stream
property of the object.
owns_stream | If you pass true , the object will delete the stream upon destruction, if you pass false it will not. |
|
virtual |
Call the SetSize() function of the assigned BPositionIO stream.
size | The new size of the BPositionIO object. |
B_OK | The stream is resized. |
B_NO_INIT | The object is not associated with a valid BPositionIO stream. |
Reimplemented from BPositionIO.
BPositionIO * BBufferIO::Stream | ( | ) | const |
Return a pointer to the stream specified on construction.
|
virtual |
Write the specified amount of bytes at the given position.
pos | The offset into the stream where to write. |
buffer | A pointer to a buffer which contains the data to write. |
size | The amount of bytes to write. |
B_NO_INIT | The object is not associated with a valid BPositionIO stream. |
B_BAD_VALUE | The buffer parameter is not valid. |
Implements BPositionIO.