#ifndef BYTE_STREAM_WRAPPER_H #define BYTE_STREAM_WRAPPER_H #include #include #include #include // For now #include "log.h" class ByteBufferException : public std::runtime_error { public: ByteBufferException(std::string const &what) : std::runtime_error(what) { } }; class ByteBuffer { public: ByteBuffer(const uint8_t *bytes, size_t capacity); ByteBuffer(const uint8_t *bytes, size_t capacity, size_t zero, size_t size); inline size_t getSize() const { DF << "end=" << (uint64_t)end << ", zero=" << (uint64_t)zero << ", size=" << (end - zero); return end - zero; } inline size_t getCapacity() const { return capacity; } inline size_t getCursor() const { return ptr - zero; } inline void setCursor(size_t newCursor) { // assertCanAccessRelative(newCursor); ptr = (uint8_t *) &zero[newCursor]; } inline void skip(size_t length) { checkAndUpdateEnd(length); ptr += length; } ByteBuffer &add8(uint8_t value); ByteBuffer &add16le(uint16_t value); uint8_t get8(size_t index); uint8_t get8(); uint16_t get16le(); void copy(uint8_t *bytes, size_t length); /** * Creates a view from cursor to size. */ ByteBuffer view() const; // TODO: should return const ByteBuffer view(size_t length) const; std::string toString() const; private: ByteBuffer(const uint8_t *bytes, size_t capacity, const uint8_t *zero, const uint8_t *end); ByteBuffer view(uint8_t *ptr, const uint8_t *end) const; void checkAndUpdateEnd(size_t count); void assertCanAccessRelative(size_t diff) const; void assertCanAccessIndex(uint8_t *p) const; const uint8_t *bytes; const size_t capacity; const uint8_t *zero; const uint8_t *end; uint8_t *ptr; }; #endif