summaryrefslogtreecommitdiff
path: root/includes/Buffer.h
blob: 4983b986d7865eb817f6d8f0c6f954d2587ce77e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once

#include <Handle.h>

namespace Balau {

class Buffer : public SeekableHandle {
  public:
      Buffer(const uint8_t * buffer, size_t s) : m_buffer(const_cast<uint8_t *>(buffer)), m_bufSize(s), m_fromConst(true) { }
      Buffer() throw (GeneralException) { }
      virtual ~Buffer() override;
    virtual void close() throw (GeneralException) override;
    virtual ssize_t read(void * buf, size_t count) throw (GeneralException) override;
    virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) override;
    virtual bool isClosed() override { return false; }
    virtual bool isEOF() override { return rtell() == m_bufSize; }
    virtual bool canRead() override { return true; }
    virtual bool canWrite() override { return !m_fromConst; }
    virtual bool canEAgainOnRead() override { return false; }
    virtual bool canEAgainOnWrite() override { return false; }
    virtual const char * getName() override { return "Buffer"; }
    virtual off64_t getSize() override { return m_bufSize; }
    const uint8_t * getBuffer() { return m_buffer + rtell(); }
    void reset();
    void clear();
    void rewind() { rseek(0); wseek(0); }
    void borrow(const uint8_t * buffer, size_t s);
  private:
    uint8_t * m_buffer = NULL;
    bool m_fromConst = false;
    off64_t m_bufSize = 0, m_numBlocks = 0;
};

};