summaryrefslogtreecommitdiff
path: root/lib/common/CollectInBufferStream.cpp
blob: 90e2e7bcb01d1adb019910fa201a419232f0ee1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// --------------------------------------------------------------------------
//
// File
//		Name:    CollectInBufferStream.cpp
//		Purpose: Collect data in a buffer, and then read it out.
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------

#include "Box.h"

#include <string.h>

#include "CollectInBufferStream.h"
#include "CommonException.h"

#include "MemLeakFindOn.h"

#define INITIAL_BUFFER_SIZE	1024
#define MAX_BUFFER_ADDITION	(1024*64)

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::CollectInBufferStream()
//		Purpose: Constructor
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
CollectInBufferStream::CollectInBufferStream()
	: mBuffer(INITIAL_BUFFER_SIZE),
	  mBufferSize(INITIAL_BUFFER_SIZE),
	  mBytesInBuffer(0),
	  mReadPosition(0),
	  mInWritePhase(true)
{
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::~CollectInBufferStream()
//		Purpose: Destructor
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
CollectInBufferStream::~CollectInBufferStream()
{
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::Read(void *, int, int)
//		Purpose: As interface. But only works in read phase
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
int CollectInBufferStream::Read(void *pBuffer, int NBytes, int Timeout)
{
	if(mInWritePhase != false) { THROW_EXCEPTION(CommonException, CollectInBufferStreamNotInCorrectPhase) }
	
	// Adjust to number of bytes left
	if(NBytes > (mBytesInBuffer - mReadPosition))
	{
		NBytes = (mBytesInBuffer - mReadPosition);
	}
	ASSERT(NBytes >= 0);
	if(NBytes <= 0) return 0;	// careful now
	
	// Copy in the requested number of bytes and adjust the read pointer
	::memcpy(pBuffer, ((char*)mBuffer) + mReadPosition, NBytes);
	mReadPosition += NBytes;
	
	return NBytes;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::BytesLeftToRead()
//		Purpose: As interface. But only works in read phase
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
IOStream::pos_type CollectInBufferStream::BytesLeftToRead()
{
	if(mInWritePhase != false) { THROW_EXCEPTION(CommonException, CollectInBufferStreamNotInCorrectPhase) }
	
	return (mBytesInBuffer - mReadPosition);
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::Write(void *, int)
//		Purpose: As interface. But only works in write phase
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
void CollectInBufferStream::Write(const void *pBuffer, int NBytes)
{
	if(mInWritePhase != true) { THROW_EXCEPTION(CommonException, CollectInBufferStreamNotInCorrectPhase) }
	
	// Enough space in the buffer
	if((mBytesInBuffer + NBytes) > mBufferSize)
	{
		// Need to reallocate... what's the block size we'll use?
		int allocateBlockSize = mBufferSize;
		if(allocateBlockSize > MAX_BUFFER_ADDITION)
		{
			allocateBlockSize = MAX_BUFFER_ADDITION;
		}
		
		// Write it the easy way. Although it's not the most efficient...
		int newSize = mBufferSize;
		while(newSize < (mBytesInBuffer + NBytes))
		{
			newSize += allocateBlockSize;
		}
		
		// Reallocate buffer
		mBuffer.Resize(newSize);
		
		// Store new size
		mBufferSize = newSize;
	}
	
	// Copy in data and adjust counter
	::memcpy(((char*)mBuffer) + mBytesInBuffer, pBuffer, NBytes);
	mBytesInBuffer += NBytes;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::GetPosition()
//		Purpose: In write phase, returns the number of bytes written, in read
//				 phase, the number of bytes to go
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
IOStream::pos_type CollectInBufferStream::GetPosition() const
{
	return mInWritePhase?mBytesInBuffer:mReadPosition;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::Seek(pos_type, int)
//		Purpose: As interface. But read phase only. 
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
void CollectInBufferStream::Seek(pos_type Offset, int SeekType)
{
	if(mInWritePhase != false) { THROW_EXCEPTION(CommonException, CollectInBufferStreamNotInCorrectPhase) }
	
	int newPos = 0;
	switch(SeekType)
	{
	case IOStream::SeekType_Absolute:
		newPos = Offset;
		break;
	case IOStream::SeekType_Relative:
		newPos = mReadPosition + Offset;
		break;
	case IOStream::SeekType_End:
		newPos = mBytesInBuffer + Offset;
		break;
	default:
		THROW_EXCEPTION(CommonException, IOStreamBadSeekType)
		break;
	}
	
	// Make sure it doesn't go over
	if(newPos > mBytesInBuffer)
	{
		newPos = mBytesInBuffer;
	}
	// or under
	if(newPos < 0)
	{
		newPos = 0;
	}
	
	// Set the new read position
	mReadPosition = newPos;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::StreamDataLeft()
//		Purpose: As interface
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
bool CollectInBufferStream::StreamDataLeft()
{
	return mInWritePhase?(false):(mReadPosition < mBytesInBuffer);
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::StreamClosed()
//		Purpose: As interface
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
bool CollectInBufferStream::StreamClosed()
{
	return !mInWritePhase;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::SetForReading()
//		Purpose: Switch to read phase, after all data written
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
void CollectInBufferStream::SetForReading()
{
	if(mInWritePhase != true) { THROW_EXCEPTION(CommonException, CollectInBufferStreamNotInCorrectPhase) }
	
	// Move to read phase
	mInWritePhase = false;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::GetBuffer()
//		Purpose: Returns the buffer
//		Created: 2003/09/05
//
// --------------------------------------------------------------------------
void *CollectInBufferStream::GetBuffer() const
{
	return mBuffer.GetPtr();
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::GetSize()
//		Purpose: Returns the buffer size
//		Created: 2003/09/05
//
// --------------------------------------------------------------------------
int CollectInBufferStream::GetSize() const
{
	return mBytesInBuffer;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    CollectInBufferStream::Reset()
//		Purpose: Reset the stream, so it is empty and ready to be written to.
//		Created: 8/12/03
//
// --------------------------------------------------------------------------
void CollectInBufferStream::Reset()
{
	mInWritePhase = true;
	mBytesInBuffer = 0;
	mReadPosition = 0;
}