From 041bbe24b39190ac0b624b2709fc38dea17ad0a7 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 12 Feb 2015 17:22:59 +0100 Subject: o wip. --- ByteBuffer.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'ByteBuffer.cpp') 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 #include +#include +#include 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()); +} -- cgit v1.2.3