-
Notifications
You must be signed in to change notification settings - Fork 221
/
LinearBufferInputOutputStream.cpp
162 lines (111 loc) · 3.42 KB
/
LinearBufferInputOutputStream.cpp
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
/*
* This file is a part of the open source stm32plus library.
* Copyright (c) 2011,2012,2013,2014 Andy Brown <www.andybrown.me.uk>
* Please see website for licensing terms.
*/
#include "config/stm32plus.h"
#include "config/stream.h"
namespace stm32plus {
/**
* Constructor
* @param buffer
* @param size
*/
LinearBufferInputOutputStream::LinearBufferInputOutputStream(void *buffer,uint32_t size)
: BufferedInputOutputStream(buffer,size) {
}
/**
* Constructor with initial size
* @param initialSize
*/
LinearBufferInputOutputStream::LinearBufferInputOutputStream(uint32_t initialSize)
: BufferedInputOutputStream(initialSize) {
}
/**
* Read a single byte unless we're at the end of the stream
* @return E_END_OF_STREAM (actually -1) or the data byte in the low 8 bits.
*/
int16_t LinearBufferInputOutputStream::read() {
if(_readPtr==_buffer+_bufferSize)
return E_END_OF_STREAM;
return *_readPtr++;
}
/**
* Read a block of data from the buffer. Always succeeds but will return actuallyRead=0 at end of stream
*
* @param[in] buffer Where to read the data out to.
* @param[in] size The maximum size to read.
* @param[out] actuallyRead The actual size read.
* @return Always true
*/
bool LinearBufferInputOutputStream::read(void *buffer,uint32_t size,uint32_t& actuallyRead) {
uint32_t available;
if((available=_bufferSize-(_readPtr-_buffer))<size)
actuallyRead=available;
else
actuallyRead=size;
if(actuallyRead>0) {
memcpy(buffer,(void *)_readPtr,actuallyRead);
_readPtr+=actuallyRead;
}
return true;
}
/**
* Skip forward (read pointer)
* @param[in] howMuch How far to skip
* @return false if would be past end of stream
*/
bool LinearBufferInputOutputStream::skip(uint32_t howMuch) {
uint32_t available;
if((available=_bufferSize-(_readPtr-_buffer))<howMuch)
return errorProvider.set(ErrorProvider::ERROR_PROVIDER_BUFFERED_IOSTREAM,E_INVALID_SEEK_SIZE);
_readPtr+=howMuch;
return true;
}
/**
* Return true if at least one byte can be read without error
* @return true if a byte can be read
*/
bool LinearBufferInputOutputStream::available() {
return _bufferSize-(_readPtr-_buffer)>0;
}
/**
* Reset the input stream pointer.
* @return Always true.
*/
bool LinearBufferInputOutputStream::reset() {
_readPtr=_buffer;
return true;
}
/**
* Reset the output pointer.
*/
void LinearBufferInputOutputStream::resetOutput() {
_writePtr=_buffer;
}
/**
* Write a byte to the output pointer
* @param[in] c The byte to write.
* @return false if out of space
*/
bool LinearBufferInputOutputStream::write(uint8_t c) {
if(_writePtr==_buffer+_bufferSize)
return errorProvider.set(ErrorProvider::ERROR_PROVIDER_BUFFERED_IOSTREAM,E_BUFFER_FULL);
*_writePtr++=c;
return true;
}
/**
* Write a block of bytes. There must be space for it all
* @param[in] buffer Where to write from.
* @param[in] size The number of bytes to write
*/
bool LinearBufferInputOutputStream::write(const void *buffer,uint32_t size) {
// check for space
if(_bufferSize-(_writePtr-_buffer)<size)
return errorProvider.set(ErrorProvider::ERROR_PROVIDER_BUFFERED_IOSTREAM,E_BUFFER_FULL);
// write the data
memcpy((void *)_writePtr,buffer,size);
_writePtr+=size;
return true;
}
}