Switch OutputStream::Write to use string_view.

PiperOrigin-RevId: 854225267
This commit is contained in:
Francis Tsui
2026-01-09 09:37:00 -08:00
committed by Copybara-Service
parent 3d92559201
commit fbf66ad7e3
61 changed files with 227 additions and 198 deletions
@@ -16,10 +16,10 @@
#include <cstdint>
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/logging.h"
#include "internal/platform/output_stream.h"
namespace nearby {
@@ -32,38 +32,26 @@ OutputStream& BleSocket::GetOutputStream() { return output_stream_; }
Exception BleSocket::Close() { return {Exception::kSuccess}; }
bool BleSocket::Connect() {
// TODO(b/271031645): implement BLE socket using weave
VLOG(1) << __func__ << ": Connect to BLE peripheral";
return false;
}
ExceptionOr<ByteArray> BleSocket::BleInputStream::Read(std::int64_t size) {
// TODO(b/271031645): implement BLE socket using weave
VLOG(1) << __func__ << ": Read data size=" << size;
return ExceptionOr<ByteArray>(Exception::kIo);
}
Exception BleSocket::BleInputStream::Close() {
// TODO(b/271031645): implement BLE socket using weave
VLOG(1) << __func__ << ": Close BLE input stream.";
return {Exception::kSuccess};
}
Exception BleSocket::BleOutputStream::Write(const ByteArray& data) {
// TODO(b/271031645): implement BLE socket using weave
VLOG(1) << __func__ << ": Write data size=" << data.size();
Exception BleSocket::BleOutputStream::Write(absl::string_view data) {
return {Exception::kIo};
}
Exception BleSocket::BleOutputStream::Flush() {
// TODO(b/271031645): implement BLE socket using weave
LOG(INFO) << __func__ << ": Flush is called.";
return {Exception::kSuccess};
}
Exception BleSocket::BleOutputStream::Close() {
// TODO(b/271031645): implement BLE socket using weave
LOG(INFO) << __func__ << ": close is called.";
return {Exception::kSuccess};
}
@@ -17,6 +17,7 @@
#include <cstdint>
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/ble.h"
@@ -55,7 +56,7 @@ class BleSocket : public api::ble::BleSocket {
public:
~BleOutputStream() override = default;
Exception Write(const ByteArray& data) override;
Exception Write(absl::string_view data) override;
Exception Flush() override;
Exception Close() override;
};
@@ -20,6 +20,7 @@
#include <memory>
#include <utility>
#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "internal/flags/nearby_flags.h"
@@ -246,7 +247,8 @@ BluetoothSocket::BluetoothOutputStream::BluetoothOutputStream(
winrt_output_stream_ = stream;
}
Exception BluetoothSocket::BluetoothOutputStream::Write(const ByteArray& data) {
Exception BluetoothSocket::BluetoothOutputStream::Write(
absl::string_view data) {
try {
if (data.size() > write_buffer_.Capacity()) {
LOG(WARNING) << __func__
@@ -20,6 +20,7 @@
#include <cstdint>
#include <memory>
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/bluetooth_classic.h"
@@ -92,7 +93,7 @@ class BluetoothSocket : public api::BluetoothSocket {
::winrt::Windows::Storage::Streams::IOutputStream stream);
~BluetoothOutputStream() override = default;
Exception Write(const ByteArray& data) override;
Exception Write(absl::string_view data) override;
Exception Flush() override;
Exception Close() override;
@@ -167,7 +167,7 @@ Exception IOFile::Close() {
return {Exception::kSuccess};
}
Exception IOFile::Write(const ByteArray& data) {
Exception IOFile::Write(absl::string_view data) {
if (file_ == INVALID_HANDLE_VALUE) {
return {Exception::kIo};
}
@@ -48,7 +48,7 @@ class IOFile final : public api::InputFile, public api::OutputFile {
std::int64_t GetTotalSize() const override { return total_size_; }
Exception Close() override;
Exception Write(const ByteArray& data) override;
Exception Write(absl::string_view data) override;
absl::Time GetLastModifiedTime() const override;
void SetLastModifiedTime(absl::Time last_modified_time) override;
@@ -161,7 +161,7 @@ TEST(IOFileTest, OutputFileAlreadyExists) {
ASSERT_NE(output_file, nullptr);
EXPECT_EQ(output_file->GetTotalSize(), 0);
ExceptionOr<ByteArray> write_result = output_file->Write(ByteArray("test"));
ExceptionOr<ByteArray> write_result = output_file->Write("test");
EXPECT_FALSE(write_result.ok());
EXPECT_TRUE(write_result.GetException().Raised(Exception::kIo));
@@ -173,9 +173,9 @@ TEST(IOFileTest, OutputFileWrite) {
std::unique_ptr<IOFile> output_file = IOFile::CreateOutputFile(temp_file);
ASSERT_NE(output_file, nullptr);
ExceptionOr<ByteArray> write_result = output_file->Write(ByteArray("test1"));
ExceptionOr<ByteArray> write_result = output_file->Write("test1");
EXPECT_TRUE(write_result.ok());
write_result = output_file->Write(ByteArray("test2"));
write_result = output_file->Write("test2");
EXPECT_TRUE(write_result.ok());
EXPECT_TRUE(output_file->Close().Ok());
@@ -22,6 +22,7 @@
#include <string>
#include <utility>
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/byte_array.h"
@@ -217,7 +218,7 @@ ExceptionOr<size_t> NearbyClientSocket::Skip(size_t offset) {
return {Exception::kIo};
}
Exception NearbyClientSocket::Write(const ByteArray& data) {
Exception NearbyClientSocket::Write(absl::string_view data) {
if (socket_ == INVALID_SOCKET) {
LOG(WARNING) << "Trying to write to an invalid socket.";
return {Exception::kIo};
@@ -21,6 +21,7 @@
#include <cstdint>
#include "absl/base/nullability.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
@@ -45,7 +46,7 @@ class NearbyClientSocket {
bool Connect(const SocketAddress& server_address, absl::Duration timeout);
ExceptionOr<ByteArray> Read(std::int64_t size);
ExceptionOr<size_t> Skip(size_t offset);
Exception Write(const ByteArray& data);
Exception Write(absl::string_view data);
Exception Flush();
Exception Close();
@@ -80,7 +81,7 @@ class SocketOutputStream : public OutputStream {
: client_socket_(client_socket) {}
~SocketOutputStream() override = default;
Exception Write(const ByteArray& data) override {
Exception Write(absl::string_view data) override {
return client_socket_->Write(data);
}
Exception Flush() override { return client_socket_->Flush(); }
@@ -132,7 +132,7 @@ TEST(NearbyClientSocketTest, Write) {
/*addrlen=*/&peer_address_length);
EXPECT_NE(accept_socket, INVALID_SOCKET);
EXPECT_TRUE(client_socket.Write(ByteArray("hello")).Ok());
EXPECT_TRUE(client_socket.Write("hello").Ok());
std::string buffer;
buffer.resize(5);
EXPECT_EQ(recv(accept_socket, buffer.data(), 5, 0), 5);