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();
ByteBuffer(size_t size);
ByteBuffer(const char *data, size_t dataLength);
~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);
void append(const char *data, size_t dataSize);
bool insert(const char *data, size_t dataSize, size_t offset = 0);
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;
};
}
#endif
Buffer.cpp
#include "Buffer.h"
#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) {
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;
}
}
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;
}
测试结果