mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
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:
committed by
Copybara-Service
parent
ebf87cc581
commit
1a78b33a89
@@ -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 = [
|
||||
|
||||
@@ -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_
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user