Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ArduinoStreamReader.hpp Source File

ArduinoStreamReader.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <Arduino.h>
00008 
00009 namespace ARDUINOJSON_NAMESPACE {
00010 
00011 template <typename TSource>
00012 struct Reader<TSource,
00013               typename enable_if<is_base_of<Stream, TSource>::value>::type> {
00014  public:
00015   explicit Reader(Stream& stream) : _stream(&stream) {}
00016 
00017   int read() {
00018     // don't use _stream.read() as it ignores the timeout
00019     char c;
00020     return _stream->readBytes(&c, 1) ? static_cast<unsigned char>(c) : -1;
00021   }
00022 
00023   size_t readBytes(char* buffer, size_t length) {
00024     return _stream->readBytes(buffer, length);
00025   }
00026 
00027  private:
00028   Stream* _stream;
00029 };
00030 
00031 }  // namespace ARDUINOJSON_NAMESPACE