-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBufferedByteStream.cpp
78 lines (62 loc) · 1.22 KB
/
BufferedByteStream.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
/*
* BufferedStream.cpp
*
* Created on: 2-sep.-2016
* Author: lieven2
*/
#include <BufferedByteStream.h>
BufferedByteStream::BufferedByteStream(uint32_t size) :
_rxd(size), _txd(size) {
}
BufferedByteStream::~BufferedByteStream() {
}
Erc BufferedByteStream::write(uint8_t b) {
return _txd.write(b);
return E_OK;
}
Erc BufferedByteStream::write(Bytes& bytes) {
Erc erc;
bytes.offset(0);
if (!_txd.hasSpace(bytes.length())) {
flush();
return ENOBUFS;
}
while (bytes.hasData()) {
erc = write(bytes.read());
if (erc) {
flush();
return erc;
}
}
flush();
return E_OK;
}
bool BufferedByteStream::hasSpace() {
return _txd.hasSpace();
}
bool BufferedByteStream::hasSpace(uint32_t sp) {
return _txd.hasSpace(sp);
}
bool BufferedByteStream::hasData() {
return _rxd.hasData();
}
bool BufferedByteStream::hasToSend() {
return _txd.hasData();
}
uint8_t BufferedByteStream::toSend() {
return _txd.read();
}
uint8_t BufferedByteStream::read() {
return _rxd.read();
}
Erc BufferedByteStream::read(Bytes& bytes) {
while (hasData())
bytes.write(read());
return E_OK;
}
bool BufferedByteStream::canReceive() {
return _rxd.hasSpace();
}
void BufferedByteStream::receive(uint8_t data) {
_rxd.write(data);
}