diff options
-rw-r--r-- | includes/Buffer.h | 5 | ||||
-rw-r--r-- | src/Buffer.cc | 10 |
2 files changed, 13 insertions, 2 deletions
diff --git a/includes/Buffer.h b/includes/Buffer.h index 50dd47b..be86162 100644 --- a/includes/Buffer.h +++ b/includes/Buffer.h @@ -6,7 +6,9 @@ namespace Balau { class Buffer : public SeekableHandle { public: - Buffer() throw (GeneralException) : m_buffer(NULL), m_bufSize(0), m_numBlocks(0) { } + Buffer(const uint8_t * buffer) : m_buffer(const_cast<uint8_t *>(buffer)), m_fromConst(true) { } + Buffer() throw (GeneralException) : m_buffer(NULL), m_fromConst(false), m_bufSize(0), m_numBlocks(0) { } + virtual ~Buffer(); virtual void close() throw (GeneralException); virtual ssize_t read(void * buf, size_t count) throw (GeneralException); virtual ssize_t write(const void * buf, size_t count) throw (GeneralException); @@ -19,6 +21,7 @@ class Buffer : public SeekableHandle { void reset(); private: uint8_t * m_buffer; + bool m_fromConst; off_t m_bufSize, m_numBlocks; }; diff --git a/src/Buffer.cc b/src/Buffer.cc index f276ec0..a958571 100644 --- a/src/Buffer.cc +++ b/src/Buffer.cc @@ -9,6 +9,11 @@ static const int s_blockSize = 16 * 1024; +Balau::Buffer::~Buffer() { + if (!m_fromConst) + free(m_buffer); +} + void Balau::Buffer::close() throw (GeneralException) { reset(); } @@ -30,6 +35,9 @@ ssize_t Balau::Buffer::read(void * buf, size_t count) throw (GeneralException) { } ssize_t Balau::Buffer::write(const void * buf, size_t count) throw (GeneralException) { + if (m_fromConst) + throw GeneralException("Buffer is read only and can't be written to."); + off_t cursor = wtell(); off_t end = cursor + count; off_t endBlock = (end / s_blockSize) + ((end % s_blockSize) ? 1 : 0); @@ -64,4 +72,4 @@ bool Balau::Buffer::isEOF() { return rtell() == m_bufSize; } const char * Balau::Buffer::getName() { return "Buffer"; } off_t Balau::Buffer::getSize() { return m_bufSize; } bool Balau::Buffer::canRead() { return true; } -bool Balau::Buffer::canWrite() { return true; } +bool Balau::Buffer::canWrite() { return !m_fromConst; } |