From 33f70931dd2864d2c522a7165404aa89871c7da8 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Fri, 4 Sep 2020 13:18:37 +0200
Subject: [PATCH] decoder/API: add decoder_read_much()

---
 src/decoder/DecoderAPI.cxx | 21 +++++++++++++++++++++
 src/decoder/DecoderAPI.hxx | 12 ++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/src/decoder/DecoderAPI.cxx b/src/decoder/DecoderAPI.cxx
index 03f02b311..35e3ebafb 100644
--- a/src/decoder/DecoderAPI.cxx
+++ b/src/decoder/DecoderAPI.cxx
@@ -42,6 +42,27 @@ decoder_read(DecoderClient *client,
 	}
 }
 
+size_t
+decoder_read_much(DecoderClient *client, InputStream &is,
+		  void *_buffer, size_t size) noexcept
+{
+	uint8_t *buffer = (uint8_t *)_buffer;
+
+	size_t total = 0;
+
+	while (size > 0 && !is.LockIsEOF()) {
+		size_t nbytes = decoder_read(client, is, buffer, size);
+		if (nbytes == 0)
+			return false;
+
+		total += nbytes;
+		buffer += nbytes;
+		size -= nbytes;
+	}
+
+	return total;
+}
+
 bool
 decoder_read_full(DecoderClient *client, InputStream &is,
 		  void *_buffer, size_t size) noexcept
diff --git a/src/decoder/DecoderAPI.hxx b/src/decoder/DecoderAPI.hxx
index 20959b456..357c4f60a 100644
--- a/src/decoder/DecoderAPI.hxx
+++ b/src/decoder/DecoderAPI.hxx
@@ -74,6 +74,18 @@ decoder_read(DecoderClient &decoder, InputStream &is,
 	return decoder_read(&decoder, is, buffer, length);
 }
 
+/**
+ * Blocking read from the input stream.  Attempts to fill the buffer
+ * as much as possible, until either end-of-file is reached or an
+ * error occurs.
+ *
+ * @return the number of bytes read, or 0 if one of the following
+ * occurs: end of file; error; command (like SEEK or STOP).
+ */
+size_t
+decoder_read_much(DecoderClient *decoder, InputStream &is,
+		  void *buffer, size_t size) noexcept;
+
 /**
  * Blocking read from the input stream.  Attempts to fill the buffer
  * completely; there is no partial result.