diff --git a/lib/bytes.cc b/lib/bytes.cc index 3a926e67..fa651430 100644 --- a/lib/bytes.cc +++ b/lib/bytes.cc @@ -273,6 +273,16 @@ ByteWriter Bytes::writer() return ByteWriter(*this); } +ByteWriter& ByteWriter::operator +=(std::istream& stream) +{ + Bytes buffer(4096); + + while (stream.read((char*) buffer.begin(), buffer.size())) + this->append(buffer); + this->append(buffer.slice(0, stream.gcount())); + return *this; +} + void BitWriter::push(uint32_t bits, size_t size) { bits <<= 32-size; diff --git a/lib/bytes.h b/lib/bytes.h index 6408bd0c..0dc5ebb9 100644 --- a/lib/bytes.h +++ b/lib/bytes.h @@ -69,7 +69,7 @@ public: unsigned pos = 0; bool eof() const - { return pos == _bytes.size(); } + { return pos >= _bytes.size(); } ByteReader& seek(unsigned pos) { @@ -77,6 +77,12 @@ public: return *this; } + ByteReader& skip(int delta) + { + this->pos += delta; + return *this; + } + const Bytes read(unsigned len) { const Bytes bytes = _bytes.slice(pos, len); @@ -256,11 +262,18 @@ public: return *this; } + ByteWriter& operator += (std::istream& stream); + ByteWriter& append(const Bytes data) { return *this += data; } + ByteWriter& append(std::istream& stream) + { + return *this += stream; + } + private: Bytes& _bytes; };