libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
bufferediodevice.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <promeki/iodevice.h>
14#include <promeki/buffer.h>
15
16PROMEKI_NAMESPACE_BEGIN
17
37class BufferedIODevice : public IODevice {
38 PROMEKI_OBJECT(BufferedIODevice, IODevice)
39 public:
44 BufferedIODevice(ObjectBase *parent = nullptr);
45
47 virtual ~BufferedIODevice();
48
61 int64_t read(void *data, int64_t maxSize) override;
62
70 int64_t bytesAvailable() const override;
71
83 Error setReadBuffer(Buffer &&buf);
84
89 const Buffer &readBuffer() const { return _readBuf; }
90
95 size_t readBufferSize() const { return _readBuf.availSize(); }
96
106 Buffer readLine(size_t maxLength = 0);
107
112 Buffer readAll();
113
119 Buffer readBytes(size_t maxBytes);
120
128 bool canReadLine() const;
129
138 int64_t peek(void *buf, size_t maxBytes) const;
139
147 Buffer peek(size_t maxBytes) const;
148
159 void setUnbuffered(bool enable);
160
165 bool isUnbuffered() const { return _unbuffered; }
166
167 protected:
177 virtual int64_t readFromDevice(void *data, int64_t maxSize) = 0;
178
187 virtual int64_t deviceBytesAvailable() const;
188
199 size_t bufferedBytesUnconsumed() const { return _readBufFill - _readBufPos; }
200
207 void ensureReadBuffer();
208
215 void resetReadBuffer();
216
217 private:
219 static constexpr size_t DefaultReadBufSize = 8192;
220
221 bool _unbuffered = false;
222 Buffer _readBuf;
223 size_t _readBufPos = 0;
224 size_t _readBufFill = 0;
225 bool _bufferAllocated = false;
226
231 int64_t fillBuffer();
232
236 void compactBuffer();
237};
238
239PROMEKI_NAMESPACE_END
240
241#endif // PROMEKI_ENABLE_CORE