Fixed ignored Stream timeout and made sure we don't read more that necessary (issue #422)

This commit is contained in:
Benoit Blanchon
2017-01-22 10:31:05 +01:00
parent fb554071dc
commit cc8c0472ca
10 changed files with 89 additions and 71 deletions

View File

@@ -17,15 +17,35 @@ namespace ArduinoJson {
namespace Internals {
struct ArduinoStreamTraits {
class Iterator {
class Reader {
Stream& _stream;
char _current, _next;
public:
Iterator(Stream& stream) : _stream(stream) {}
Reader(Stream& stream) : _stream(stream), _current(0), _next(0) {}
void move() {
_current = _next;
_next = 0;
}
char current() {
if (!_current) _current = read();
return _current;
}
char next() {
int n = _stream.read();
return n >= 0 ? static_cast<char>(n) : '\0';
// assumes that current() has been called
if (!_next) _next = read();
return _next;
}
private:
char read() {
// don't use _stream.read() as it ignores the timeout
char c = 0;
_stream.readBytes(&c, 1);
return c;
}
};
};