Add InputStream::ReadExactly

Read() can return fewer bytes than requested.
ReadExactly() will call Read() repeatedly until we
have read as many bytes as we need.

PiperOrigin-RevId: 518719418
This commit is contained in:
Janusz Sobczak
2023-03-22 17:45:50 -07:00
committed by Copybara-Service
parent ebf87cc581
commit 1a78b33a89
8 changed files with 170 additions and 27 deletions
+2 -2
View File
@@ -102,7 +102,7 @@ void Medium::RunLoop(BluetoothSocket socket) {
NEARBY_LOGS(INFO) << "Run loop";
InputStream& input = socket.GetInputStream();
while (!cancellation_flag_.Cancelled()) {
ExceptionOr<ByteArray> header = input.Read(kHeaderSize);
ExceptionOr<ByteArray> header = input.ReadExactly(kHeaderSize);
if (!header.ok() || header.result().size() != kHeaderSize) {
break;
}
@@ -113,7 +113,7 @@ void Medium::RunLoop(BluetoothSocket socket) {
static_cast<unsigned int>(data[3]);
ExceptionOr<ByteArray> payload;
if (length > 0) {
payload = input.Read(length);
payload = input.ReadExactly(length);
} else {
payload = ExceptionOr<ByteArray>(ByteArray(""));
}