DataSourceStream.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * DataSourceStream.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include <user_config.h>
14 #include <Stream.h>
15 #include <WString.h>
16 #include "SeekOrigin.h"
17 #include "../WebConstants.h"
18 
26 enum StreamType {
34 };
35 
40 class IDataSourceStream : public Stream
41 {
42 public:
46  virtual StreamType getStreamType() const
47  {
48  return eSST_Unknown;
49  }
50 
56  virtual bool isValid() const
57  {
58  return getStreamType() != eSST_Invalid;
59  }
60 
61  size_t readBytes(char* buffer, size_t length) override;
62 
69  virtual uint16_t readMemoryBlock(char* data, int bufSize) = 0;
70 
75  int read() override;
76 
81  int peek() override;
82 
90  virtual int seekFrom(int offset, SeekOrigin origin)
91  {
92  return -1;
93  }
94 
99  virtual bool seek(int len)
100  {
101  return seekFrom(len, SeekOrigin::Current) >= 0;
102  }
103 
107  virtual bool isFinished() = 0;
108 
113  virtual int available()
114  {
115  return -1;
116  }
117 
122  size_t write(uint8_t charToWrite) override
123  {
124  (void)charToWrite;
125  return 0;
126  }
127 
135  {
136  return available();
137  }
138 
139  /*
140  * @brief Flushes the stream
141  */
142  void flush() override
143  {
144  }
145 
150  virtual String id() const
151  {
152  return nullptr;
153  }
154 
160  virtual String getName() const
161  {
162  return nullptr;
163  }
164 
169  virtual MimeType getMimeType() const
170  {
171  return ContentType::fromFullFileName(getName(), MIME_UNKNOWN);
172  }
173 
178  String readString(size_t maxLen) override;
179 
194  virtual bool moveString(String& s)
195  {
196  s = nullptr;
197  return false;
198  };
199 };
String readString(size_t maxLen) override
Overrides Stream method for more efficient reading.
Memory data stream.
Definition: DataSourceStream.h:28
Base class for read-only stream.
Definition: DataSourceStream.h:40
int read() override
Read one character and moves the stream pointer.
int length()
Return the total length of the stream.
Definition: DataSourceStream.h:134
virtual bool isFinished()=0
Check if all data has been read.
size_t readBytes(char *buffer, size_t length) override
Read chars from stream into buffer.
virtual uint16_t readMemoryBlock(char *data, int bufSize)=0
Read a block of memory.
The String class.
Definition: WString.h:136
virtual bool isValid() const
Determine if the stream object contains valid data.
Definition: DataSourceStream.h:56
JSON object data stream.
Definition: DataSourceStream.h:31
void flush() override
Definition: DataSourceStream.h:142
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
SEEK_CUR: Current position in file.
virtual bool seek(int len)
Move read cursor.
Definition: DataSourceStream.h:99
Unknown data stream type.
Definition: DataSourceStream.h:33
#define SMING_DEPRECATED
Definition: sming_attr.h:30
virtual int seekFrom(int offset, SeekOrigin origin)
Change position in stream.
Definition: DataSourceStream.h:90
StreamType
Data stream type.
Definition: DataSourceStream.h:26
virtual String getName() const
Returns name of the resource.
Definition: DataSourceStream.h:160
File data stream.
Definition: DataSourceStream.h:29
Template data stream.
Definition: DataSourceStream.h:30
Stream content not valid.
Definition: DataSourceStream.h:27
int peek() override
Read a character without advancing the stream pointer.
virtual StreamType getStreamType() const
Get the stream type.
Definition: DataSourceStream.h:46
virtual String id() const
Returns unique id of the resource.
Definition: DataSourceStream.h:150
virtual MimeType getMimeType() const
Get MIME type for stream content.
Definition: DataSourceStream.h:169
MimeType fromFullFileName(const char *fileName, MimeType unknown)
Obtain MIME type value from file name or path, with extension.
size_t write(uint8_t charToWrite) override
From Stream class: We don&#39;t write using this stream.
Definition: DataSourceStream.h:122
User defined data stream.
Definition: DataSourceStream.h:32
virtual bool moveString(String &s)
Memory-based streams may be able to move content into a String.
Definition: DataSourceStream.h:194
MimeType
Definition: WebConstants.h:53
virtual int available()
Return the total length of the stream.
Definition: DataSourceStream.h:113
Base Stream class.
Definition: Stream.h:32