BPicture: Fix archive constructor.
[haiku.git] / src / kits / network / libnetapi / DynamicBuffer.cpp
blobb64fd80bfeb41c21a5908b107c37ecbc0a8e2933
1 /*
2 * Copyright 2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Bruno Albuquerque, bga@bug-br.org.br
7 */
9 #include "DynamicBuffer.h"
11 #include <stdio.h>
12 #include <string.h>
14 #include <algorithm>
16 #include <Errors.h>
17 #include <SupportDefs.h>
19 #include <new>
21 DynamicBuffer::DynamicBuffer(size_t initialSize) :
22 fBuffer(NULL),
23 fBufferSize(0),
24 fDataStart(0),
25 fDataEnd(0),
26 fInit(B_NO_INIT)
28 fBuffer = new (std::nothrow) unsigned char[initialSize];
29 if (fBuffer != NULL) {
30 fBufferSize = initialSize;
31 fInit = B_OK;
36 DynamicBuffer::~DynamicBuffer()
38 delete[] fBuffer;
39 fBufferSize = 0;
40 fDataStart = 0;
41 fDataEnd = 0;
45 DynamicBuffer::DynamicBuffer(const DynamicBuffer& buffer) :
46 fBuffer(NULL),
47 fBufferSize(0),
48 fDataStart(0),
49 fDataEnd(0),
50 fInit(B_NO_INIT)
52 fInit = buffer.fInit;
53 if (fInit == B_OK) {
54 status_t result = _GrowToFit(buffer.fBufferSize, true);
55 if (result == B_OK) {
56 memcpy(fBuffer, buffer.fBuffer, fBufferSize);
57 fDataStart = buffer.fDataStart;
58 fDataEnd = buffer.fDataEnd;
59 } else
60 fInit = result;
65 status_t
66 DynamicBuffer::InitCheck() const
68 return fInit;
72 ssize_t
73 DynamicBuffer::Write(const void* data, size_t size)
75 if (fInit != B_OK)
76 return fInit;
78 status_t result = _GrowToFit(size);
79 if (result != B_OK)
80 return result;
82 memcpy(fBuffer + fDataEnd, data, size);
83 fDataEnd += size;
85 return (ssize_t)size;
89 ssize_t
90 DynamicBuffer::Read(void* data, size_t size)
92 if (fInit != B_OK)
93 return fInit;
95 size = std::min(size, Size());
96 if (size == 0)
97 return 0;
99 memcpy(data, fBuffer + fDataStart, size);
100 fDataStart += size;
102 if (fDataStart == fDataEnd)
103 fDataStart = fDataEnd = 0;
105 return size;
109 unsigned char*
110 DynamicBuffer::Data() const
112 return fBuffer + fDataStart;
116 size_t
117 DynamicBuffer::Size() const
119 return fDataEnd - fDataStart;
123 size_t
124 DynamicBuffer::BytesRemaining() const
126 return fBufferSize - fDataEnd;
130 void
131 DynamicBuffer::PrintToStream()
133 printf("Current buffer size : %ld\n", fBufferSize);
134 printf("Data start position : %ld\n", fDataStart);
135 printf("Data end position : %ld\n", fDataEnd);
136 printf("Bytes wasted : %ld\n", fDataStart);
137 printf("Bytes available : %ld\n", fBufferSize - fDataEnd);
141 status_t
142 DynamicBuffer::_GrowToFit(size_t size, bool exact)
144 if (size <= fBufferSize - fDataEnd)
145 return B_OK;
147 size_t newSize;
148 if (!exact)
149 newSize = (fBufferSize + size) * 2;
150 else
151 newSize = size;
153 unsigned char* newBuffer = new (std::nothrow) unsigned char[newSize];
154 if (newBuffer == NULL)
155 return B_NO_MEMORY;
157 if (fDataStart != fDataEnd)
158 memcpy(newBuffer, fBuffer + fDataStart, fDataEnd - fDataStart);
160 delete[] fBuffer;
161 fBuffer = newBuffer;
162 fDataEnd -= fDataStart;
163 fDataStart = 0;
164 fBufferSize = newSize;
166 return B_OK;