Kodi Documentation 22.0
Kodi is an open source media player and entertainment hub.
No Matches
XbmcCommons::Buffer Class Reference

#include <Buffer.h>

Inheritance diagram for XbmcCommons::Buffer:
CharVecBuffer DRM::CharVecBuffer

Public Member Functions

 Buffer ()
 Buffer (void *buffer_, size_t bufferSize)
 Buffer (size_t bufferSize)
 Buffer (const Buffer &buf)=default
Bufferoperator= (const Buffer &buf)
Bufferallocate (size_t bufferSize)
Bufferflip ()
Bufferclear ()
Bufferrewind ()
size_t remaining () const
Bufferput (const void *src, size_t bytes)
Bufferget (void *dest, size_t bytes)
unsigned char * data () const
unsigned char * curPosition () const
BuffersetPosition (size_t position)
Bufferforward (size_t positionIncrement)
size_t limit () const
size_t capacity () const
size_t position () const
BufferputString (const char *str)
BufferputString (const std::string &str)
std::string getString ()
std::string getString (size_t length)
char * getCharPointerDirect ()

Detailed Description

This class is based on the java java.nio.Buffer class however, it does not implement the 'mark' functionality.

[ the following is borrowed from the javadocs for java.nio.Buffer where it applies to this class]:

A buffer is a linear, finite sequence of elements of a unspecified types. Aside from its content, the essential properties of a buffer are its capacity, limit, and position: A buffer's capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes.

A buffer's limit is the index of the first element that should not be read or written. A buffer's limit is never negative and is never greater than its capacity.

A buffer's position is the index of the next element to be read or written. A buffer's position is never negative and is never greater than its limit.


The following invariant holds for the mark, position, limit, and capacity values:

0 <= mark <= position <= limit <= capacity

A newly-created buffer always has a position of zero and a limit set to the capacity. The initial content of a buffer is, in general, undefined.

Example: Buffer buffer(1024); buffer.putInt(1).putString("hello there").putLongLong( ((long long)2)^40 ); buffer.flip(); std::cout << "buffer contents:" << buffer.getInt() << ", "; std::cout << buffer.getCharPointerDirect() << ", "; std::cout << buffer.getLongLong() << std::endl;

Note: the 'gets' are sensitive to the order-of-operations. Therefore, while the above is correct, it would be wrong to chain the output as follows:

std::cout << "buffer contents:" << buffer.getInt() << ", " << std::cout << buffer.getCharPointerDirect() << ", " << buffer.getLongLong() << std::endl;

This would result in the get's executing from right to left and therefore would produce totally erroneous results. This is also a problem when the values are passed to a method as in:

printf("buffer contents: %d, \"%s\", %ll\n", buffer.getInt(), buffer.getCharPointerDirect(), buffer.getLongLong());

This would also produce erroneous results as they get's will be evaluated from right to left in the parameter list of printf.

Constructor & Destructor Documentation

◆ Buffer() [1/4]

XbmcCommons::Buffer::Buffer ( )

Construct an uninitialized buffer instance, perhaps as an lvalue.

◆ Buffer() [2/4]

XbmcCommons::Buffer::Buffer ( void * buffer_,
size_t bufferSize )

Construct a buffer given an externally managed memory buffer. The ownership of the buffer is assumed to be the code that called this constructor, therefore the Buffer destructor will not free it.

The newly constructed buffer is considered empty and is ready to have data written into it.

If you want to read from the buffer you just created, you can use:

Buffer b = Buffer(buf,bufSize).forward(bufSize).flip();

◆ Buffer() [3/4]

XbmcCommons::Buffer::Buffer ( size_t bufferSize)

Construct a buffer buffer using the size buffer provided. The buffer will be internally managed and potentially shared with other Buffer instances. It will be freed upon destruction of the last Buffer that references it.

◆ Buffer() [4/4]

XbmcCommons::Buffer::Buffer ( const Buffer & buf)

Copy another buffer. This is a "shallow copy" and therefore shares the underlying data buffer with the Buffer it is a copy of. Changes made to the data through this buffer will be seen in the source buffer and vice/vrs. However, each buffer maintains its own indexing.

Member Function Documentation

◆ allocate()

Buffer & XbmcCommons::Buffer::allocate ( size_t bufferSize)

◆ capacity()

size_t XbmcCommons::Buffer::capacity ( ) const

◆ clear()

Buffer & XbmcCommons::Buffer::clear ( )

Clears this buffer. The position is set to zero, the limit is set to the capacity.

Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example:

buf.clear(); // Prepare buffer for reading in.read(buf); // Read data

This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.

◆ curPosition()

unsigned char * XbmcCommons::Buffer::curPosition ( ) const

◆ data()

unsigned char * XbmcCommons::Buffer::data ( ) const


bool  )


char  )


XbmcCommons::Buffer::DEFAULTBUFFERRELATIVERW ( Double ,
double  )


XbmcCommons::Buffer::DEFAULTBUFFERRELATIVERW ( Float ,
float  )


int  )


long  )


XbmcCommons::Buffer::DEFAULTBUFFERRELATIVERW ( LongLong ,
long long  )


XbmcCommons::Buffer::DEFAULTBUFFERRELATIVERW ( Pointer ,
void *  )

◆ flip()

Buffer & XbmcCommons::Buffer::flip ( )

Flips this buffer. The limit is set to the current position and then the position is set to zero.

After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations. For example:

buf.put(magic); // Prepend header in.read(buf); // Read data into rest of buffer buf.flip(); // Flip buffer out.write(buf); // Write header + data to channel

This is used to prepare the Buffer for reading from after it has been written to.

◆ forward()

Buffer & XbmcCommons::Buffer::forward ( size_t positionIncrement)

◆ get()

Buffer & XbmcCommons::Buffer::get ( void * dest,
size_t bytes )

◆ getCharPointerDirect()

char * XbmcCommons::Buffer::getCharPointerDirect ( )

◆ getString() [1/2]

std::string XbmcCommons::Buffer::getString ( )

◆ getString() [2/2]

std::string XbmcCommons::Buffer::getString ( size_t length)

◆ limit()

size_t XbmcCommons::Buffer::limit ( ) const

◆ operator=()

Buffer & XbmcCommons::Buffer::operator= ( const Buffer & buf)

Copy another buffer. This is a "shallow copy" and therefore shares the underlying data buffer with the Buffer it is a copy of. Changes made to the data through this buffer will be seen in the source buffer and vice/vrs. However, each buffer maintains its own indexing.

◆ position()

size_t XbmcCommons::Buffer::position ( ) const

◆ put()

Buffer & XbmcCommons::Buffer::put ( const void * src,
size_t bytes )

◆ putString() [1/2]

Buffer & XbmcCommons::Buffer::putString ( const char * str)

◆ putString() [2/2]

Buffer & XbmcCommons::Buffer::putString ( const std::string & str)

◆ remaining()

size_t XbmcCommons::Buffer::remaining ( ) const

This method provides for the remaining number of bytes that can be read out of the buffer or written into the buffer before it's finished.

◆ rewind()

Buffer & XbmcCommons::Buffer::rewind ( )

This method resets the position to the beginning of the buffer so that it can be either reread or written to all over again.

◆ setPosition()

Buffer & XbmcCommons::Buffer::setPosition ( size_t position)

The documentation for this class was generated from the following file: