Google Custom Search

BBufferIO Class Reference
[Support Kit(libbe.so)]

A buffered adapter for BPositionIO objects. More...

Inheritance diagram for BBufferIO:

BPositionIO BDataIO List of all members.

Public Member Functions

 BBufferIO (BPositionIO *stream, size_t bufferSize=65536L, bool ownsStream=true)
 Initialize a BBufferIO object.
size_t BufferSize () const
 Return the size of the internal buffer.
virtual status_t Flush ()
 Write pending modifications to the stream.
virtual status_t GetSize (off_t *size) const
 Get the size of the object or data.
bool OwnsStream () const
 Tell if the BBufferIO object "owns" the specified stream.
virtual off_t Position () const
 Return the current position in the stream.
void PrintToStream () const
 Print the object to stdout.
virtual ssize_t Read (void *buffer, size_t size)
 Read data from current position.
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.
BPositionIOStream () const
 Return a pointer to the stream specified on construction.
virtual ssize_t Write (const void *buffer, size_t size)
 Write data to the current position.
virtual ssize_t WriteAt (off_t pos, const void *buffer, size_t size)
 Write the specified amount of bytes at the given position.
virtual ~BBufferIO ()
 Free the resources allocated by the object.

Detailed Description

A buffered adapter for BPositionIO objects.

Author:
Stefano Ceccherini <burton666@freemail.it>
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 initialised 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.

Note:
This class is not meant to be used in cases where the original stream requires to be in a consistent state. Neither should this class be used as a way to perform 'atomic' writes, because the object might need to do partial writes if it needs to 'move' the buffer. This happens for instance if the original stream is bigger than the buffer.


Constructor & Destructor Documentation

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.

Parameters:
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.

BBufferIO::~BBufferIO (  )  [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.


Member Function Documentation

size_t BBufferIO::BufferSize (  )  const

Return the size of the internal buffer.

Returns:
The size of the buffer allocated by the object.

status_t BBufferIO::Flush (  )  [virtual]

Write pending modifications to the stream.

Returns:
The amount of bytes written, or if it failed it will return an error code.

status_t BPositionIO::GetSize ( off_t *  size  )  const [virtual, inherited]

Get the size of the object or data.

The default implementation uses Seek() with the SEEK_END flag to determine the size of the buffer. If your data or object has a different way of determining size, reimplement this method.

Please check that NULL is not passed into size if you reimplement it in your class.

Parameters:
[out] size The size of the object is put into this parameter.
Returns:
This method returns B_OK on success or an error code on error.
See also:
Seek()

bool BBufferIO::OwnsStream (  )  const

Tell if the BBufferIO object "owns" the specified stream.

Return values:
true The object "owns" the stream and will destroy it upon destruction.
false The object does not own the stream.
See also:
SetOwnsStream()

off_t BBufferIO::Position (  )  const [virtual]

Return the current position in the stream.

Returns:
The current position as an offset in bytes from the beginning of the stream.
Return values:
B_NO_INIT The object is not associated with a valid BPositionIO stream.

Implements BPositionIO.

ssize_t BPositionIO::Read ( void *  buffer,
size_t  size 
) [virtual, inherited]

Read data from current position.

This method is derived from BDataIO. The default implementation reads data from the current position of the cursor, pointed at by Position(). If you require different behaviour, please look at BDataIO::Read() for what is expected of this method.

Implements BDataIO.

ssize_t BBufferIO::ReadAt ( off_t  pos,
void *  buffer,
size_t  size 
) [virtual]

Read the specified amount of bytes at the given position.

Parameters:
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.
Returns:
The amount of bytes actually read, or an error code.
Return values:
B_NO_INIT The object is not associated with a valid BPositionIO stream.
B_BAD_VALUE The buffer parameter is not valid.

Implements BPositionIO.

off_t BBufferIO::Seek ( off_t  position,
uint32  seekMode 
) [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.

Parameters:
position The position where you want to seek.
seekMode Can have three values:
  • SEEK_SET. The position passed is an offset from the beginning of the stream; in other words, the current position is set to position. For this mode, position should be a positive value.
  • SEEK_CUR. The position argument is an offset from the current position; the value of the argument is added to the current position.
  • SEEK_END. The position argument is an offset from the end of the stream. In this mode the position argument should be negative (or zero).
Returns:
The current position as an offset in bytes from the beginning of the stream.
Return 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.

Parameters:
owns_stream If you pass true, the object will delete the stream upon destruction, if you pass false it will not.

status_t BBufferIO::SetSize ( off_t  size  )  [virtual]

Call the SetSize() function of the assigned BPositionIO stream.

Parameters:
size The new size of the BPositionIO object.
Return values:
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.

Returns:
A pointer to the BPositionIO stream specified on construction.

ssize_t BPositionIO::Write ( const void *  buffer,
size_t  size 
) [virtual, inherited]

Write data to the current position.

This method is derived from BDataIO. The default implementation writes data to the current position of the cursor, pointed at by Position(). If you require different behaviour, please look at BDataIO::Write() for what is expected of this method.

Implements BDataIO.

ssize_t BBufferIO::WriteAt ( off_t  pos,
const void *  buffer,
size_t  size 
) [virtual]

Write the specified amount of bytes at the given position.

Parameters:
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.
Returns:
The amount of bytes actually written, or an error code.
Return values:
B_NO_INIT The object is not associated with a valid BPositionIO stream.
B_BAD_VALUE The buffer parameter is not valid.

Implements BPositionIO.


The Haiku Book pre-R1 - BBufferIO Class Reference
Generated on 14 Feb 2008