ByteBuffer

Buffer.h

#ifndef __ALIAS_BUFFER_H__
#define __ALIAS_BUFFER_H__

namespace Alias {

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#define DEFAULT_BUFFER_SIZE (4096)

class ByteBuffer final
{
public:
    ByteBuffer();                       // call ByteBuffer(4096);
    ByteBuffer(size_t size);            // size == 0 ? 创建4096大小, 内容为空的buf : size
    ByteBuffer(const char *data, size_t dataLength);  // 如果data为null,则和上面一致
    ~ByteBuffer();

    ByteBuffer(const ByteBuffer& other);
    ByteBuffer& operator=(const ByteBuffer& other);
    char&       operator[](size_t index);

    size_t      set(const char *data, size_t dataSize, size_t offset = 0);  // 在offset之后设为data
    void        append(const char *data, size_t dataSize);
    bool        insert(const char *data, size_t dataSize, size_t offset = 0);   // 在offset之后插入数据而不覆盖之后的数据

    char*       data() { return mBuffer ? mBuffer : nullptr; }
    const char* begin() const { return mBuffer ? mBuffer : nullptr; }    // 返回数据开始地址
    const char* end() const { return mBuffer ? (mBuffer + mDataSize) : nullptr; }      // 返回数据结束地址
    void        resize(size_t newSize);
    size_t      capacity() const { return mCapacity; }
    size_t      size() const { return mDataSize; }
    void        clear();

private:
    size_t      calculate(size_t);
    size_t      getBuffer(size_t size = DEFAULT_BUFFER_SIZE);
    void        freeBuffer();

private:
    char*   mBuffer;
    size_t  mDataSize;
    size_t  mCapacity;
};

} // namespace Alias

#endif // __ALIAS_BUFFER_H__

Buffer.cpp

#include "Buffer.h"

// #define DEBUG
#ifdef DEBUG
    #ifndef SLOGD
        #define SLOGD printf
    #endif
#else
    #ifndef SLOGD
        #define SLOGD(...)
    #endif
#endif

namespace Alias {
ByteBuffer::ByteBuffer() : ByteBuffer(DEFAULT_BUFFER_SIZE)
{
}

ByteBuffer::ByteBuffer(size_t size) : mDataSize(0)
{
    mCapacity = getBuffer(size);
    SLOGD("%s(size_t size) mCapacity = %zu\n", __func__, mCapacity);
}

ByteBuffer::ByteBuffer(const char *data, size_t dataLength) :
    mBuffer(nullptr),
    mCapacity(0),
    mDataSize(0)
{
    set(data, dataLength);
    SLOGD("%s(const char *data, size_t dataLength) mCapacity = %zu\n", __func__, mCapacity);
}

ByteBuffer::ByteBuffer(const ByteBuffer& other)
{
    if (&other == this) {   // is that possible?
        return;
    }
    
    set(other.mBuffer, other.mDataSize);
}

ByteBuffer::~ByteBuffer()
{
    freeBuffer();
}

ByteBuffer& ByteBuffer::operator=(const ByteBuffer& other)
{
    set(other.mBuffer, other.mDataSize);
    return *this;
}

char& ByteBuffer::operator[](size_t index)
{
    if (index < mDataSize) {
        return mBuffer[index];
    }
    return mBuffer[0];
}

size_t ByteBuffer::set(const char *data, size_t dataSize, size_t offset)
{
    char *temp = nullptr;
    if (mCapacity <= (dataSize + offset)) {
        if (offset > 0) {
            temp = (char *)malloc(offset);
            if (temp == nullptr) {
                return 0;
            }
            memcpy(temp, mBuffer, offset);
        }

        freeBuffer();
        mCapacity = getBuffer(calculate(dataSize + offset));
    }
    SLOGD("%s() data(%s), dataSize(%zu), offset(%zu), mBuffer = %p, mCapacity = %zu mDataSize = %zu\n",
            __func__, data, dataSize, offset, mBuffer, mCapacity, mDataSize);

    if (mCapacity > 0 && data != nullptr) {
        if (temp) {
            memcpy(mBuffer, temp, offset);
        }
        memcpy(mBuffer + offset, data, dataSize);
        mDataSize = offset + dataSize;
        return dataSize;
    }
    return 0;
}

void ByteBuffer::append(const char *data, size_t dataSize)
{
    set(data, dataSize, size());
}

bool ByteBuffer::insert(const char *data, size_t dataSize, size_t offset)
{
    if (data == nullptr) {
        return true;
    }

}

void ByteBuffer::resize(size_t newSize)
{
    mCapacity = newSize;
    ByteBuffer tmp(mBuffer, mDataSize);
    freeBuffer();
    size_t realCap = getBuffer(newSize);
    if (realCap > 0) {
        *this = tmp;
    }
}

void ByteBuffer::clear() 
{
    if (mBuffer) {
        memset(mBuffer, 0, mCapacity);
        mDataSize = 0;
    }
}

size_t ByteBuffer::calculate(size_t dataSize)
{
    if (dataSize >= DEFAULT_BUFFER_SIZE) {
        dataSize *= 2;
    } else {
        dataSize = DEFAULT_BUFFER_SIZE;
    }
    return dataSize;
}

size_t ByteBuffer::getBuffer(size_t size)
{
    if (size == 0) {
        size = DEFAULT_BUFFER_SIZE;
    }
    mBuffer = (char *)malloc(size);
    SLOGD("new buffer %p\n", mBuffer);
    if (mBuffer) {
        memset(mBuffer, 0, size);
        return size;
    }

    return 0;
}

void  ByteBuffer::freeBuffer()
{
    if (mBuffer) {
        free(mBuffer);
        SLOGD("free %p\n", mBuffer);
    }
    mBuffer = nullptr;
}

} // namespace Alias

testbuffer.cc

#include <utils/Buffer.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace Alias;

int main()
{
    ByteBuffer buf;
    printf("capacity = %zu\n", buf.capacity());
    printf("data size = %zu\n", buf.size());
    printf("data = %p\n", buf.data());
    printf("1 -----------------\n");

    ByteBuffer tmp("Hello World", 11);
    buf = tmp;
    printf("capacity = %zu\n", buf.capacity());
    printf("data size = %zu\n", buf.size());
    printf("data = %p\n", buf.data());

    printf("2 -----------------\n");
    buf.append("-----", 5);
    printf("capacity = %zu\n", buf.capacity());
    printf("data size = %zu\n", buf.size());
    printf("data = %p\n", buf.data());
    printf("data begin = %p(%s) end = %p\n", buf.begin(), buf.begin(), buf.end());

    printf("3 -----------------\n");
    buf.resize(1024);
    printf("capacity = %zu\n", buf.capacity());
    printf("data size = %zu\n", buf.size());
    printf("data = %p\n", buf.data());
    printf("data begin = %p(%s) end = %p index = 0->%c\n", buf.begin(), buf.begin(), buf.end(), buf[0]);
    buf[6] = 'w';
    printf("data begin = %p(%s) end = %p index = 6->%c\n", buf.begin(), buf.begin(), buf.end(), buf[6]);
    printf("4 -----------------\n");
    buf.clear();
    printf("capacity = %zu\n", buf.capacity());
    printf("data size = %zu\n", buf.size());
    printf("data = %p\n", buf.data());
    printf("data begin = %p(%s) end = %p\n", buf.begin(), buf.begin(), buf.end());
    printf("**************************\n");
    buf = buf;
    return 0;
}

测试结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值