diff --git a/src/fs/io/BufferedReader.cxx b/src/fs/io/BufferedReader.cxx index 9ad7bdbc3..c233a7423 100644 --- a/src/fs/io/BufferedReader.cxx +++ b/src/fs/io/BufferedReader.cxx @@ -53,6 +53,19 @@ BufferedReader::Fill(bool need_more) return true; } +void * +BufferedReader::ReadFull(size_t size) +{ + while (true) { + auto r = Read(); + if (r.size >= size) + return r.data; + + if (!Fill(true)) + throw std::runtime_error("Premature end of file"); + } +} + size_t BufferedReader::ReadFromBuffer(WritableBuffer dest) { diff --git a/src/fs/io/BufferedReader.hxx b/src/fs/io/BufferedReader.hxx index 6d6bd5431..fcbef0f11 100644 --- a/src/fs/io/BufferedReader.hxx +++ b/src/fs/io/BufferedReader.hxx @@ -51,6 +51,14 @@ public: return buffer.Read().ToVoid(); } + /** + * Read a buffer of exactly the given size (without consuming + * it). Throws std::runtime_error if not enough data is + * available. + */ + gcc_pure + void *ReadFull(size_t size); + void Consume(size_t n) { buffer.Consume(n); }