some nullability fixes

This commit is contained in:
Adam Hathcock
2026-02-13 14:43:56 +00:00
parent 67bbdeab3d
commit eb5c5faa99
19 changed files with 76 additions and 102 deletions

View File

@@ -1,5 +1,3 @@
#nullable disable
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Text;
@@ -16,9 +14,9 @@ internal class CryptKey3 : ICryptKey
{
const int AES_128 = 128;
private string _password;
private readonly string _password;
public CryptKey3(string password) => _password = password ?? "";
public CryptKey3(string? password) => _password = password ?? string.Empty;
public ICryptoTransform Transformer(byte[] salt)
{

View File

@@ -1,5 +1,3 @@
#nullable disable
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Common.Rar;

View File

@@ -1,5 +1,3 @@
#nullable disable
using SharpCompress.Common.Rar;
using SharpCompress.IO;

View File

@@ -1,11 +1,9 @@
#nullable disable
namespace SharpCompress.Common.SevenZip;
internal class CCoderInfo
{
internal CMethodId _methodId;
internal byte[] _props;
internal byte[] _props = [];
internal int _numInStreams;
internal int _numOutStreams;
}

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
namespace SharpCompress.Common.SevenZip;
@@ -10,7 +8,7 @@ internal class CFileItem
public uint? Attrib { get; internal set; }
public uint? ExtendedAttrib { get; internal set; }
public uint? Crc { get; internal set; }
public string Name { get; internal set; }
public string Name { get; internal set; } = string.Empty;
public bool HasStream { get; internal set; }
public bool IsDir { get; internal set; }

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
using System.Security.Cryptography;

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
using System.IO;
@@ -15,7 +13,7 @@ public sealed class Crc32Stream : Stream
public const uint DEFAULT_POLYNOMIAL = 0xedb88320u;
public const uint DEFAULT_SEED = 0xffffffffu;
private static uint[] _defaultTable;
private static uint[]? _defaultTable;
public Crc32Stream(Stream stream)
: this(stream, DEFAULT_POLYNOMIAL, DEFAULT_SEED) { }

View File

@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;
@@ -43,7 +41,7 @@ internal sealed class LazyReadOnlyCollection<T> : ICollection<T>
#region IEnumerator Members
object IEnumerator.Current => Current;
object IEnumerator.Current => Current!;
public bool MoveNext()
{

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;
@@ -15,7 +13,7 @@ namespace SharpCompress.Readers.Ace;
internal class MultiVolumeAceReader : AceReader
{
private readonly IEnumerator<Stream> streams;
private Stream tempStream;
private Stream? tempStream;
internal MultiVolumeAceReader(IEnumerable<Stream> streams, ReaderOptions options)
: base(options) => this.streams = streams.GetEnumerator();
@@ -54,13 +52,13 @@ internal class MultiVolumeAceReader : AceReader
{
private readonly MultiVolumeAceReader reader;
private readonly IEnumerator<Stream> nextReadableStreams;
private Stream tempStream;
private Stream? tempStream;
private bool isFirst = true;
internal MultiVolumeStreamEnumerator(
MultiVolumeAceReader r,
IEnumerator<Stream> nextReadableStreams,
Stream tempStream
Stream? tempStream
)
{
reader = r;
@@ -72,7 +70,7 @@ internal class MultiVolumeAceReader : AceReader
IEnumerator IEnumerable.GetEnumerator() => this;
public FilePart Current { get; private set; }
public FilePart Current { get; private set; } = null!;
public void Dispose() { }

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;
@@ -16,7 +14,7 @@ namespace SharpCompress.Readers.Arj;
internal class MultiVolumeArjReader : ArjReader
{
private readonly IEnumerator<Stream> streams;
private Stream tempStream;
private Stream? tempStream;
internal MultiVolumeArjReader(IEnumerable<Stream> streams, ReaderOptions options)
: base(options) => this.streams = streams.GetEnumerator();
@@ -55,13 +53,13 @@ internal class MultiVolumeArjReader : ArjReader
{
private readonly MultiVolumeArjReader reader;
private readonly IEnumerator<Stream> nextReadableStreams;
private Stream tempStream;
private Stream? tempStream;
private bool isFirst = true;
internal MultiVolumeStreamEnumerator(
MultiVolumeArjReader r,
IEnumerator<Stream> nextReadableStreams,
Stream tempStream
Stream? tempStream
)
{
reader = r;
@@ -73,7 +71,7 @@ internal class MultiVolumeArjReader : ArjReader
IEnumerator IEnumerable.GetEnumerator() => this;
public FilePart Current { get; private set; }
public FilePart Current { get; private set; } = null!;
public void Dispose() { }

View File

@@ -1,5 +1,3 @@
#nullable disable
using System.Collections;
using System.Collections.Generic;
using System.IO;
@@ -26,13 +24,13 @@ internal partial class MultiVolumeRarReader : RarReader
{
private readonly MultiVolumeRarReader reader;
private readonly IEnumerator<Stream> nextReadableStreams;
private Stream tempStream;
private Stream? tempStream;
private bool isFirst = true;
internal MultiVolumeStreamAsyncEnumerator(
MultiVolumeRarReader r,
IEnumerator<Stream> nextReadableStreams,
Stream tempStream
Stream? tempStream
)
{
reader = r;
@@ -40,7 +38,7 @@ internal partial class MultiVolumeRarReader : RarReader
this.tempStream = tempStream;
}
public FilePart Current { get; private set; }
public FilePart Current { get; private set; } = null!;
public async ValueTask<bool> MoveNextAsync()
{

View File

@@ -1,5 +1,3 @@
#nullable disable
using System.Collections;
using System.Collections.Generic;
using System.IO;
@@ -14,7 +12,7 @@ namespace SharpCompress.Readers.Rar;
internal partial class MultiVolumeRarReader : RarReader
{
private readonly IEnumerator<Stream> streams;
private Stream tempStream;
private Stream? tempStream;
internal MultiVolumeRarReader(IEnumerable<Stream> streams, ReaderOptions options)
: base(options) => this.streams = streams.GetEnumerator();
@@ -55,13 +53,13 @@ internal partial class MultiVolumeRarReader : RarReader
{
private readonly MultiVolumeRarReader reader;
private readonly IEnumerator<Stream> nextReadableStreams;
private Stream tempStream;
private Stream? tempStream;
private bool isFirst = true;
internal MultiVolumeStreamEnumerator(
MultiVolumeRarReader r,
IEnumerator<Stream> nextReadableStreams,
Stream tempStream
Stream? tempStream
)
{
reader = r;
@@ -73,7 +71,7 @@ internal partial class MultiVolumeRarReader : RarReader
IEnumerator IEnumerable.GetEnumerator() => this;
public FilePart Current { get; private set; }
public FilePart Current { get; private set; } = null!;
public void Dispose() { }

View File

@@ -8,7 +8,6 @@ using SharpCompress.IO;
namespace SharpCompress.Writers;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public abstract partial class AbstractWriter(ArchiveType type, IWriterOptions writerOptions)
: IWriter,
IAsyncWriter
@@ -19,8 +18,7 @@ public abstract partial class AbstractWriter(ArchiveType type, IWriterOptions wr
protected void InitializeStream(Stream stream) => OutputStream = stream;
protected Stream OutputStream { get; private set; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
protected Stream? OutputStream { get; private set; }
public ArchiveType WriterType { get; } = type;
@@ -57,7 +55,7 @@ public abstract partial class AbstractWriter(ArchiveType type, IWriterOptions wr
{
if (isDisposing)
{
OutputStream.Dispose();
OutputStream?.Dispose();
}
}

View File

@@ -19,7 +19,7 @@ public partial class GZipWriter
{
throw new ArgumentException("Can only write a single stream to a GZip file.");
}
var stream = (GZipStream)OutputStream;
var stream = (GZipStream)OutputStream.NotNull();
stream.FileName = filename;
stream.LastModified = modificationTime;
var progressStream = WrapWithProgress(source, filename);

View File

@@ -43,7 +43,7 @@ public sealed partial class GZipWriter : AbstractWriter
if (isDisposing)
{
//dispose here to finish the GZip, GZip won't close the underlying stream
OutputStream.Dispose();
OutputStream.NotNull().Dispose();
}
base.Dispose(isDisposing);
}
@@ -63,7 +63,7 @@ public sealed partial class GZipWriter : AbstractWriter
}
var progressStream = WrapWithProgress(source, filename);
progressStream.CopyTo(OutputStream, Constants.BufferSize);
progressStream.CopyTo(OutputStream.NotNull(), Constants.BufferSize);
_wroteToStream = true;
}

View File

@@ -29,7 +29,7 @@ public partial class TarWriter
header.Name = normalizedName;
header.Size = 0;
header.EntryType = EntryType.Directory;
await header.WriteAsync(OutputStream, cancellationToken).ConfigureAwait(false);
await header.WriteAsync(OutputStream.NotNull(), cancellationToken).ConfigureAwait(false);
}
/// <summary>
@@ -67,10 +67,10 @@ public partial class TarWriter
header.LastModifiedTime = modificationTime ?? TarHeader.EPOCH;
header.Name = NormalizeFilename(filename);
header.Size = realSize;
await header.WriteAsync(OutputStream, cancellationToken).ConfigureAwait(false);
await header.WriteAsync(OutputStream.NotNull(), cancellationToken).ConfigureAwait(false);
var progressStream = WrapWithProgress(source, filename);
var written = await progressStream
.TransferToAsync(OutputStream, realSize, cancellationToken)
.TransferToAsync(OutputStream.NotNull(), realSize, cancellationToken)
.ConfigureAwait(false);
await PadTo512Async(written, cancellationToken).ConfigureAwait(false);
}
@@ -81,6 +81,7 @@ public partial class TarWriter
if (zeros > 0)
{
await OutputStream
.NotNull()
.WriteAsync(new byte[zeros], 0, zeros, cancellationToken)
.ConfigureAwait(false);
}

View File

@@ -98,7 +98,7 @@ public partial class TarWriter : AbstractWriter
header.Name = normalizedName;
header.Size = 0;
header.EntryType = EntryType.Directory;
header.Write(OutputStream);
header.Write(OutputStream.NotNull());
}
public void Write(string filename, Stream source, DateTime? modificationTime, long? size)
@@ -115,9 +115,9 @@ public partial class TarWriter : AbstractWriter
header.LastModifiedTime = modificationTime ?? TarHeader.EPOCH;
header.Name = NormalizeFilename(filename);
header.Size = realSize;
header.Write(OutputStream);
header.Write(OutputStream.NotNull());
var progressStream = WrapWithProgress(source, filename);
size = progressStream.TransferTo(OutputStream, realSize);
size = progressStream.TransferTo(OutputStream.NotNull(), realSize);
PadTo512(size.Value);
}
@@ -125,7 +125,7 @@ public partial class TarWriter : AbstractWriter
{
var zeros = unchecked((int)(((size + 511L) & ~511L) - size));
OutputStream.Write(stackalloc byte[zeros]);
OutputStream.NotNull().Write(stackalloc byte[zeros]);
}
protected override void Dispose(bool isDisposing)
@@ -134,7 +134,7 @@ public partial class TarWriter : AbstractWriter
{
if (finalizeArchiveOnClose)
{
OutputStream.Write(stackalloc byte[1024]);
OutputStream.NotNull().Write(stackalloc byte[1024]);
}
// Use IFinishable interface for generic finalization
if (OutputStream is IFinishable finishable)

View File

@@ -59,7 +59,7 @@ public partial class ZipWriter : AbstractWriter
ulong size = 0;
foreach (var entry in entries)
{
size += entry.Write(OutputStream);
size += entry.Write(OutputStream.NotNull());
}
WriteEndRecord(size);
}
@@ -208,7 +208,7 @@ public partial class ZipWriter : AbstractWriter
)
{
// We err on the side of caution until the zip specification clarifies how to support this
if (!OutputStream.CanSeek && useZip64)
if (!OutputStream.NotNull().CanSeek && useZip64)
{
throw new NotSupportedException(
"Zip64 extensions are not supported on non-seekable streams"
@@ -222,26 +222,26 @@ public partial class ZipWriter : AbstractWriter
Span<byte> intBuf = stackalloc byte[4];
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, ZipHeaderFactory.ENTRY_HEADER_BYTES);
OutputStream.Write(intBuf);
OutputStream.NotNull().Write(intBuf);
if (explicitZipCompressionInfo == ZipCompressionMethod.Deflate)
{
if (OutputStream.CanSeek && useZip64)
if (OutputStream.NotNull().CanSeek && useZip64)
{
OutputStream.Write(stackalloc byte[] { 45, 0 }); //smallest allowed version for zip64
OutputStream.NotNull().Write(stackalloc byte[] { 45, 0 }); //smallest allowed version for zip64
}
else
{
OutputStream.Write(stackalloc byte[] { 20, 0 }); //older version which is more compatible
OutputStream.NotNull().Write(stackalloc byte[] { 20, 0 }); //older version which is more compatible
}
}
else
{
OutputStream.Write(stackalloc byte[] { 63, 0 }); //version says we used PPMd or LZMA
OutputStream.NotNull().Write(stackalloc byte[] { 63, 0 }); //version says we used PPMd or LZMA
}
var flags = Equals(WriterOptions.ArchiveEncoding.GetEncoding(), Encoding.UTF8)
? HeaderFlags.Efs
: 0;
if (!OutputStream.CanSeek)
if (!OutputStream.NotNull().CanSeek)
{
flags |= HeaderFlags.UsePostDataDescriptor;
@@ -252,35 +252,35 @@ public partial class ZipWriter : AbstractWriter
}
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, (ushort)flags);
OutputStream.Write(intBuf.Slice(0, 2));
OutputStream.NotNull().Write(intBuf.Slice(0, 2));
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, (ushort)explicitZipCompressionInfo);
OutputStream.Write(intBuf.Slice(0, 2)); // zipping method
OutputStream.NotNull().Write(intBuf.Slice(0, 2)); // zipping method
BinaryPrimitives.WriteUInt32LittleEndian(
intBuf,
zipWriterEntryOptions.ModificationDateTime.DateTimeToDosTime()
);
OutputStream.Write(intBuf);
OutputStream.NotNull().Write(intBuf);
// zipping date and time
OutputStream.Write(stackalloc byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
OutputStream.NotNull().Write(stackalloc byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
// unused CRC, un/compressed size, updated later
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, (ushort)encodedFilename.Length);
OutputStream.Write(intBuf.Slice(0, 2)); // filename length
OutputStream.NotNull().Write(intBuf.Slice(0, 2)); // filename length
var extralength = 0;
if (OutputStream.CanSeek && useZip64)
if (OutputStream.NotNull().CanSeek && useZip64)
{
extralength = 2 + 2 + 8 + 8;
}
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, (ushort)extralength);
OutputStream.Write(intBuf.Slice(0, 2)); // extra length
OutputStream.Write(encodedFilename, 0, encodedFilename.Length);
OutputStream.NotNull().Write(intBuf.Slice(0, 2)); // extra length
OutputStream.NotNull().Write(encodedFilename, 0, encodedFilename.Length);
if (extralength != 0)
{
OutputStream.Write(new byte[extralength], 0, extralength); // reserve space for zip64 data
OutputStream.NotNull().Write(new byte[extralength], 0, extralength); // reserve space for zip64 data
entry.Zip64HeaderOffset = (ushort)(6 + 2 + 2 + 4 + 12 + 2 + 2 + encodedFilename.Length);
}
@@ -291,11 +291,11 @@ public partial class ZipWriter : AbstractWriter
{
Span<byte> intBuf = stackalloc byte[4];
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, crc);
OutputStream.Write(intBuf);
OutputStream.NotNull().Write(intBuf);
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, compressed);
OutputStream.Write(intBuf);
OutputStream.NotNull().Write(intBuf);
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, uncompressed);
OutputStream.Write(intBuf);
OutputStream.NotNull().Write(intBuf);
}
private void WriteEndRecord(ulong size)
@@ -315,57 +315,57 @@ public partial class ZipWriter : AbstractWriter
var recordlen = 2 + 2 + 4 + 4 + 8 + 8 + 8 + 8;
// Write zip64 end of central directory record
OutputStream.Write(stackalloc byte[] { 80, 75, 6, 6 });
OutputStream.NotNull().Write(stackalloc byte[] { 80, 75, 6, 6 });
BinaryPrimitives.WriteUInt64LittleEndian(intBuf, (ulong)recordlen);
OutputStream.Write(intBuf); // Size of zip64 end of central directory record
OutputStream.NotNull().Write(intBuf); // Size of zip64 end of central directory record
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, 45);
OutputStream.Write(intBuf.Slice(0, 2)); // Made by
OutputStream.NotNull().Write(intBuf.Slice(0, 2)); // Made by
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, 45);
OutputStream.Write(intBuf.Slice(0, 2)); // Version needed
OutputStream.NotNull().Write(intBuf.Slice(0, 2)); // Version needed
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, 0);
OutputStream.Write(intBuf.Slice(0, 4)); // Disk number
OutputStream.Write(intBuf.Slice(0, 4)); // Central dir disk
OutputStream.NotNull().Write(intBuf.Slice(0, 4)); // Disk number
OutputStream.NotNull().Write(intBuf.Slice(0, 4)); // Central dir disk
// TODO: entries.Count is int, so max 2^31 files
BinaryPrimitives.WriteUInt64LittleEndian(intBuf, (ulong)entries.Count);
OutputStream.Write(intBuf); // Entries in this disk
OutputStream.Write(intBuf); // Total entries
OutputStream.NotNull().Write(intBuf); // Entries in this disk
OutputStream.NotNull().Write(intBuf); // Total entries
BinaryPrimitives.WriteUInt64LittleEndian(intBuf, size);
OutputStream.Write(intBuf); // Central Directory size
OutputStream.NotNull().Write(intBuf); // Central Directory size
BinaryPrimitives.WriteUInt64LittleEndian(intBuf, (ulong)streamPosition);
OutputStream.Write(intBuf); // Disk offset
OutputStream.NotNull().Write(intBuf); // Disk offset
// Write zip64 end of central directory locator
OutputStream.Write(stackalloc byte[] { 80, 75, 6, 7 });
OutputStream.NotNull().Write(stackalloc byte[] { 80, 75, 6, 7 });
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, 0);
OutputStream.Write(intBuf.Slice(0, 4)); // Entry disk
OutputStream.NotNull().Write(intBuf.Slice(0, 4)); // Entry disk
BinaryPrimitives.WriteUInt64LittleEndian(intBuf, (ulong)streamPosition + size);
OutputStream.Write(intBuf); // Offset to the zip64 central directory
OutputStream.NotNull().Write(intBuf); // Offset to the zip64 central directory
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, 1);
OutputStream.Write(intBuf.Slice(0, 4)); // Number of disks
OutputStream.NotNull().Write(intBuf.Slice(0, 4)); // Number of disks
streamPosition += 4 + 8 + recordlen + (4 + 4 + 8 + 4);
}
// Write normal end of central directory record
OutputStream.Write(stackalloc byte[] { 80, 75, 5, 6, 0, 0, 0, 0 });
OutputStream.NotNull().Write(stackalloc byte[] { 80, 75, 5, 6, 0, 0, 0, 0 });
BinaryPrimitives.WriteUInt16LittleEndian(
intBuf,
(ushort)(entries.Count < 0xFFFF ? entries.Count : 0xFFFF)
);
OutputStream.Write(intBuf.Slice(0, 2));
OutputStream.Write(intBuf.Slice(0, 2));
OutputStream.NotNull().Write(intBuf.Slice(0, 2));
OutputStream.NotNull().Write(intBuf.Slice(0, 2));
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, sizevalue);
OutputStream.Write(intBuf.Slice(0, 4));
OutputStream.NotNull().Write(intBuf.Slice(0, 4));
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, streampositionvalue);
OutputStream.Write(intBuf.Slice(0, 4));
OutputStream.NotNull().Write(intBuf.Slice(0, 4));
var encodedComment = WriterOptions.ArchiveEncoding.Encode(zipComment);
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, (ushort)encodedComment.Length);
OutputStream.Write(intBuf.Slice(0, 2));
OutputStream.Write(encodedComment, 0, encodedComment.Length);
OutputStream.NotNull().Write(intBuf.Slice(0, 2));
OutputStream.NotNull().Write(encodedComment, 0, encodedComment.Length);
}
#region Nested type: ZipWritingStream