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
+1
View File
@@ -493,6 +493,7 @@ let package = Package(
"internal/platform/byte_array_test.cc",
"internal/platform/bluetooth_utils_test.cc",
"internal/platform/credential_storage_impl_test.cc",
"internal/platform/input_stream_test.cc",
"internal/platform/single_thread_executor_test.cc",
"internal/platform/scheduled_executor_test.cc",
"internal/platform/count_down_latch_test.cc",
@@ -54,31 +54,8 @@ ByteArray IntToBytes(std::int32_t value) {
return ByteArray(int_bytes, sizeof(int_bytes));
}
ExceptionOr<ByteArray> ReadExactly(InputStream* reader, std::int64_t size) {
ByteArray buffer(size);
std::int64_t current_pos = 0;
while (current_pos < size) {
ExceptionOr<ByteArray> read_bytes = reader->Read(size - current_pos);
if (!read_bytes.ok()) {
return read_bytes;
}
ByteArray result = read_bytes.result();
if (result.Empty()) {
NEARBY_LOGS(WARNING) << __func__ << ": Empty result when reading bytes.";
return ExceptionOr<ByteArray>(Exception::kIo);
}
buffer.CopyAt(current_pos, result);
current_pos += result.size();
}
return ExceptionOr<ByteArray>(std::move(buffer));
}
ExceptionOr<std::int32_t> ReadInt(InputStream* reader) {
ExceptionOr<ByteArray> read_bytes = ReadExactly(reader, sizeof(std::int32_t));
ExceptionOr<ByteArray> read_bytes = reader->ReadExactly(sizeof(std::int32_t));
if (!read_bytes.ok()) {
return ExceptionOr<std::int32_t>(read_bytes.exception());
}
@@ -147,7 +124,7 @@ ExceptionOr<ByteArray> BaseEndpointChannel::Read(
return ExceptionOr<ByteArray>(Exception::kIo);
}
ExceptionOr<ByteArray> read_bytes = ReadExactly(reader_, read_int.result());
ExceptionOr<ByteArray> read_bytes = reader_->ReadExactly(read_int.result());
if (!read_bytes.ok()) {
return read_bytes;
}
+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(""));
}
+1
View File
@@ -232,6 +232,7 @@ cc_test(
"bluetooth_utils_test.cc",
"byte_array_test.cc",
"feature_flags_test.cc",
"input_stream_test.cc",
"prng_test.cc",
],
deps = [
+9
View File
@@ -107,6 +107,15 @@ class ExceptionOr {
Exception exception_{Exception::kFailed};
};
template <typename T>
constexpr inline bool operator==(const ExceptionOr<T>& a,
const ExceptionOr<T>& b) {
if (a.ok() && b.ok()) {
return a.result() == b.result();
}
return a.exception() == b.exception();
}
} // namespace nearby
#endif // PLATFORM_BASE_EXCEPTION_H_
+31
View File
@@ -17,6 +17,7 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <utility>
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
@@ -43,4 +44,34 @@ ExceptionOr<size_t> InputStream::Skip(size_t offset) {
return ExceptionOr<size_t>(offset);
}
ExceptionOr<ByteArray> InputStream::ReadExactly(std::size_t size) {
ByteArray buffer;
std::size_t current_pos = 0;
while (current_pos < size) {
ExceptionOr<ByteArray> read_bytes = Read(size - current_pos);
if (!read_bytes.ok()) {
return read_bytes;
}
const ByteArray& result = read_bytes.result();
if (result.Empty()) {
return ExceptionOr<ByteArray>(Exception::kIo);
}
if (current_pos == 0) {
if (result.size() == size) {
// We have read the requested `size` bytes in one chunk. We can return
// it directly.
return read_bytes;
} else {
// Reserve space for in the buffer.
buffer.SetData(size);
}
}
buffer.CopyAt(current_pos, result);
current_pos += result.size();
}
return ExceptionOr<ByteArray>(std::move(buffer));
}
} // namespace nearby
+5
View File
@@ -38,6 +38,11 @@ class InputStream {
// or Exception::kIo on error.
virtual ExceptionOr<size_t> Skip(size_t offset);
// Reads exactly `size` bytes from the input stream.
// Return Exception::kIo on error, or if end of file is reached before reading
// `size` bytes.
ExceptionOr<ByteArray> ReadExactly(std::size_t size);
// throws Exception::kIo
virtual Exception Close() = 0;
};
+119
View File
@@ -0,0 +1,119 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/input_stream.h"
#include <string>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
namespace nearby {
namespace {
using ::testing::_;
using ::testing::InSequence;
using ::testing::NiceMock;
using ::testing::Return;
class TestInputStream : public InputStream {
public:
MOCK_METHOD(ExceptionOr<ByteArray>, Read, (std::int64_t), (override));
MOCK_METHOD(Exception, Close, (), (override));
};
// Returns a ByteArray with values: a, a + 1, a + 2, ..., b - 1.
ExceptionOr<ByteArray> Range(char a, char b) {
std::string s;
for (char c = a; c < b; c++) {
s.push_back(c);
}
return ExceptionOr<ByteArray>(ByteArray(s));
}
TEST(InputStreamTest, Skip) {
NiceMock<TestInputStream> stream;
InSequence seq;
EXPECT_CALL(stream, Read(50)).WillOnce(Return(Range(0, 10)));
EXPECT_CALL(stream, Read(40)).WillOnce(Return(Range(0, 10)));
EXPECT_CALL(stream, Read(30)).WillOnce(Return(Range(0, 30)));
ExceptionOr<std::size_t> skipped = stream.Skip(50);
EXPECT_EQ(skipped.result(), 50);
}
TEST(InputStreamTest, SkipsLessOnEof) {
NiceMock<TestInputStream> stream;
InSequence seq;
EXPECT_CALL(stream, Read(50)).WillOnce(Return(Range(0, 10)));
EXPECT_CALL(stream, Read(40)).WillOnce(Return(Range(0, 10)));
// Returns EOF
EXPECT_CALL(stream, Read(30)).WillOnce(Return(Range(0, 0)));
ExceptionOr<std::size_t> skipped = stream.Skip(50);
EXPECT_EQ(skipped.result(), 20);
}
TEST(InputStreamTest, SkipFailsOnError) {
NiceMock<TestInputStream> stream;
InSequence seq;
EXPECT_CALL(stream, Read(50)).WillOnce(Return(Range(0, 10)));
EXPECT_CALL(stream, Read(40))
.WillOnce(Return(ExceptionOr<ByteArray>(Exception::kIo)));
ExceptionOr<std::size_t> skipped = stream.Skip(50);
EXPECT_EQ(skipped.exception(), Exception::kIo);
}
TEST(InputStreamTest, ReadExactlyOneChunk) {
NiceMock<TestInputStream> stream;
EXPECT_CALL(stream, Read(10)).WillOnce(Return(Range(0, 10)));
ExceptionOr<ByteArray> result = stream.ReadExactly(10);
EXPECT_EQ(result, Range(0, 10));
}
TEST(InputStreamTest, ReadExactlyMultipleChunks) {
NiceMock<TestInputStream> stream;
InSequence seq;
EXPECT_CALL(stream, Read(30)).WillOnce(Return(Range(0, 10)));
EXPECT_CALL(stream, Read(20)).WillOnce(Return(Range(10, 20)));
EXPECT_CALL(stream, Read(10)).WillOnce(Return(Range(20, 30)));
ExceptionOr<ByteArray> result = stream.ReadExactly(30);
EXPECT_EQ(result, Range(0, 30));
}
TEST(InputStreamTest, ReadExactlyFailsOnError) {
NiceMock<TestInputStream> stream;
InSequence seq;
EXPECT_CALL(stream, Read(30)).WillOnce(Return(Range(0, 10)));
EXPECT_CALL(stream, Read(20))
.WillOnce(Return(ExceptionOr<ByteArray>(Exception::kIo)));
ExceptionOr<ByteArray> result = stream.ReadExactly(30);
EXPECT_EQ(result.exception(), Exception::kIo);
}
} // namespace
} // namespace nearby