Internal refactor

PiperOrigin-RevId: 727057179
This commit is contained in:
Guogang Li
2025-02-14 14:07:18 -08:00
committed by Copybara-Service
parent 60d5fe440e
commit eea3441da9
12 changed files with 116 additions and 142 deletions
+1 -1
View File
@@ -514,13 +514,13 @@ let package = Package(
"internal/platform/feature_flags_test.cc",
"internal/platform/cancelable_alarm_test.cc",
"internal/platform/crypto_test.cc",
"internal/platform/base_input_stream_test.cc",
"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/stream_reader_test.cc",
"internal/platform/stream_writer_test.cc",
"internal/platform/count_down_latch_test.cc",
"internal/platform/pipe_test.cc",
+13 -16
View File
@@ -23,10 +23,10 @@
#include "absl/strings/str_cat.h"
#include "connections/implementation/base_pcp_handler.h"
#include "connections/implementation/pcp.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/bluetooth_utils.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/stream_reader.h"
namespace nearby {
namespace connections {
@@ -113,9 +113,9 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
}
ByteArray advertisement_bytes{ble_advertisement_bytes};
BaseInputStream base_input_stream{advertisement_bytes};
StreamReader stream_reader{advertisement_bytes};
// The first 1 byte is supposed to be the version and pcp.
auto version_and_pcp_byte = base_input_stream.ReadUint8();
auto version_and_pcp_byte = stream_reader.ReadUint8();
if (!version_and_pcp_byte.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: version_and_pcp.");
@@ -145,8 +145,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// advertisement.
ByteArray service_id_hash;
if (!fast_advertisement) {
auto service_id_hash_bytes =
base_input_stream.ReadBytes(kServiceIdHashLength);
auto service_id_hash_bytes = stream_reader.ReadBytes(kServiceIdHashLength);
if (!service_id_hash_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: service_id_hash.");
@@ -156,7 +155,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
}
// The next 4 bytes are supposed to be the endpoint_id.
auto endpoint_id_bytes = base_input_stream.ReadBytes(kEndpointIdLength);
auto endpoint_id_bytes = stream_reader.ReadBytes(kEndpointIdLength);
if (!endpoint_id_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: endpoint_id.");
@@ -165,7 +164,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
std::string endpoint_id = std::string{*endpoint_id_bytes};
// The next 1 byte is supposed to be the length of the endpoint_info.
auto expected_endpoint_info_length = base_input_stream.ReadUint8();
auto expected_endpoint_info_length = stream_reader.ReadUint8();
if (!expected_endpoint_info_length.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: endpoint_info_length.");
@@ -174,7 +173,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// The next x bytes are the endpoint info. (Max length is 131 bytes or 17
// bytes as fast_advertisement being true).
auto endpoint_info_bytes =
base_input_stream.ReadBytes(*expected_endpoint_info_length);
stream_reader.ReadBytes(*expected_endpoint_info_length);
if (!endpoint_info_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: endpoint_info.");
@@ -197,7 +196,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
std::string bluetooth_mac_address;
if (!fast_advertisement) {
auto bluetooth_mac_address_bytes =
base_input_stream.ReadBytes(BluetoothUtils::kBluetoothMacAddressLength);
stream_reader.ReadBytes(BluetoothUtils::kBluetoothMacAddressLength);
if (!bluetooth_mac_address_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: bluetooth_mac_address.");
@@ -211,8 +210,8 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// it for remaining bytes.
ByteArray uwb_address;
BleAdvertisement ble_advertisement;
if (base_input_stream.IsAvailable(1)) {
auto expected_uwb_address_length = base_input_stream.ReadUint8();
if (stream_reader.IsAvailable(1)) {
auto expected_uwb_address_length = stream_reader.ReadUint8();
if (!expected_uwb_address_length.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: uwb_address_length.");
@@ -220,7 +219,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// If the length of uwb_address is not zero, then retrieve it.
if (expected_uwb_address_length != 0) {
auto uwb_address_bytes =
base_input_stream.ReadBytes(*expected_uwb_address_length);
stream_reader.ReadBytes(*expected_uwb_address_length);
if (!uwb_address_bytes.has_value()) {
return absl::InvalidArgumentError(
@@ -231,8 +230,8 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// The next 1 byte is extra field.
if (!fast_advertisement) {
if (base_input_stream.IsAvailable(kExtraFieldLength)) {
auto extra_field = base_input_stream.ReadUint8();
if (stream_reader.IsAvailable(kExtraFieldLength)) {
auto extra_field = stream_reader.ReadUint8();
if (!extra_field.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: extra_field.");
@@ -245,8 +244,6 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
}
}
base_input_stream.Close();
ble_advertisement.fast_advertisement_ = fast_advertisement;
ble_advertisement.version_ = version;
ble_advertisement.pcp_ = pcp;
@@ -24,9 +24,9 @@
#include "connections/implementation/base_pcp_handler.h"
#include "connections/implementation/pcp.h"
#include "internal/platform/base64_utils.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/stream_reader.h"
namespace nearby {
namespace connections {
@@ -75,9 +75,9 @@ BluetoothDeviceName::BluetoothDeviceName(
return;
}
BaseInputStream base_input_stream{bluetooth_device_name_bytes};
StreamReader stream_reader{bluetooth_device_name_bytes};
// The first 1 byte is supposed to be the version and pcp.
auto version_and_pcp_byte = base_input_stream.ReadUint8();
auto version_and_pcp_byte = stream_reader.ReadUint8();
if (!version_and_pcp_byte.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: version_and_pcp.";
return;
@@ -104,7 +104,7 @@ BluetoothDeviceName::BluetoothDeviceName(
}
// The next 4 bytes are supposed to be the endpoint_id.
auto endpoint_id_bytes = base_input_stream.ReadBytes(kEndpointIdLength);
auto endpoint_id_bytes = stream_reader.ReadBytes(kEndpointIdLength);
if (!endpoint_id_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: endpoint_id.";
return;
@@ -112,8 +112,7 @@ BluetoothDeviceName::BluetoothDeviceName(
endpoint_id_ = std::string{*endpoint_id_bytes};
// The next 3 bytes are supposed to be the service_id_hash.
auto service_id_hash_bytes =
base_input_stream.ReadBytes(kServiceIdHashLength);
auto service_id_hash_bytes = stream_reader.ReadBytes(kServiceIdHashLength);
if (!service_id_hash_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: service_id_hash.";
endpoint_id_.clear();
@@ -123,7 +122,7 @@ BluetoothDeviceName::BluetoothDeviceName(
service_id_hash_ = *service_id_hash_bytes;
// The next 1 byte is field containing WebRtc state.
auto field_byte = base_input_stream.ReadUint8();
auto field_byte = stream_reader.ReadUint8();
if (!field_byte.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: extra_field.";
endpoint_id_.clear();
@@ -135,10 +134,10 @@ BluetoothDeviceName::BluetoothDeviceName(
// The next 6 bytes are supposed to be reserved, and can be left
// untouched.
base_input_stream.ReadBytes(kReservedLength);
stream_reader.ReadBytes(kReservedLength);
// The next 1 byte is supposed to be the length of the endpoint_info.
auto expected_endpoint_info_length = base_input_stream.ReadUint8();
auto expected_endpoint_info_length = stream_reader.ReadUint8();
if (!expected_endpoint_info_length.has_value()) {
LOG(INFO)
<< "Cannot deserialize BluetoothDeviceName: endpoint_info_length.";
@@ -148,7 +147,7 @@ BluetoothDeviceName::BluetoothDeviceName(
// The rest bytes are supposed to be the endpoint_info
auto endpoint_info_bytes =
base_input_stream.ReadBytes(*expected_endpoint_info_length);
stream_reader.ReadBytes(*expected_endpoint_info_length);
if (!endpoint_info_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: endpoint_info.";
endpoint_id_.clear();
@@ -159,9 +158,9 @@ BluetoothDeviceName::BluetoothDeviceName(
// If the input stream has extra bytes, it's for UWB address. The first byte
// is the address length. It can be 2-byte short address or 8-byte extended
// address.
if (base_input_stream.IsAvailable(1)) {
if (stream_reader.IsAvailable(1)) {
// The next 1 byte is supposed to be the length of the uwb_address.
auto expected_uwb_address_length = base_input_stream.ReadUint8();
auto expected_uwb_address_length = stream_reader.ReadUint8();
if (!expected_uwb_address_length.has_value()) {
LOG(INFO)
<< "Cannot deserialize BluetoothDeviceName: uwb_address_length.";
@@ -172,7 +171,7 @@ BluetoothDeviceName::BluetoothDeviceName(
// If the length of usb_address is not zero, then retrieve it.
if (expected_uwb_address_length != 0) {
auto uwb_address_bytes =
base_input_stream.ReadBytes(*expected_uwb_address_length);
stream_reader.ReadBytes(*expected_uwb_address_length);
if (!uwb_address_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: uwb_address.";
endpoint_id_.clear();
@@ -24,9 +24,9 @@
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/stream_reader.h"
namespace nearby {
namespace connections {
@@ -98,10 +98,10 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
}
ByteArray advertisement_bytes(ble_advertisement_bytes);
BaseInputStream base_input_stream(advertisement_bytes);
StreamReader stream_reader(advertisement_bytes);
// The first 1 byte is supposed to be the version, socket version and the fast
// advertisement flag.
auto version_byte = base_input_stream.ReadUint8();
auto version_byte = stream_reader.ReadUint8();
if (!version_byte.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: version.");
@@ -129,8 +129,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// advertisement.
ByteArray service_id_hash;
if (!fast_advertisement) {
auto service_id_hash_bytes =
base_input_stream.ReadBytes(kServiceIdHashLength);
auto service_id_hash_bytes = stream_reader.ReadBytes(kServiceIdHashLength);
if (!service_id_hash_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: service_id_hash.");
@@ -141,15 +140,14 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// Data length.
uint32_t expected_data_size;
if (fast_advertisement) {
auto fast_data_size_bytes =
base_input_stream.ReadBytes(kFastDataSizeLength);
auto fast_data_size_bytes = stream_reader.ReadBytes(kFastDataSizeLength);
if (!fast_data_size_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: fast_data_size.");
}
expected_data_size = static_cast<uint32_t>(fast_data_size_bytes->data()[0]);
} else {
auto data_size_bytes = base_input_stream.ReadUint32();
auto data_size_bytes = stream_reader.ReadUint32();
if (!data_size_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: data_size.");
@@ -161,7 +159,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// Check that the stated data size is the same as what we received.
ByteArray data;
if (expected_data_size > 0) {
auto data_bytes = base_input_stream.ReadBytes(expected_data_size);
auto data_bytes = stream_reader.ReadBytes(expected_data_size);
if (!data_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: data.");
@@ -178,8 +176,8 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// Device token. If the number of remaining bytes are valid for device token,
// then read it.
if (base_input_stream.IsAvailable(kDeviceTokenLength)) {
auto device_token_bytes = base_input_stream.ReadBytes(kDeviceTokenLength);
if (stream_reader.IsAvailable(kDeviceTokenLength)) {
auto device_token_bytes = stream_reader.ReadBytes(kDeviceTokenLength);
if (!device_token_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: device_token.");
@@ -196,9 +194,8 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// to put a random or empty device token in the advertisement.
int extra_fields_byte_number =
kExtraFieldsMaskLength + BleAdvertisementHeader::kPsmValueByteLength;
if (base_input_stream.IsAvailable(extra_fields_byte_number)) {
auto extra_fields_bytes =
base_input_stream.ReadBytes(extra_fields_byte_number);
if (stream_reader.IsAvailable(extra_fields_byte_number)) {
auto extra_fields_bytes = stream_reader.ReadBytes(extra_fields_byte_number);
if (!extra_fields_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: extra_field.");
@@ -304,18 +301,17 @@ BleAdvertisement::BleExtraFields::BleExtraFields(
}
ByteArray mutated_extra_fields_bytes = {ble_extra_fields_bytes};
BaseInputStream base_input_stream{mutated_extra_fields_bytes};
StreamReader stream_reader{mutated_extra_fields_bytes};
// The first 1 byte is field mask.
auto mask_byte = base_input_stream.ReadUint8().value_or(0);
auto mask_byte = stream_reader.ReadUint8().value_or(0);
if (!mask_byte) {
return;
}
// The next 2 bytes are supposed to be the psm value.
if (HasField(mask_byte, kPsmBitmask) &&
base_input_stream.IsAvailable(
BleAdvertisementHeader::kPsmValueByteLength)) {
psm_ = base_input_stream.ReadUint16().value_or(0);
stream_reader.IsAvailable(BleAdvertisementHeader::kPsmValueByteLength)) {
psm_ = stream_reader.ReadUint16().value_or(0);
}
}
@@ -21,9 +21,9 @@
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/base64_utils.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/stream_reader.h"
namespace nearby {
namespace connections {
@@ -86,9 +86,9 @@ BleAdvertisementHeader::BleAdvertisementHeader(
return;
}
BaseInputStream base_input_stream(advertisement_header_bytes);
StreamReader stream_reader(advertisement_header_bytes);
// The first 1 byte is supposed to be the version and number of slots.
auto version_and_num_slots_byte = base_input_stream.ReadUint8();
auto version_and_num_slots_byte = stream_reader.ReadUint8();
if (!version_and_num_slots_byte.has_value()) {
LOG(INFO) << "Cannot deserialize BleAdvertisementHeader: version_and_num.";
return;
@@ -114,17 +114,16 @@ BleAdvertisementHeader::BleAdvertisementHeader(
// The next 10 bytes are supposed to be the service_id_bloom_filter.
service_id_bloom_filter_ =
base_input_stream.ReadBytes(kServiceIdBloomFilterByteLength)
stream_reader.ReadBytes(kServiceIdBloomFilterByteLength)
.value_or(ByteArray());
// The next 4 bytes are supposed to be the advertisement_hash.
advertisement_hash_ =
base_input_stream.ReadBytes(kAdvertisementHashByteLength)
.value_or(ByteArray());
advertisement_hash_ = stream_reader.ReadBytes(kAdvertisementHashByteLength)
.value_or(ByteArray());
// The next 2 bytes are PSM value.
if (base_input_stream.IsAvailable(kPsmValueByteLength)) {
psm_ = base_input_stream.ReadInt16().value_or(0);
if (stream_reader.IsAvailable(kPsmValueByteLength)) {
psm_ = stream_reader.ReadInt16().value_or(0);
}
}
@@ -21,9 +21,9 @@
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/stream_reader.h"
#include "proto/mediums/ble_frames.pb.h"
namespace nearby {
@@ -137,10 +137,9 @@ BlePacket::BlePacket(const ByteArray& ble_packet_bytes) {
}
ByteArray packet_bytes(ble_packet_bytes);
BaseInputStream base_input_stream{packet_bytes};
StreamReader stream_reader{packet_bytes};
// The first 3 bytes are supposed to be the service_id_hash.
auto service_id_hash_bytes =
base_input_stream.ReadBytes(kServiceIdHashLength);
auto service_id_hash_bytes = stream_reader.ReadBytes(kServiceIdHashLength);
if (!service_id_hash_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BlePacket: service_id_hash.";
return;
@@ -155,8 +154,8 @@ BlePacket::BlePacket(const ByteArray& ble_packet_bytes) {
}
// The rest bytes are supposed to be the data.
auto data_bytes = base_input_stream.ReadBytes(ble_packet_bytes.size() -
kServiceIdHashLength);
auto data_bytes =
stream_reader.ReadBytes(ble_packet_bytes.size() - kServiceIdHashLength);
if (!data_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BlePacket: data.";
return;
@@ -23,10 +23,10 @@
#include "connections/implementation/base_pcp_handler.h"
#include "connections/implementation/pcp.h"
#include "internal/platform/base64_utils.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/nsd_service_info.h"
#include "internal/platform/stream_reader.h"
namespace nearby {
namespace connections {
@@ -96,9 +96,9 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
return;
}
BaseInputStream base_input_stream{service_info_bytes};
StreamReader stream_reader{service_info_bytes};
// The first 1 byte is supposed to be the version and pcp.
auto version_and_pcp_byte = base_input_stream.ReadUint8();
auto version_and_pcp_byte = stream_reader.ReadUint8();
if (!version_and_pcp_byte.has_value()) {
LOG(INFO) << "Cannot deserialize WifiLanServiceInfo: version_and_pcp.";
return;
@@ -124,7 +124,7 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
}
// The next 4 bytes are supposed to be the endpoint_id.
auto endpoint_id_bytes = base_input_stream.ReadBytes(kEndpointIdLength);
auto endpoint_id_bytes = stream_reader.ReadBytes(kEndpointIdLength);
if (!endpoint_id_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize WifiLanServiceInfo: endpoint_id.";
return;
@@ -132,8 +132,7 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
endpoint_id_ = std::string{*endpoint_id_bytes};
// The next 3 bytes are supposed to be the service_id_hash.
auto service_id_hash_bytes =
base_input_stream.ReadBytes(kServiceIdHashLength);
auto service_id_hash_bytes = stream_reader.ReadBytes(kServiceIdHashLength);
if (!service_id_hash_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize WifiLanServiceInfo: service_id_hash.";
endpoint_id_.clear();
@@ -144,13 +143,12 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
// The next 1 byte is supposed to be the length of the uwb_address. If
// available, continues to deserialize UWB address and extra field of WebRtc
// state.
if (base_input_stream.IsAvailable(1)) {
auto expected_uwb_address_length =
base_input_stream.ReadUint8().value_or(0);
if (stream_reader.IsAvailable(1)) {
auto expected_uwb_address_length = stream_reader.ReadUint8().value_or(0);
// If the length of uwb_address is not zero, then retrieve it.
if (expected_uwb_address_length != 0) {
auto uwb_address_bytes =
base_input_stream.ReadBytes(expected_uwb_address_length);
stream_reader.ReadBytes(expected_uwb_address_length);
if (!uwb_address_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize WifiLanServiceInfo: uwb_address.";
endpoint_id_.clear();
@@ -161,8 +159,8 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
// The next 1 byte is extra field.
web_rtc_state_ = WebRtcState::kUndefined;
if (base_input_stream.IsAvailable(kExtraFieldLength)) {
auto extra_field = base_input_stream.ReadUint8().value_or(0);
if (stream_reader.IsAvailable(kExtraFieldLength)) {
auto extra_field = stream_reader.ReadUint8().value_or(0);
web_rtc_state_ = (extra_field & kWebRtcConnectableFlagBitmask) == 1
? WebRtcState::kConnectable
: WebRtcState::kUnconnectable;
+3 -3
View File
@@ -82,13 +82,13 @@ cc_library(
cc_library(
name = "util",
srcs = [
"base_input_stream.cc",
"byte_utils.cc",
"stream_reader.cc",
"stream_writer.cc",
],
hdrs = [
"base_input_stream.h",
"byte_utils.h",
"stream_reader.h",
"stream_writer.h",
],
visibility = [
@@ -399,8 +399,8 @@ cc_test(
cc_test(
name = "platform_util_test",
srcs = [
"base_input_stream_test.cc",
"byte_utils_test.cc",
"stream_reader_test.cc",
"stream_writer_test.cc",
],
deps = [
+4 -5
View File
@@ -14,13 +14,12 @@
#include "internal/platform/byte_utils.h"
#include <cstdint>
#include <cstdlib>
#include <string>
#include "absl/strings/str_format.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/stream_reader.h"
namespace nearby {
@@ -28,9 +27,9 @@ std::string ByteUtils::ToFourDigitString(ByteArray& bytes) {
int multiplier = 1;
int hashCode = 0;
BaseInputStream base_input_stream{bytes};
while (base_input_stream.IsAvailable(1)) {
auto byte = base_input_stream.ReadInt8().value_or(0);
StreamReader stream_reader{bytes};
while (stream_reader.IsAvailable(1)) {
auto byte = stream_reader.ReadInt8().value_or(0);
hashCode = (hashCode + byte * multiplier) % kHashBasePrime;
multiplier = multiplier * kHashBaseMultiplier % kHashBasePrime;
}
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/base_input_stream.h"
#include "internal/platform/stream_reader.h"
#include <cstddef>
#include <cstdint>
@@ -23,22 +23,7 @@
namespace nearby {
ExceptionOr<ByteArray> BaseInputStream::Read(std::int64_t size) {
if (!IsAvailable(size)) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
ByteArray read_bytes{static_cast<size_t>(size)};
if (read_bytes.CopyAt(/*offset=*/0, buffer_,
/*source_offset=*/position_)) {
position_ += size;
return ExceptionOr<ByteArray>{read_bytes};
} else {
return ExceptionOr<ByteArray>{Exception::kIo};
}
}
std::optional<std::uint8_t> BaseInputStream::ReadBits(int bits) {
std::optional<std::uint8_t> StreamReader::ReadBits(int bits) {
if (bits > 8 || bits <= 0) {
return std::nullopt;
}
@@ -59,7 +44,7 @@ std::optional<std::uint8_t> BaseInputStream::ReadBits(int bits) {
return value;
}
std::optional<std::uint8_t> BaseInputStream::ReadUint8() {
std::optional<std::uint8_t> StreamReader::ReadUint8() {
constexpr int byte_size = sizeof(std::uint8_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -70,7 +55,7 @@ std::optional<std::uint8_t> BaseInputStream::ReadUint8() {
return static_cast<std::uint8_t>(data[0]);
}
std::optional<std::int8_t> BaseInputStream::ReadInt8() {
std::optional<std::int8_t> StreamReader::ReadInt8() {
constexpr int byte_size = sizeof(std::int8_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -81,7 +66,7 @@ std::optional<std::int8_t> BaseInputStream::ReadInt8() {
return static_cast<std::int8_t>(data[0]);
}
std::optional<std::uint16_t> BaseInputStream::ReadUint16() {
std::optional<std::uint16_t> StreamReader::ReadUint16() {
constexpr int byte_size = sizeof(std::uint16_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -94,7 +79,7 @@ std::optional<std::uint16_t> BaseInputStream::ReadUint16() {
return static_cast<uint16_t>(data[0] << 8 | data[1]);
}
std::optional<std::int16_t> BaseInputStream::ReadInt16() {
std::optional<std::int16_t> StreamReader::ReadInt16() {
constexpr int byte_size = sizeof(std::int16_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -107,7 +92,7 @@ std::optional<std::int16_t> BaseInputStream::ReadInt16() {
return static_cast<int16_t>(data[0] << 8 | data[1]);
}
std::optional<std::uint32_t> BaseInputStream::ReadUint32() {
std::optional<std::uint32_t> StreamReader::ReadUint32() {
constexpr int byte_size = sizeof(std::uint32_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -121,7 +106,7 @@ std::optional<std::uint32_t> BaseInputStream::ReadUint32() {
data[3]);
}
std::optional<std::int32_t> BaseInputStream::ReadInt32() {
std::optional<std::int32_t> StreamReader::ReadInt32() {
constexpr int byte_size = sizeof(std::uint32_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -135,7 +120,7 @@ std::optional<std::int32_t> BaseInputStream::ReadInt32() {
data[3]);
}
std::optional<std::uint64_t> BaseInputStream::ReadUint64() {
std::optional<std::uint64_t> StreamReader::ReadUint64() {
constexpr int byte_size = sizeof(std::uint64_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -154,7 +139,7 @@ std::optional<std::uint64_t> BaseInputStream::ReadUint64() {
static_cast<uint64_t>(data[6]) << 8 | static_cast<uint64_t>(data[7]);
}
std::optional<std::int64_t> BaseInputStream::ReadInt64() {
std::optional<std::int64_t> StreamReader::ReadInt64() {
constexpr int byte_size = sizeof(std::int64_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -173,7 +158,7 @@ std::optional<std::int64_t> BaseInputStream::ReadInt64() {
static_cast<int64_t>(data[6]) << 8 | static_cast<int64_t>(data[7]);
}
std::optional<ByteArray> BaseInputStream::ReadBytes(int size) {
std::optional<ByteArray> StreamReader::ReadBytes(int size) {
if (bits_unused_ != 0) {
return std::nullopt;
}
@@ -186,4 +171,19 @@ std::optional<ByteArray> BaseInputStream::ReadBytes(int size) {
return read_bytes_result.result();
}
ExceptionOr<ByteArray> StreamReader::Read(std::int64_t size) {
if (!IsAvailable(size)) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
ByteArray read_bytes{static_cast<size_t>(size)};
if (read_bytes.CopyAt(/*offset=*/0, buffer_,
/*source_offset=*/position_)) {
position_ += size;
return ExceptionOr<ByteArray>{read_bytes};
} else {
return ExceptionOr<ByteArray>{Exception::kIo};
}
}
} // namespace nearby
@@ -12,40 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_BASE_BASE_INPUT_STREAM_H_
#define PLATFORM_BASE_BASE_INPUT_STREAM_H_
#ifndef PLATFORM_BASE_STREAM_READERH_
#define PLATFORM_BASE_STREAM_READERH_
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/input_stream.h"
namespace nearby {
// A base {@link InputStream } for reading the contents of a byte array.
class BaseInputStream : public InputStream {
class StreamReader {
public:
explicit BaseInputStream(ByteArray &buffer) : buffer_{buffer} {}
BaseInputStream(const BaseInputStream &) = delete;
BaseInputStream &operator=(const BaseInputStream &) = delete;
~BaseInputStream() override { Close(); }
ExceptionOr<ByteArray> Read(std::int64_t size) override;
ExceptionOr<size_t> Skip(size_t offset) override {
size_t real_offset = std::min(offset, buffer_.size() - position_);
position_ += real_offset;
return ExceptionOr<size_t>(real_offset);
}
Exception Close() override {
// Do nothing.
return {Exception::kSuccess};
}
explicit StreamReader(ByteArray &buffer) : buffer_{buffer} {}
StreamReader(const StreamReader &) = delete;
StreamReader &operator=(const StreamReader &) = delete;
~StreamReader() = default;
// Reads less than 8 bits from the stream, returning the value if available.
// The read bits cannot across a byte boundary.
@@ -65,6 +50,8 @@ class BaseInputStream : public InputStream {
}
private:
ExceptionOr<ByteArray> Read(std::int64_t size);
uint8_t bits_unused_{0};
uint8_t bits_buffer_{0};
ByteArray &buffer_;
@@ -73,4 +60,4 @@ class BaseInputStream : public InputStream {
} // namespace nearby
#endif // PLATFORM_BASE_BASE_INPUT_STREAM_H_
#endif // PLATFORM_BASE_STREAM_READERH_
@@ -11,7 +11,7 @@
// 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/base_input_stream.h"
#include "internal/platform/stream_reader.h"
#include <cstdint>
#include <string>
@@ -22,10 +22,10 @@
namespace nearby {
namespace {
TEST(BaseInputStreamTest, ReadBits) {
TEST(StreamReaderTest, ReadBits) {
std::string data{static_cast<char>(0b01011100)};
ByteArray byte_array(data);
BaseInputStream stream{byte_array};
StreamReader stream{byte_array};
EXPECT_EQ(stream.ReadBits(1), 0);
EXPECT_EQ(stream.ReadBits(2), 2);
EXPECT_EQ(stream.ReadBits(3), 7);
@@ -34,28 +34,28 @@ TEST(BaseInputStreamTest, ReadBits) {
EXPECT_FALSE(stream.ReadBits(1).has_value());
}
TEST(BaseInputStreamTest, ReadBitsExceedsByteBoundary) {
TEST(StreamReaderTest, ReadBitsExceedsByteBoundary) {
std::string data = "ab";
ByteArray byte_array(data);
BaseInputStream stream{byte_array};
StreamReader stream{byte_array};
EXPECT_FALSE(stream.ReadBits(9).has_value());
EXPECT_EQ(stream.ReadBits(1), 0);
EXPECT_FALSE(stream.ReadInt16().has_value());
}
TEST(BaseInputStreamTest, ReadUintValues) {
TEST(StreamReaderTest, ReadUintValues) {
std::string data = "\xff\xf1\x0f\x0e\x01\x02\x01\x01\x01\x02\x03\x04\x05\x06";
ByteArray byte_array(data);
BaseInputStream stream{byte_array};
StreamReader stream{byte_array};
EXPECT_EQ(stream.ReadUint16(), 0xfff1);
EXPECT_EQ(stream.ReadUint32(), 0x0f0e0102);
EXPECT_EQ(stream.ReadUint64(), 0x0101010203040506);
}
TEST(BaseInputStreamTest, ReadIntValues) {
TEST(StreamReaderTest, ReadIntValues) {
std::string data = "\xff\xf1\x0f\x0e\x01\x02\x01\x01\x01\x02\x03\x04\x05\x06";
ByteArray byte_array(data);
BaseInputStream stream{byte_array};
StreamReader stream{byte_array};
EXPECT_EQ(stream.ReadInt16(), static_cast<int16_t>(0xfff1));
EXPECT_EQ(stream.ReadInt32(), static_cast<int32_t>(0x0f0e0102));
EXPECT_EQ(stream.ReadInt64(), static_cast<int64_t>(0x0101010203040506));