Revert "Revert "DynamicBuffer: implement BDataIO""

This reverts commit 747b401e87.
This commit is contained in:
Ingo Weinhold 2014-07-02 21:29:53 +02:00
parent 4260c14601
commit 739f15e144
3 changed files with 14 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2009, Haiku, Inc. All Rights Reserved. * Copyright 2009-2014, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@ -9,10 +9,11 @@
#ifndef _DYNAMIC_BUFFER_H #ifndef _DYNAMIC_BUFFER_H
#define _DYNAMIC_BUFFER_H #define _DYNAMIC_BUFFER_H
#include <DataIO.h>
#include <SupportDefs.h> #include <SupportDefs.h>
class DynamicBuffer { class DynamicBuffer : public BDataIO {
public: public:
DynamicBuffer(size_t initialSize = 0); DynamicBuffer(size_t initialSize = 0);
~DynamicBuffer(); ~DynamicBuffer();
@ -25,11 +26,11 @@ public:
// Insert data at the end of the buffer. The buffer will be increased to // Insert data at the end of the buffer. The buffer will be increased to
// accomodate the data if needed. // accomodate the data if needed.
status_t Insert(const void* data, size_t size); status_t Write(const void* data, size_t size);
// Remove data from the start of the buffer. Move the buffer start // Remove data from the start of the buffer. Move the buffer start
// pointer to point to the data following it. // pointer to point to the data following it.
status_t Remove(void* data, size_t size); ssize_t Read(void* data, size_t size);
// Return a pointer to the underlying buffer. Note this will not // Return a pointer to the underlying buffer. Note this will not
// necessarily be a pointer to the start of the allocated memory as the // necessarily be a pointer to the start of the allocated memory as the

View File

@ -68,7 +68,7 @@ DynamicBuffer::InitCheck() const
status_t status_t
DynamicBuffer::Insert(const void* data, size_t size) DynamicBuffer::Write(const void* data, size_t size)
{ {
if (fInit != B_OK) if (fInit != B_OK)
return fInit; return fInit;
@ -84,14 +84,14 @@ DynamicBuffer::Insert(const void* data, size_t size)
} }
status_t ssize_t
DynamicBuffer::Remove(void* data, size_t size) DynamicBuffer::Read(void* data, size_t size)
{ {
if (fInit != B_OK) if (fInit != B_OK)
return fInit; return fInit;
if (fDataStart + size > fDataEnd) if (size > Size())
return B_BUFFER_OVERFLOW; size = Size();
memcpy(data, fBuffer + fDataStart, size); memcpy(data, fBuffer + fDataStart, size);
fDataStart += size; fDataStart += size;
@ -99,7 +99,7 @@ DynamicBuffer::Remove(void* data, size_t size)
if (fDataStart == fDataEnd) if (fDataStart == fDataEnd)
fDataStart = fDataEnd = 0; fDataStart = fDataEnd = 0;
return B_OK; return size;
} }

View File

@ -54,7 +54,7 @@ BNetBuffer::BNetBuffer(BMessage* archive) :
&bufferSize) == B_OK) { &bufferSize) == B_OK) {
fImpl = new (std::nothrow) DynamicBuffer(bufferSize); fImpl = new (std::nothrow) DynamicBuffer(bufferSize);
if (fImpl != NULL) { if (fImpl != NULL) {
status_t result = fImpl->Insert(bufferPtr, bufferSize); status_t result = fImpl->Write(bufferPtr, bufferSize);
if (result == B_OK) if (result == B_OK)
fInit = fImpl->InitCheck(); fInit = fImpl->InitCheck();
else else
@ -185,7 +185,7 @@ BNetBuffer::AppendString(const char* data)
status_t status_t
BNetBuffer::AppendData(const void* data, size_t size) BNetBuffer::AppendData(const void* data, size_t size)
{ {
return fImpl->Insert(data, size); return fImpl->Write(data, size);
} }
@ -332,7 +332,7 @@ BNetBuffer::RemoveString(char* data, size_t size)
status_t status_t
BNetBuffer::RemoveData(void* data, size_t size) BNetBuffer::RemoveData(void* data, size_t size)
{ {
return fImpl->Remove(data, size); return fImpl->Read(data, size);
} }