fs/io/BufferedReader: add ReadFull(size_t)

This commit is contained in:
Max Kellermann 2016-08-16 12:09:04 +02:00
parent 5f66af3712
commit 8748b2dc3f
2 changed files with 21 additions and 0 deletions

View File

@ -53,6 +53,19 @@ BufferedReader::Fill(bool need_more)
return true; 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 size_t
BufferedReader::ReadFromBuffer(WritableBuffer<void> dest) BufferedReader::ReadFromBuffer(WritableBuffer<void> dest)
{ {

View File

@ -51,6 +51,14 @@ public:
return buffer.Read().ToVoid(); 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) { void Consume(size_t n) {
buffer.Consume(n); buffer.Consume(n);
} }