firstworks Programming with Rudiments using the bytebuffer class

Using the bytebuffer class

The bytebuffer class can be used to store binary data of arbitrary length.

Internally, the class stores this data in a series of extents which are coalesced when getBuffer() or detachBuffer() are called. The size of the initial and incremental extents may be defined in the constructor.

When cleared, all extents except for the initial extent are freed.

It is generally more efficient to reuse the same bytebuffer over and over than to allocate a new one for each operation.

The bytebuffer class provides methods for appending data, writing to arbitrary locations, clearing and truncating the buffer, getting the data stored in the buffer, getting the number of bytes stored in the buffer, and detaching the data from the buffer.

When data is detached, it is no longer associated with the buffer and must be freed by the calling program.

> #include <rudiments/bytebuffer.h>
> #include <rudiments/stdio.h>
> int main(int argc, const char **argv) {
> bytebuffer bb;
> // append strings, numbers, and characters...
> bb.append("hello, ")->append("goodbye, ");
> bb.append((uint64_t)1)->append(", ");
> bb.append((double)1.234)->append('.');
> stdoutput.write("after initial append:\n");
> stdoutput.write(" contents: ");
> stdoutput.safePrint(bb.getBuffer(),bb.getSize());
> stdoutput.write('\n');
> stdoutput.printf(" size: %d\n",bb.getSize());
> stdoutput.write('\n');
> // clear the buffer and append more data to it
> const unsigned char data[]={1,2,3,4,5,6,7,8};
> bb.clear();
> bb.append(data,sizeof(data));
> // overwrite the first 5 numbers...
> bb.setPosition(0);
> const unsigned char newdata[]={8,7,6,5};
> bb.write(newdata,sizeof(newdata));
> stdoutput.write("after overwrite:\n");
> stdoutput.write(" contents: ");
> stdoutput.safePrint(bb.getBuffer(),bb.getSize());
> stdoutput.write('\n');
> stdoutput.printf(" size: %d\n",bb.getSize());
> stdoutput.write('\n');
> // truncate the string after the first 2 numbers
> bb.truncate(3);
> stdoutput.write("after truncate:\n");
> stdoutput.write(" contents: ");
> stdoutput.safePrint(bb.getBuffer(),bb.getSize());
> stdoutput.write('\n');
> stdoutput.printf(" size: %d\n",bb.getSize());
> stdoutput.write('\n');
> // detach the string
> size_t ddatasize=bb.getSize();
> unsigned char *ddata=bb.detachBuffer();
> stdoutput.write("after detach:\n");
> stdoutput.write(" contents: ");
> stdoutput.safePrint(bb.getBuffer(),bb.getSize());
> stdoutput.write('\n');
> stdoutput.printf(" size: %d\n",bb.getSize());
> stdoutput.write(" detached data: ");
> stdoutput.safePrint(ddata,ddatasize);
> stdoutput.write('\n');
> stdoutput.printf(" size: %d\n",ddatasize);
> stdoutput.write('\n');
> delete[] ddata;
> }