Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d52d836
Avoid unnecessary byte[] allocations
stephentoub Aug 19, 2022
1e5020e
Remove unnecessary use of FileStreamOptions
stephentoub Aug 19, 2022
6938c77
Clean up Dispose{Async} implementations
stephentoub Aug 19, 2022
5fa03e9
Clean up unnecessary consts
stephentoub Aug 19, 2022
8dd0ac1
Remove MemoryStream/Encoding.UTF8.GetBytes allocations, unnecessary a…
stephentoub Aug 19, 2022
5be57ad
Avoid string allocations in ReadMagicAttribute
stephentoub Aug 19, 2022
ab71e6c
Avoid allocation in WriteAsOctal
stephentoub Aug 19, 2022
df2d742
Improve handling of octal
stephentoub Aug 19, 2022
c6058bd
Avoid allocation for version string
stephentoub Aug 19, 2022
5756a8c
Removing boxing and char string allocation in GenerateExtendedAttribu…
stephentoub Aug 19, 2022
9539a4a
Fix a couple unnecessary dictionary lookups
stephentoub Aug 19, 2022
74bbc9c
Replace Enum.HasFlag usage
stephentoub Aug 19, 2022
46e0855
Remove allocations from Write{Posix}Name
stephentoub Aug 19, 2022
02ca7da
Replace ArrayPool use with string.Create
stephentoub Aug 19, 2022
f9eb99f
Replace more superfluous ArrayPool usage
stephentoub Aug 19, 2022
add6179
Remove ArrayPool use from System.IO.Compression.ZipFile
stephentoub Aug 20, 2022
6f8cb75
Fix inverted condition
stephentoub Aug 20, 2022
827a588
Use generic math to parse octal
stephentoub Aug 20, 2022
ae21478
Remove allocations from StringReader and string.Split
stephentoub Aug 20, 2022
d6b6727
Remove magic string allocation for Ustar when not V7
stephentoub Aug 20, 2022
480af5c
Remove file name and directory name allocation in GenerateExtendedAtt…
stephentoub Aug 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean up Dispose{Async} implementations
  • Loading branch information
stephentoub committed Aug 19, 2022
commit 6938c772e5cf28c500ae2809e312b1ce01bd9e64
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ public override Task FlushAsync(CancellationToken cancellationToken) =>
// the substream is just 'a chunk' of the super-stream
protected override void Dispose(bool disposing)
{
if (disposing && !_isDisposed)
{
_isDisposed = true;
}
_isDisposed = true;
base.Dispose(disposing);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,18 @@ public TarReader(Stream archiveStream, bool leaveOpen = false)
/// <remarks>The <see cref="TarEntry.DataStream"/> property of any entry can be replaced with a new stream. If the user decides to replace it on a <see cref="TarEntry"/> instance that was obtained using a <see cref="TarReader"/>, the underlying stream gets disposed immediately, freeing the <see cref="TarReader"/> of origin from the responsibility of having to dispose it.</remarks>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
if (!_isDisposed)
{
_isDisposed = true;

if (!_leaveOpen && _dataStreamsToDispose?.Count > 0)
{
foreach (Stream s in _dataStreamsToDispose)
{
s.Dispose();
}
}
}
}

/// <summary>
Expand All @@ -64,8 +74,18 @@ public void Dispose()
/// <remarks>The <see cref="TarEntry.DataStream"/> property of any entry can be replaced with a new stream. If the user decides to replace it on a <see cref="TarEntry"/> instance that was obtained using a <see cref="TarReader"/>, the underlying stream gets disposed immediately, freeing the <see cref="TarReader"/> of origin from the responsibility of having to dispose it.</remarks>
public async ValueTask DisposeAsync()
{
await DisposeAsync(disposing: true).ConfigureAwait(false);
GC.SuppressFinalize(this);
if (!_isDisposed)
{
_isDisposed = true;

if (!_leaveOpen && _dataStreamsToDispose?.Count > 0)
{
foreach (Stream s in _dataStreamsToDispose)
{
await s.DisposeAsync().ConfigureAwait(false);
}
}
}
}

/// <summary>
Expand Down Expand Up @@ -249,52 +269,6 @@ internal async ValueTask AdvanceDataStreamIfNeededAsync(CancellationToken cancel
}
}

// Disposes the current instance.
// If 'disposing' is 'false', the method was called from the finalizer.
private void Dispose(bool disposing)
{
if (disposing && !_isDisposed)
{
try
{
if (!_leaveOpen && _dataStreamsToDispose?.Count > 0)
{
foreach (Stream s in _dataStreamsToDispose)
{
s.Dispose();
}
}
}
finally
{
_isDisposed = true;
}
}
}

// Asynchronously disposes the current instance.
// If 'disposing' is 'false', the method was called from the finalizer.
private async ValueTask DisposeAsync(bool disposing)
{
if (disposing && !_isDisposed)
{
try
{
if (!_leaveOpen && _dataStreamsToDispose?.Count > 0)
{
foreach (Stream s in _dataStreamsToDispose)
{
await s.DisposeAsync().ConfigureAwait(false);
}
}
}
finally
{
_isDisposed = true;
}
}
}

// Asynchronously retrieves the next entry if one is found.
private async ValueTask<TarEntry?> GetNextEntryInternalAsync(bool copyData, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,41 @@ public TarWriter(Stream archiveStream, TarEntryFormat format = TarEntryFormat.Pa
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
if (!_isDisposed)
{
_isDisposed = true;

if (_wroteEntries)
{
WriteFinalRecords();
}

if (!_leaveOpen)
{
_archiveStream.Dispose();
}
}
}

/// <summary>
/// Asynchronously disposes the current <see cref="TarWriter"/> instance, and closes the archive stream if the <c>leaveOpen</c> argument was set to <see langword="false"/> in the constructor.
/// </summary>
public async ValueTask DisposeAsync()
{
await DisposeAsync(disposing: true).ConfigureAwait(false);
GC.SuppressFinalize(this);
if (!_isDisposed)
{
_isDisposed = true;

if (_wroteEntries)
{
await WriteFinalRecordsAsync().ConfigureAwait(false);
}

if (!_leaveOpen)
{
await _archiveStream.DisposeAsync().ConfigureAwait(false);
}
}
}

/// <summary>
Expand Down Expand Up @@ -241,58 +265,6 @@ public Task WriteEntryAsync(TarEntry entry, CancellationToken cancellationToken
return WriteEntryAsyncInternal(entry, cancellationToken);
}

// Disposes the current instance.
// If 'disposing' is 'false', the method was called from the finalizer.
private void Dispose(bool disposing)
{
if (disposing && !_isDisposed)
{
try
{
if (_wroteEntries)
{
WriteFinalRecords();
}


if (!_leaveOpen)
{
_archiveStream.Dispose();
}
}
finally
{
_isDisposed = true;
}
}
}

// Asynchronously disposes the current instance.
// If 'disposing' is 'false', the method was called from the finalizer.
private async ValueTask DisposeAsync(bool disposing)
{
if (disposing && !_isDisposed)
{
try
{
if (_wroteEntries)
{
await WriteFinalRecordsAsync().ConfigureAwait(false);
}


if (!_leaveOpen)
{
await _archiveStream.DisposeAsync().ConfigureAwait(false);
}
}
finally
{
_isDisposed = true;
}
}
}

// Portion of the WriteEntry(entry) method that rents a buffer and writes to the archive.
private void WriteEntryInternal(TarEntry entry)
{
Expand Down