Skip to content

Commit 410ea94

Browse files
Replace custom Read methods with existing BlobReader from System.Reflection.Metadata
Co-authored-by: davidnguyen-tech <87228593+davidnguyen-tech@users.noreply.github.com>
1 parent 5f25ed5 commit 410ea94

File tree

2 files changed

+18
-44
lines changed

2 files changed

+18
-44
lines changed

src/tasks/AndroidAppBuilder/AndroidAppBuilder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
66
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
77
<Nullable>enable</Nullable>
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
89
<NoWarn>$(NoWarn),CA1050,CA1850</NoWarn>
910
</PropertyGroup>
1011
<ItemGroup>

src/tasks/AndroidAppBuilder/ApkBuilder.cs

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.IO.Compression;
88
using System.Linq;
9+
using System.Reflection.Metadata;
910
using System.Text;
1011
using System.Text.Json;
1112
using System.Text.RegularExpressions;
@@ -716,18 +717,24 @@ private Dictionary<string, string> ParseRuntimeConfigProperties()
716717
{
717718
if (File.Exists(runtimeConfigPath))
718719
{
719-
using var stream = new FileStream(runtimeConfigPath, FileMode.Open, FileAccess.Read, FileShare.Read);
720-
using var reader = new BinaryReader(stream);
720+
byte[] fileBytes = File.ReadAllBytes(runtimeConfigPath);
721+
unsafe
722+
{
723+
fixed (byte* ptr = fileBytes)
724+
{
725+
var blobReader = new BlobReader(ptr, fileBytes.Length);
721726

722-
// Read the compressed integer count
723-
int count = ReadCompressedInteger(reader);
727+
// Read the compressed integer count
728+
int count = blobReader.ReadCompressedInteger();
724729

725-
// Read each key-value pair
726-
for (int i = 0; i < count; i++)
727-
{
728-
string key = ReadSerializedString(reader);
729-
string value = ReadSerializedString(reader);
730-
configProperties[key] = value;
730+
// Read each key-value pair
731+
for (int i = 0; i < count; i++)
732+
{
733+
string key = blobReader.ReadSerializedString() ?? string.Empty;
734+
string value = blobReader.ReadSerializedString() ?? string.Empty;
735+
configProperties[key] = value;
736+
}
737+
}
731738
}
732739
}
733740
else
@@ -742,38 +749,4 @@ private Dictionary<string, string> ParseRuntimeConfigProperties()
742749

743750
return configProperties;
744751
}
745-
746-
private static int ReadCompressedInteger(BinaryReader reader)
747-
{
748-
// This mirrors the format used by BlobBuilder.WriteCompressedInteger
749-
byte firstByte = reader.ReadByte();
750-
751-
if ((firstByte & 0x80) == 0)
752-
{
753-
// Single byte format
754-
return firstByte;
755-
}
756-
else if ((firstByte & 0xC0) == 0x80)
757-
{
758-
// Two byte format
759-
byte secondByte = reader.ReadByte();
760-
return ((firstByte & 0x3F) << 8) | secondByte;
761-
}
762-
else
763-
{
764-
// Four byte format
765-
byte secondByte = reader.ReadByte();
766-
byte thirdByte = reader.ReadByte();
767-
byte fourthByte = reader.ReadByte();
768-
return ((firstByte & 0x1F) << 24) | (secondByte << 16) | (thirdByte << 8) | fourthByte;
769-
}
770-
}
771-
772-
private static string ReadSerializedString(BinaryReader reader)
773-
{
774-
// This mirrors the format used by BlobBuilder.WriteSerializedString
775-
int length = ReadCompressedInteger(reader);
776-
byte[] bytes = reader.ReadBytes(length);
777-
return System.Text.Encoding.UTF8.GetString(bytes);
778-
}
779752
}

0 commit comments

Comments
 (0)