mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-11-15 17:02:03 +00:00
Use pooled memory streams for temporary production buffers
This commit is contained in:
@@ -25,7 +25,7 @@ public partial class ArcLzwStream
|
||||
if (!_processed)
|
||||
{
|
||||
using var limited = new ReadOnlySubStream(_stream, _compressedSize);
|
||||
using var data = new MemoryStream();
|
||||
using var data = new PooledMemoryStream();
|
||||
await limited.CopyToAsync(data, 81920, cancellationToken).ConfigureAwait(false);
|
||||
FinishDecompression(data);
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ public partial class ArcLzwStream : Stream
|
||||
if (!_processed)
|
||||
{
|
||||
using var limited = new ReadOnlySubStream(_stream, _compressedSize);
|
||||
using var data = new MemoryStream();
|
||||
using var data = new PooledMemoryStream();
|
||||
limited.CopyTo(data);
|
||||
FinishDecompression(data);
|
||||
}
|
||||
@@ -201,7 +201,7 @@ public partial class ArcLzwStream : Stream
|
||||
return result;
|
||||
}
|
||||
|
||||
private void FinishDecompression(MemoryStream data)
|
||||
private void FinishDecompression(PooledMemoryStream data)
|
||||
{
|
||||
if (data.Length != _compressedSize)
|
||||
{
|
||||
|
||||
@@ -202,7 +202,8 @@ public class PpmdStream : Stream, IAsyncDisposable
|
||||
_isDisposed = true;
|
||||
if (_compress)
|
||||
{
|
||||
await _model.EncodeBlockAsync(_stream, new MemoryStream(), true).ConfigureAwait(false);
|
||||
using var input = new PooledMemoryStream();
|
||||
await _model.EncodeBlockAsync(_stream, input, true).ConfigureAwait(false);
|
||||
}
|
||||
_modelH?.Dispose();
|
||||
_modelH = null;
|
||||
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.IO;
|
||||
|
||||
namespace SharpCompress.Compressors.Shrink;
|
||||
|
||||
@@ -28,7 +29,7 @@ internal partial class ShrinkStream : Stream
|
||||
// Read actual compressed data from the stream rather than pre-allocating based on the
|
||||
// declared compressed size, which may be crafted to cause an OutOfMemoryException.
|
||||
// The stream is already bounded by ReadOnlySubStream in ZipFilePart.
|
||||
using var srcMs = new MemoryStream();
|
||||
using var srcMs = new PooledMemoryStream();
|
||||
await _inStream.CopyToAsync(srcMs, 81920, cancellationToken).ConfigureAwait(false);
|
||||
var src = srcMs.ToArray();
|
||||
// Decompress synchronously (CPU-bound operation).
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.IO;
|
||||
|
||||
namespace SharpCompress.Compressors.Shrink;
|
||||
|
||||
@@ -52,7 +53,7 @@ internal partial class ShrinkStream : Stream
|
||||
// Read actual compressed data from the stream rather than pre-allocating based on the
|
||||
// declared compressed size, which may be crafted to cause an OutOfMemoryException.
|
||||
// The stream is already bounded by ReadOnlySubStream in ZipFilePart.
|
||||
using var srcMs = new MemoryStream();
|
||||
using var srcMs = new PooledMemoryStream();
|
||||
_inStream.CopyTo(srcMs);
|
||||
var src = srcMs.ToArray();
|
||||
Decode(src);
|
||||
|
||||
@@ -53,7 +53,7 @@ public partial class SevenZipWriter
|
||||
var filesInfo = new SevenZipFilesInfoWriter { Entries = entries.ToArray() };
|
||||
|
||||
// Write header to a temporary stream first
|
||||
using var headerStream = new MemoryStream();
|
||||
using var headerStream = new PooledMemoryStream();
|
||||
ArchiveHeaderWriter.WriteRawHeader(headerStream, mainStreamsInfo, filesInfo);
|
||||
|
||||
// Optionally compress the header
|
||||
@@ -68,7 +68,7 @@ public partial class SevenZipWriter
|
||||
}
|
||||
|
||||
private async ValueTask WriteCompressedHeaderAsync(
|
||||
MemoryStream rawHeaderStream,
|
||||
PooledMemoryStream rawHeaderStream,
|
||||
long endOfPackedData
|
||||
)
|
||||
{
|
||||
@@ -95,7 +95,7 @@ public partial class SevenZipWriter
|
||||
};
|
||||
|
||||
// Write encoded header to a second temporary stream
|
||||
using var encodedHeaderStream = new MemoryStream();
|
||||
using var encodedHeaderStream = new PooledMemoryStream();
|
||||
ArchiveHeaderWriter.WriteEncodedHeader(encodedHeaderStream, headerStreamsInfo);
|
||||
|
||||
// Write the encoded header to the output
|
||||
@@ -123,7 +123,7 @@ public partial class SevenZipWriter
|
||||
}
|
||||
|
||||
private async ValueTask WriteRawHeaderToOutputAsync(
|
||||
MemoryStream rawHeaderStream,
|
||||
PooledMemoryStream rawHeaderStream,
|
||||
long endOfPackedData
|
||||
)
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.Zip;
|
||||
using SharpCompress.Common.Zip.Headers;
|
||||
using SharpCompress.IO;
|
||||
|
||||
namespace SharpCompress.Writers.Zip;
|
||||
|
||||
@@ -24,7 +25,7 @@ public partial class ZipWriter
|
||||
|
||||
// Buffer the entire central directory + end record into memory, then write async.
|
||||
// This avoids synchronous writes to the underlying stream during finalization.
|
||||
using var ms = new MemoryStream();
|
||||
using var ms = new PooledMemoryStream();
|
||||
ulong size = 0;
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
@@ -149,9 +150,9 @@ public partial class ZipWriter
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
// Build the header synchronously into a MemoryStream, then async-copy to OutputStream.
|
||||
// Build the header synchronously into a pooled stream, then async-copy to OutputStream.
|
||||
// This avoids any synchronous writes to the potentially async-only output stream.
|
||||
using var ms = new MemoryStream();
|
||||
using var ms = new PooledMemoryStream();
|
||||
var outputStream = OutputStream.NotNull();
|
||||
var result = WriteHeader(
|
||||
ms,
|
||||
|
||||
Reference in New Issue
Block a user