diff options
Diffstat (limited to 'ByteBuffer.cpp')
-rw-r--r-- | ByteBuffer.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/ByteBuffer.cpp b/ByteBuffer.cpp index 70b9a8e..912e697 100644 --- a/ByteBuffer.cpp +++ b/ByteBuffer.cpp @@ -1,6 +1,8 @@ #include "ByteBuffer.h" #include <stdint-gcc.h> #include <string.h> +#include <sstream> +#include <iomanip> using namespace std; @@ -35,7 +37,7 @@ uint16_t ByteBuffer::get16le() { canAccessIndex(cursor + 1); uint16_t value; value = bytes[zero + cursor++]; - value = ((uint16_t) bytes[zero + cursor++]) << 8; + value |= ((uint16_t) bytes[zero + cursor++]) << 8; return value; } @@ -46,6 +48,12 @@ void ByteBuffer::copy(uint8_t *bytes, size_t length) { cursor += length; } +ByteBuffer ByteBuffer::view(size_t length) { + canAccessIndex(cursor + length); + size_t s = zero + cursor + length; + return ByteBuffer(bytes, s, s, cursor); +} + void ByteBuffer::checkAndUpdateSize(size_t newBytes) { size_t newSize = zero + cursor + newBytes; if (newSize >= capacity) { @@ -60,3 +68,13 @@ void ByteBuffer::canAccessIndex(size_t index) { throw ByteBufferException(string("Out of bounds! zero=") + to_string(zero) + ", index=" + to_string(index) + ", size=" + to_string(size)); } } + +std::string ByteBuffer::toString() const { + stringstream s; + + for(size_t i = zero; i < size; i++) { + s << hex << setfill('0') << setw(2) << bytes[i] << " "; + } + + return string(s.str()); +} |