libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
bufferediodevice.h
Go to the documentation of this file.
1
8#pragma once
9
11#include <promeki/core/buffer.h>
12
14
30class BufferedIODevice : public IODevice {
31 PROMEKI_OBJECT(BufferedIODevice, IODevice)
32 public:
38
41
54 int64_t read(void *data, int64_t maxSize) override;
55
63 int64_t bytesAvailable() const override;
64
77
82 const Buffer &readBuffer() const { return _readBuf; }
83
88 size_t readBufferSize() const { return _readBuf.availSize(); }
89
100
106
113
121 bool canReadLine() const;
122
131 int64_t peek(void *buf, size_t maxBytes) const;
132
140 Buffer peek(size_t maxBytes) const;
141
153
158 bool isUnbuffered() const { return _unbuffered; }
159
160 protected:
161
171 virtual int64_t readFromDevice(void *data, int64_t maxSize) = 0;
172
182
193 size_t bufferedBytesUnconsumed() const {
194 return _readBufFill - _readBufPos;
195 }
196
204
212
213 private:
215 static constexpr size_t DefaultReadBufSize = 8192;
216
217 bool _unbuffered = false;
218 Buffer _readBuf;
219 size_t _readBufPos = 0;
220 size_t _readBufFill = 0;
221 bool _bufferAllocated = false;
222
227 int64_t fillBuffer();
228
232 void compactBuffer();
233};
234
Generic memory buffer descriptor with alignment and memory space support.
Definition buffer.h:85
size_t availSize() const
Returns the available space from data() to the end of the allocation.
Definition buffer.h:261
Abstract IODevice with an internal read buffer.
Definition bufferediodevice.h:30
virtual ~BufferedIODevice()
Destructor.
const Buffer & readBuffer() const
Returns a const reference to the current read buffer.
Definition bufferediodevice.h:82
Error setReadBuffer(Buffer &&buf)
Replaces the internal read buffer.
virtual int64_t readFromDevice(void *data, int64_t maxSize)=0
Reads raw data from the underlying device.
bool canReadLine() const
Returns true if a complete line is available for reading.
size_t readBufferSize() const
Returns the capacity of the read buffer.
Definition bufferediodevice.h:88
virtual int64_t deviceBytesAvailable() const
Returns the number of bytes available from the device.
bool isUnbuffered() const
Returns true if unbuffered mode is enabled.
Definition bufferediodevice.h:158
Buffer readLine(size_t maxLength=0)
Reads a line of text up to a newline character.
int64_t bytesAvailable() const override
Returns the number of bytes available for reading.
void setUnbuffered(bool enable)
Enables or disables unbuffered mode.
size_t bufferedBytesUnconsumed() const
Returns the number of buffered bytes not yet consumed.
Definition bufferediodevice.h:193
int64_t peek(void *buf, size_t maxBytes) const
Reads up to maxBytes without consuming them.
Buffer readAll()
Reads all available data from the device.
void ensureReadBuffer()
Ensures the read buffer is allocated.
Buffer peek(size_t maxBytes) const
Reads up to maxBytes without consuming them.
int64_t read(void *data, int64_t maxSize) override
Reads up to maxSize bytes into data.
void resetReadBuffer()
Resets the read buffer cursors.
Buffer readBytes(size_t maxBytes)
Reads up to maxBytes bytes and returns as a Buffer.
BufferedIODevice(ObjectBase *parent=nullptr)
Constructs a BufferedIODevice.
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Abstract base class for all I/O devices.
Definition iodevice.h:29
Dynamic array container wrapping std::vector.
Definition list.h:40
Base object for promeki.
Definition objectbase.h:129
ObjectBase * parent() const
Returns the parent object, if one. nullptr if none.
Definition objectbase.h:258
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19