diff --git a/Package.swift b/Package.swift index 03fc2193..1395545d 100644 --- a/Package.swift +++ b/Package.swift @@ -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", diff --git a/connections/implementation/ble_advertisement.cc b/connections/implementation/ble_advertisement.cc index 972440d2..6600d04a 100644 --- a/connections/implementation/ble_advertisement.cc +++ b/connections/implementation/ble_advertisement.cc @@ -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::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::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::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::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::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::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::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::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::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::CreateBleAdvertisement( } } - base_input_stream.Close(); - ble_advertisement.fast_advertisement_ = fast_advertisement; ble_advertisement.version_ = version; ble_advertisement.pcp_ = pcp; diff --git a/connections/implementation/bluetooth_device_name.cc b/connections/implementation/bluetooth_device_name.cc index ea228427..47becf48 100644 --- a/connections/implementation/bluetooth_device_name.cc +++ b/connections/implementation/bluetooth_device_name.cc @@ -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(); diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement.cc b/connections/implementation/mediums/ble_v2/ble_advertisement.cc index bd6e9c39..15690cd1 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement.cc +++ b/connections/implementation/mediums/ble_v2/ble_advertisement.cc @@ -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::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::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::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(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::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::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::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); } } diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc b/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc index 44d72df4..717ee916 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc +++ b/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc @@ -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); } } diff --git a/connections/implementation/mediums/ble_v2/ble_packet.cc b/connections/implementation/mediums/ble_v2/ble_packet.cc index 5bb6e884..244b2c8e 100644 --- a/connections/implementation/mediums/ble_v2/ble_packet.cc +++ b/connections/implementation/mediums/ble_v2/ble_packet.cc @@ -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; diff --git a/connections/implementation/wifi_lan_service_info.cc b/connections/implementation/wifi_lan_service_info.cc index 7857cb0a..9938e01d 100644 --- a/connections/implementation/wifi_lan_service_info.cc +++ b/connections/implementation/wifi_lan_service_info.cc @@ -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; diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 626649c4..792154af 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -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 = [ diff --git a/internal/platform/byte_utils.cc b/internal/platform/byte_utils.cc index c1bb4613..3c261c24 100644 --- a/internal/platform/byte_utils.cc +++ b/internal/platform/byte_utils.cc @@ -14,13 +14,12 @@ #include "internal/platform/byte_utils.h" -#include #include #include #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; } diff --git a/internal/platform/base_input_stream.cc b/internal/platform/stream_reader.cc similarity index 87% rename from internal/platform/base_input_stream.cc rename to internal/platform/stream_reader.cc index 3335ea1b..f6bf4741 100644 --- a/internal/platform/base_input_stream.cc +++ b/internal/platform/stream_reader.cc @@ -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 #include @@ -23,22 +23,7 @@ namespace nearby { -ExceptionOr BaseInputStream::Read(std::int64_t size) { - if (!IsAvailable(size)) { - return ExceptionOr{Exception::kIo}; - } - - ByteArray read_bytes{static_cast(size)}; - if (read_bytes.CopyAt(/*offset=*/0, buffer_, - /*source_offset=*/position_)) { - position_ += size; - return ExceptionOr{read_bytes}; - } else { - return ExceptionOr{Exception::kIo}; - } -} - -std::optional BaseInputStream::ReadBits(int bits) { +std::optional StreamReader::ReadBits(int bits) { if (bits > 8 || bits <= 0) { return std::nullopt; } @@ -59,7 +44,7 @@ std::optional BaseInputStream::ReadBits(int bits) { return value; } -std::optional BaseInputStream::ReadUint8() { +std::optional StreamReader::ReadUint8() { constexpr int byte_size = sizeof(std::uint8_t); std::optional read_bytes = ReadBytes(byte_size); if (!read_bytes.has_value()) { @@ -70,7 +55,7 @@ std::optional BaseInputStream::ReadUint8() { return static_cast(data[0]); } -std::optional BaseInputStream::ReadInt8() { +std::optional StreamReader::ReadInt8() { constexpr int byte_size = sizeof(std::int8_t); std::optional read_bytes = ReadBytes(byte_size); if (!read_bytes.has_value()) { @@ -81,7 +66,7 @@ std::optional BaseInputStream::ReadInt8() { return static_cast(data[0]); } -std::optional BaseInputStream::ReadUint16() { +std::optional StreamReader::ReadUint16() { constexpr int byte_size = sizeof(std::uint16_t); std::optional read_bytes = ReadBytes(byte_size); if (!read_bytes.has_value()) { @@ -94,7 +79,7 @@ std::optional BaseInputStream::ReadUint16() { return static_cast(data[0] << 8 | data[1]); } -std::optional BaseInputStream::ReadInt16() { +std::optional StreamReader::ReadInt16() { constexpr int byte_size = sizeof(std::int16_t); std::optional read_bytes = ReadBytes(byte_size); if (!read_bytes.has_value()) { @@ -107,7 +92,7 @@ std::optional BaseInputStream::ReadInt16() { return static_cast(data[0] << 8 | data[1]); } -std::optional BaseInputStream::ReadUint32() { +std::optional StreamReader::ReadUint32() { constexpr int byte_size = sizeof(std::uint32_t); std::optional read_bytes = ReadBytes(byte_size); if (!read_bytes.has_value()) { @@ -121,7 +106,7 @@ std::optional BaseInputStream::ReadUint32() { data[3]); } -std::optional BaseInputStream::ReadInt32() { +std::optional StreamReader::ReadInt32() { constexpr int byte_size = sizeof(std::uint32_t); std::optional read_bytes = ReadBytes(byte_size); if (!read_bytes.has_value()) { @@ -135,7 +120,7 @@ std::optional BaseInputStream::ReadInt32() { data[3]); } -std::optional BaseInputStream::ReadUint64() { +std::optional StreamReader::ReadUint64() { constexpr int byte_size = sizeof(std::uint64_t); std::optional read_bytes = ReadBytes(byte_size); if (!read_bytes.has_value()) { @@ -154,7 +139,7 @@ std::optional BaseInputStream::ReadUint64() { static_cast(data[6]) << 8 | static_cast(data[7]); } -std::optional BaseInputStream::ReadInt64() { +std::optional StreamReader::ReadInt64() { constexpr int byte_size = sizeof(std::int64_t); std::optional read_bytes = ReadBytes(byte_size); if (!read_bytes.has_value()) { @@ -173,7 +158,7 @@ std::optional BaseInputStream::ReadInt64() { static_cast(data[6]) << 8 | static_cast(data[7]); } -std::optional BaseInputStream::ReadBytes(int size) { +std::optional StreamReader::ReadBytes(int size) { if (bits_unused_ != 0) { return std::nullopt; } @@ -186,4 +171,19 @@ std::optional BaseInputStream::ReadBytes(int size) { return read_bytes_result.result(); } +ExceptionOr StreamReader::Read(std::int64_t size) { + if (!IsAvailable(size)) { + return ExceptionOr{Exception::kIo}; + } + + ByteArray read_bytes{static_cast(size)}; + if (read_bytes.CopyAt(/*offset=*/0, buffer_, + /*source_offset=*/position_)) { + position_ += size; + return ExceptionOr{read_bytes}; + } else { + return ExceptionOr{Exception::kIo}; + } +} + } // namespace nearby diff --git a/internal/platform/base_input_stream.h b/internal/platform/stream_reader.h similarity index 66% rename from internal/platform/base_input_stream.h rename to internal/platform/stream_reader.h index 0f5fd1cf..2131dc3b 100644 --- a/internal/platform/base_input_stream.h +++ b/internal/platform/stream_reader.h @@ -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 #include #include #include #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 Read(std::int64_t size) override; - - ExceptionOr Skip(size_t offset) override { - size_t real_offset = std::min(offset, buffer_.size() - position_); - position_ += real_offset; - return ExceptionOr(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 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_ diff --git a/internal/platform/base_input_stream_test.cc b/internal/platform/stream_reader_test.cc similarity index 82% rename from internal/platform/base_input_stream_test.cc rename to internal/platform/stream_reader_test.cc index a7466ebe..ef3016c7 100644 --- a/internal/platform/base_input_stream_test.cc +++ b/internal/platform/stream_reader_test.cc @@ -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 #include @@ -22,10 +22,10 @@ namespace nearby { namespace { -TEST(BaseInputStreamTest, ReadBits) { +TEST(StreamReaderTest, ReadBits) { std::string data{static_cast(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(0xfff1)); EXPECT_EQ(stream.ReadInt32(), static_cast(0x0f0e0102)); EXPECT_EQ(stream.ReadInt64(), static_cast(0x0101010203040506));