Skip to content

Commit

Permalink
Improved memory stream
Browse files Browse the repository at this point in the history
  • Loading branch information
uholeschak committed Dec 16, 2024
1 parent b2e5e97 commit df1ce71
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions BmwDeepObd/BmwDeepObd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.8" />
<PackageReference Include="Karamunting.Android.Skydoves.Balloon" Version="1.1.5" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.12.0" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.0" />
<PackageReference Include="Xamarin.AndroidX.Activity" Version="1.9.3.1" />
Expand Down
1 change: 1 addition & 0 deletions Tools/ApkUncompress/ApkUncompress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="ELFSharp" Version="2.17.3" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.8" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
</ItemGroup>

Expand Down
15 changes: 9 additions & 6 deletions Tools/ApkUncompress/ApkUncompressCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Xamarin.Android.AssemblyStore;
using Xamarin.Android.Tasks;
using System.Collections;
using Microsoft.IO;

namespace ApkUncompress;

Expand All @@ -18,6 +19,7 @@ public class ApkUncompressCommon
public const string AssembliesLibPath = "lib/";
public const string AssembliesPathApk = "assemblies/";
public const string AssembliesPathAab = "base/root/assemblies/";
public static readonly RecyclableMemoryStreamManager MemoryStreamManager = new RecyclableMemoryStreamManager();

private const uint CompressedDataMagic = 0x5A4C4158; // 'XALZ', little-endian
private readonly ArrayPool<byte> bytePool;
Expand Down Expand Up @@ -143,6 +145,7 @@ public bool UncompressFromAPK_IndividualElfFiles(ZipFile apk, string filePath, s
else if (entryFileName.StartsWith(MonoAndroidHelper.MANGLED_ASSEMBLY_SATELLITE_ASSEMBLY_MARKER, StringComparison.OrdinalIgnoreCase))
{
assemblyFileName = cleanedFileName.Remove(0, MonoAndroidHelper.MANGLED_ASSEMBLY_SATELLITE_ASSEMBLY_MARKER.Length);
// MonoAndroidHelper.SATELLITE_CULTURE_END_MARKER_CHAR is incorrect!
int cultureSepIndex = assemblyFileName.IndexOf('-');
if (cultureSepIndex < 1)
{
Expand Down Expand Up @@ -192,7 +195,7 @@ public bool UncompressFromAPK_IndividualElfFiles(ZipFile apk, string filePath, s
continue;
}

using (MemoryStream memoryStream = new MemoryStream())
using (RecyclableMemoryStream memoryStream = new RecyclableMemoryStream(MemoryStreamManager))
{
byte[] buffer = new byte[4096]; // 4K is optimum
using (Stream zipStream = apk.GetInputStream(entry))
Expand Down Expand Up @@ -287,7 +290,7 @@ public bool UncompressFromAPK_IndividualEntries(ZipFile apk, string filePath, st
continue;
}

using (MemoryStream memoryStream = new MemoryStream())
using (RecyclableMemoryStream memoryStream = new RecyclableMemoryStream(MemoryStreamManager))
{
byte[] buffer = new byte[4096]; // 4K is optimum
using (Stream zipStream = apk.GetInputStream(entry))
Expand Down Expand Up @@ -325,11 +328,11 @@ public bool UncompressFromAPK_AssemblyStores(string filePath, string prefix, str
assemblyName = Path.Combine(assembly.Store.Arch, assemblyName);
}

using (var stream = new MemoryStream())
using (RecyclableMemoryStream memoryStream = new RecyclableMemoryStream(MemoryStreamManager))
{
assembly.ExtractImage(stream);
stream.Seek(0, SeekOrigin.Begin);
UncompressDLL(stream, assemblyName, prefix, outputPath);
assembly.ExtractImage(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
UncompressDLL(memoryStream, assemblyName, prefix, outputPath);
}
}

Expand Down
4 changes: 3 additions & 1 deletion Tools/ApkUncompress/AssemblyStore/AssemblyStoreExplorer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using ApkUncompress;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.IO;

namespace Xamarin.Android.AssemblyStore
{
Expand Down Expand Up @@ -233,7 +235,7 @@ void ReadStoreSetFromArchive (ZipFile archive, string basePathInArchive)
continue;
}

using (var stream = new MemoryStream ()) {
using (RecyclableMemoryStream stream = new RecyclableMemoryStream(ApkUncompressCommon.MemoryStreamManager)) {
byte[] buffer = new byte[4096]; // 4K is optimum
using (Stream zipStream = archive.GetInputStream(entry))
{
Expand Down
6 changes: 4 additions & 2 deletions Tools/ApkUncompress/AssemblyStore/AssemblyStoreReader.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using ApkUncompress;
using Microsoft.IO;
using System;
using System.Buffers;
using System.Collections.Generic;
Expand All @@ -12,7 +14,7 @@ class AssemblyStoreReader
const uint ASSEMBLY_STORE_MAGIC = 0x41424158; // 'XABA', little-endian
const uint ASSEMBLY_STORE_FORMAT_VERSION = 1; // The highest format version this reader understands

MemoryStream? storeData;
RecyclableMemoryStream? storeData;

public uint Version { get; private set; }
public uint LocalEntryCount { get; private set; }
Expand All @@ -31,7 +33,7 @@ public AssemblyStoreReader (Stream store, string? arch = null, bool keepStoreInM

store.Seek (0, SeekOrigin.Begin);
if (keepStoreInMemory) {
storeData = new MemoryStream ();
storeData = new RecyclableMemoryStream(ApkUncompressCommon.MemoryStreamManager);
store.CopyTo (storeData);
storeData.Flush ();
store.Seek (0, SeekOrigin.Begin);
Expand Down

0 comments on commit df1ce71

Please sign in to comment.