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#include <promeki/list.h>
16
17PROMEKI_NAMESPACE_BEGIN
18
52class BufferedIODevice : public IODevice {
53 PROMEKI_OBJECT(BufferedIODevice, IODevice)
54 public:
59 BufferedIODevice(ObjectBase *parent = nullptr);
60
62 virtual ~BufferedIODevice();
63
76 int64_t read(void *data, int64_t maxSize) override;
77
85 int64_t bytesAvailable() const override;
86
98 Error setReadBuffer(Buffer &&buf);
99
104 const Buffer &readBuffer() const { return _readBuf; }
105
110 size_t readBufferSize() const { return _readBuf.availSize(); }
111
121 Buffer readLine(size_t maxLength = 0);
122
127 Buffer readAll();
128
134 Buffer readBytes(size_t maxBytes);
135
143 bool canReadLine() const;
144
153 int64_t peek(void *buf, size_t maxBytes) const;
154
162 Buffer peek(size_t maxBytes) const;
163
174 void setUnbuffered(bool enable);
175
180 bool isUnbuffered() const { return _unbuffered; }
181
195 int64_t write(const void *data, int64_t maxSize) override;
196
205 void flush() override;
206
215 void setWriteBuffered(bool enable);
216
221 bool isWriteBuffered() const { return _writeBuffered; }
222
227 size_t writeBufferCapacity() const { return _writeBufCapacity; }
228
238 void setWriteBufferCapacity(size_t bytes);
239
240 protected:
250 virtual int64_t readFromDevice(void *data, int64_t maxSize) = 0;
251
264 virtual int64_t writeToDevice(const void *data, int64_t maxSize) = 0;
265
274 virtual int64_t deviceBytesAvailable() const;
275
286 size_t bufferedBytesUnconsumed() const { return _readBufFill - _readBufPos; }
287
299 size_t bufferedBytesPending() const { return _writeBuf.size(); }
300
307 void ensureReadBuffer();
308
315 void resetReadBuffer();
316
317 private:
319 static constexpr size_t DefaultReadBufSize = 8192;
320
322 static constexpr size_t DefaultWriteBufSize = 8192;
323
324 bool _unbuffered = false;
325 Buffer _readBuf;
326 size_t _readBufPos = 0;
327 size_t _readBufFill = 0;
328 bool _bufferAllocated = false;
329
330 bool _writeBuffered = false;
331 size_t _writeBufCapacity = DefaultWriteBufSize;
332 List<char> _writeBuf;
333
338 int64_t fillBuffer();
339
343 void compactBuffer();
344};
345
346PROMEKI_NAMESPACE_END
347
348#endif // PROMEKI_ENABLE_CORE