Minor fixes

This commit is contained in:
Adam Hathcock
2021-02-08 18:25:14 +00:00
parent d6fe729068
commit db02e8b634
5 changed files with 39 additions and 37 deletions

View File

@@ -32,7 +32,7 @@ namespace SharpCompress.Common.GZip
{
long position = stream.Position;
stream.Position = stream.Length - 8;
ReadTrailer();
await ReadTrailerAsync(cancellationToken);
stream.Position = position;
}
EntryStartPosition = stream.Position;
@@ -56,14 +56,12 @@ namespace SharpCompress.Common.GZip
return _stream;
}
private void ReadTrailer()
private async ValueTask ReadTrailerAsync(CancellationToken cancellationToken)
{
// Read and potentially verify the GZIP trailer: CRC32 and size mod 2^32
Span<byte> trailer = stackalloc byte[8];
int n = _stream.Read(trailer);
Crc = BinaryPrimitives.ReadInt32LittleEndian(trailer);
UncompressedSize = BinaryPrimitives.ReadInt32LittleEndian(trailer.Slice(4));
Crc = await _stream.ReadInt32(cancellationToken);
UncompressedSize = await _stream.ReadInt32(cancellationToken);
}
private async ValueTask ReadAndValidateGzipHeaderAsync(CancellationToken cancellationToken)

View File

@@ -199,21 +199,22 @@ namespace SharpCompress.Compressors.Deflate
/// </remarks>
protected override void Dispose(bool disposing)
{
try
if (disposing)
{
if (!_disposed)
{
if (disposing && (_baseStream != null))
{
_baseStream.Dispose();
Crc32 = _baseStream.Crc32;
}
_disposed = true;
}
throw new NotImplementedException();
}
finally
}
public override async ValueTask DisposeAsync()
{
if (!_disposed)
{
base.Dispose(disposing);
if ((_baseStream != null))
{
await _baseStream.DisposeAsync();
Crc32 = _baseStream.Crc32;
}
_disposed = true;
}
}

View File

@@ -87,7 +87,7 @@ namespace SharpCompress.IO
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
Stream.Write(buffer, offset, count);
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

View File

@@ -25,6 +25,10 @@ namespace SharpCompress
{
return stream.ReadPrimitive(4, x => BinaryPrimitives.ReadUInt32LittleEndian(x.Span), cancellationToken);
}
public static ValueTask<int> ReadInt32(this Stream stream, CancellationToken cancellationToken)
{
return stream.ReadPrimitive(4, x => BinaryPrimitives.ReadInt32LittleEndian(x.Span), cancellationToken);
}
public static ValueTask<ulong> ReadUInt64(this Stream stream, CancellationToken cancellationToken)
{
@@ -317,6 +321,11 @@ namespace SharpCompress
return total;
}
public static async ValueTask WriteByte(this Stream stream, byte b, CancellationToken cancellationToken = default)
{
await stream.WriteAsync(new ReadOnlyMemory<byte>(new[] {b}), cancellationToken);
}
public static long TransferTo(this Stream source, Stream destination)
{
byte[] array = GetTransferByteArray();
@@ -339,25 +348,19 @@ namespace SharpCompress
public static async ValueTask<long> TransferToAsync(this Stream source, Stream destination, Common.Entry entry, IReaderExtractionListener readerExtractionListener, CancellationToken cancellationToken)
{
byte[] array = GetTransferByteArray();
try
using var buffer = MemoryPool<byte>.Shared.Rent(81920);
var iterations = 0;
long total = 0;
var count = 0;
var slice = buffer.Memory.Slice(0, 81920);
while ((count = await source.ReadAsync(slice, cancellationToken)) != 0)
{
var iterations = 0;
long total = 0;
var count = 0;
while ((count = await source.ReadAsync(array, 0, array.Length, cancellationToken)) != 0)
{
total += count;
await destination.WriteAsync(array, 0, count, cancellationToken);
iterations++;
readerExtractionListener.FireEntryExtractionProgress(entry, total, iterations);
}
return total;
}
finally
{
ArrayPool<byte>.Shared.Return(array);
total += count;
await destination.WriteAsync(slice.Slice(0, count), cancellationToken);
iterations++;
readerExtractionListener.FireEntryExtractionProgress(entry, total, iterations);
}
return total;
}
private static byte[] GetTransferByteArray()

View File

@@ -44,7 +44,7 @@ namespace SharpCompress.Test
await using (var reader = await ReaderFactory.OpenAsync(new NonDisposingStream(stream), readerOptions))
{
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH, new ExtractionOptions()
await reader.WriteAllToDirectoryAsync(SCRATCH_FILES_PATH, new ExtractionOptions()
{
ExtractFullPath = true
});