Monday, August 12, 2013

Bytebuffer class in C++

I was wondering if a class similar to Java ByteBuffer is available in C++ is as well. Strinstream works, but it doesn't have the control what Java ByteBuffer can offer. It does a very simple job. But if a class is already available, then it becomes convenient for the developers. There are many libraries that can generate the data encoding code for us. Good examples are Apache Thrift and Google protobuf.  But there are many softwares requiring messages to be encoded in some other formats hoping they won't be accessible only by the languages supported by Thrift, Protobuf etc.  

My implementation for ByteBuffer in C++ is accessible at this github link.
There is a test program (test.cpp) which demonstrates the operation.
 This can be compiled as shown below:
$g++ -I .  test.cpp bytebuffer.cpp bytebuffer_exception.cpp

Then run it as shown below:
$./a.out

The source for the test program is pasted below:

#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <bytebuffer.hpp>
#include <stdio.h>

using namespace GeetPutula;
using namespace std;

int main()
{
    ByteBuffer buffer(256, ByteBuffer::LITTLE);
    buffer.putInt32(102);
    buffer.rewind();
    int32_t val = buffer.getInt32();
    cout << val << endl;
    val = buffer.getInt32();
    cout << val << endl;
    size_t position = buffer.currentPosition();
    cout << buffer.position(1024) << endl;
    cout << buffer.position(1024) << endl;
    if (buffer.putInt32(100) == false) {
        cout << "Worked as expected \n";
    }
    buffer.position(position);
    buffer.putBytes((void *)"this is my string", strlen("this is my string") + 1);
    void *buf = malloc(strlen("this is my string") + 1);
    buffer.position(position);
    buffer.readBytes(buf, strlen("this is my string") + 1);
    cout << (char *) buf << endl;
    free(buf);
    position = buffer.currentPosition();
    buffer.putInt16(45);
    buffer.position(position);
    cout << buffer.getInt16() << endl;
    cout << "Current position " << buffer.currentPosition() << endl;
    position = buffer.currentPosition();
    buffer.putInt64(-123456789123400);
    buffer.position(position);
    cout << buffer.getInt64() << endl;
    position = buffer.currentPosition() ;
    cout << "Current position " << position << endl;
    buffer.putFloat(-1223.3233);
    buffer.position(position);
    float f  = buffer.getFloat() ;
    printf("%f floattttt\n", f);
    
    position = buffer.currentPosition() ;
    cout << "Current position " << position << endl;
    buffer.putDouble(-1223879967.3233129);
    buffer.position(position);
    cout << buffer.getDouble() << endl;
    buffer.position(position);
    double x = buffer.getDouble();
    printf("%lf \n", x);
    return 0;
}

1 comment:

  1. Hello Nipun, I find this class very usefull, I see you are using "void *" insted of vector of chars which i belive is the right way but can you please comment why do you have to used " size_t position" in almost all of your methods as an argument. Should not be possible to omit that argument i mean the adjustPosition method only assigns the current position. Why do we need that argoment always in the call?

    ReplyDelete