Skip to content

Commit

Permalink
Annotate System.IO.Compression for nullable (dotnet/corefx#41322)
Browse files Browse the repository at this point in the history
Nullable annotation for System.IO.Compression

Commit migrated from dotnet/corefx@967eb3c
  • Loading branch information
buyaa-n authored Sep 27, 2019
1 parent f322904 commit 69feffc
Show file tree
Hide file tree
Showing 20 changed files with 253 additions and 296 deletions.
4 changes: 3 additions & 1 deletion src/libraries/Common/src/System/IO/PathInternal.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Runtime.CompilerServices;

namespace System.IO
Expand Down Expand Up @@ -73,7 +74,8 @@ private static bool EndsWithPeriodOrSpace(string path)
/// away from paths during normalization, but if we see such a path at this point it should be
/// normalized and has retained the final characters. (Typically from one of the *Info classes)
/// </summary>
internal static string EnsureExtendedPrefixIfNeeded(string path)
/// TODO: add atribute [return: NotNullIfNotNull("path")]
internal static string? EnsureExtendedPrefixIfNeeded(string? path)
{
if (path != null && (path.Length >= MaxShortPath || EndsWithPeriodOrSpace(path)))
{
Expand Down
12 changes: 6 additions & 6 deletions src/libraries/System.IO.Compression/ref/System.IO.Compression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public DeflateStream(System.IO.Stream stream, System.IO.Compression.CompressionM
public override bool CanWrite { get { throw null; } }
public override long Length { get { throw null; } }
public override long Position { get { throw null; } set { } }
public override System.IAsyncResult BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) { throw null; }
public override System.IAsyncResult BeginWrite(byte[] array, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) { throw null; }
public override System.IAsyncResult BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback? asyncCallback, object? asyncState) { throw null; }
public override System.IAsyncResult BeginWrite(byte[] array, int offset, int count, System.AsyncCallback? asyncCallback, object? asyncState) { throw null; }
public override void CopyTo(System.IO.Stream destination, int bufferSize) { }
public override System.Threading.Tasks.Task CopyToAsync(System.IO.Stream destination, int bufferSize, System.Threading.CancellationToken cancellationToken) { throw null; }
protected override void Dispose(bool disposing) { }
Expand Down Expand Up @@ -64,8 +64,8 @@ public GZipStream(System.IO.Stream stream, System.IO.Compression.CompressionMode
public override bool CanWrite { get { throw null; } }
public override long Length { get { throw null; } }
public override long Position { get { throw null; } set { } }
public override System.IAsyncResult BeginRead(byte[] array, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) { throw null; }
public override System.IAsyncResult BeginWrite(byte[] array, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) { throw null; }
public override System.IAsyncResult BeginRead(byte[] array, int offset, int count, System.AsyncCallback? asyncCallback, object? asyncState) { throw null; }
public override System.IAsyncResult BeginWrite(byte[] array, int offset, int count, System.AsyncCallback? asyncCallback, object? asyncState) { throw null; }
public override void CopyTo(System.IO.Stream destination, int bufferSize) { }
public override System.Threading.Tasks.Task CopyToAsync(System.IO.Stream destination, int bufferSize, System.Threading.CancellationToken cancellationToken) { throw null; }
protected override void Dispose(bool disposing) { }
Expand All @@ -91,14 +91,14 @@ public partial class ZipArchive : System.IDisposable
public ZipArchive(System.IO.Stream stream) { }
public ZipArchive(System.IO.Stream stream, System.IO.Compression.ZipArchiveMode mode) { }
public ZipArchive(System.IO.Stream stream, System.IO.Compression.ZipArchiveMode mode, bool leaveOpen) { }
public ZipArchive(System.IO.Stream stream, System.IO.Compression.ZipArchiveMode mode, bool leaveOpen, System.Text.Encoding entryNameEncoding) { }
public ZipArchive(System.IO.Stream stream, System.IO.Compression.ZipArchiveMode mode, bool leaveOpen, System.Text.Encoding? entryNameEncoding) { }
public System.Collections.ObjectModel.ReadOnlyCollection<System.IO.Compression.ZipArchiveEntry> Entries { get { throw null; } }
public System.IO.Compression.ZipArchiveMode Mode { get { throw null; } }
public System.IO.Compression.ZipArchiveEntry CreateEntry(string entryName) { throw null; }
public System.IO.Compression.ZipArchiveEntry CreateEntry(string entryName, System.IO.Compression.CompressionLevel compressionLevel) { throw null; }
public void Dispose() { }
protected virtual void Dispose(bool disposing) { }
public System.IO.Compression.ZipArchiveEntry GetEntry(string entryName) { throw null; }
public System.IO.Compression.ZipArchiveEntry? GetEntry(string entryName) { throw null; }
}
public partial class ZipArchiveEntry
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configurations>netcoreapp-Debug;netcoreapp-Release;uap-Debug;uap-Release</Configurations>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.IO.Compression.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Library</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Configurations>netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release</Configurations>
<Nullable>enable</Nullable>
</PropertyGroup>
<!-- Default configurations to help VS understand the options -->
<ItemGroup>
Expand All @@ -16,7 +17,6 @@
<Compile Include="$(SharedOpenSourcePath)System\IO\Compression\ZipHelper.cs" />
<Compile Include="$(SharedOpenSourcePath)System\IO\Compression\ZipVersion.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\BlockType.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\DeflateInput.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\DeflateManagedStream.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\FileFormats.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\HuffmanTree.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,27 @@ internal sealed partial class DeflateManagedStream : Stream
{
internal const int DefaultBufferSize = 8192;

private Stream _stream;
private bool _leaveOpen;
private Stream? _stream;
private InflaterManaged _inflater;
private byte[] _buffer;
private readonly byte[] _buffer;

private int _asyncOperations;

// A specific constructor to allow decompression of Deflate64
internal DeflateManagedStream(Stream stream, ZipArchiveEntry.CompressionMethodValues method, long uncompressedSize)
internal DeflateManagedStream(Stream stream, ZipArchiveEntry.CompressionMethodValues method, long uncompressedSize = -1)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
if (!stream.CanRead)
throw new ArgumentException(SR.NotSupported_UnreadableStream, nameof(stream));

InitializeInflater(stream, false, null, method, uncompressedSize);
}

/// <summary>
/// Sets up this DeflateManagedStream to be used for Inflation/Decompression
/// </summary>
internal void InitializeInflater(Stream stream, bool leaveOpen, IFileFormatReader reader = null, ZipArchiveEntry.CompressionMethodValues method = ZipArchiveEntry.CompressionMethodValues.Deflate, long uncompressedSize = -1)
{
Debug.Assert(stream != null);
Debug.Assert(method == ZipArchiveEntry.CompressionMethodValues.Deflate64);
if (!stream.CanRead)
throw new ArgumentException(SR.NotSupported_UnreadableStream, nameof(stream));

_inflater = new InflaterManaged(reader, method == ZipArchiveEntry.CompressionMethodValues.Deflate64 ? true : false, uncompressedSize);
Debug.Assert(method == ZipArchiveEntry.CompressionMethodValues.Deflate64);

_inflater = new InflaterManaged(null, method == ZipArchiveEntry.CompressionMethodValues.Deflate64 ? true : false, uncompressedSize);

_stream = stream;
_leaveOpen = leaveOpen;
_buffer = new byte[DefaultBufferSize];
}

Expand Down Expand Up @@ -133,7 +122,7 @@ public override int Read(byte[] array, int offset, int count)
break;
}

int bytes = _stream.Read(_buffer, 0, _buffer.Length);
int bytes = _stream!.Read(_buffer, 0, _buffer.Length);
if (bytes <= 0)
{
break;
Expand Down Expand Up @@ -177,7 +166,7 @@ private static void ThrowStreamClosedException()
throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamClosed);
}

public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) =>
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);

public override int EndRead(IAsyncResult asyncResult) =>
Expand All @@ -198,7 +187,7 @@ public override Task<int> ReadAsync(byte[] array, int offset, int count, Cancell
}

Interlocked.Increment(ref _asyncOperations);
Task<int> readTask = null;
Task<int>? readTask = null;

try
{
Expand All @@ -218,7 +207,7 @@ public override Task<int> ReadAsync(byte[] array, int offset, int count, Cancell

// If there is no data on the output buffer and we are not at
// the end of the stream, we need to get more data from the base stream
readTask = _stream.ReadAsync(_buffer, 0, _buffer.Length, cancellationToken);
readTask = _stream!.ReadAsync(_buffer, 0, _buffer.Length, cancellationToken);
if (readTask == null)
{
throw new InvalidOperationException(SR.NotSupported_UnreadableStream);
Expand Down Expand Up @@ -267,7 +256,7 @@ private async Task<int> ReadAsyncCore(Task<int> readTask, byte[] array, int offs
{
// We could have read in head information and didn't get any data.
// Read from the base stream again.
readTask = _stream.ReadAsync(_buffer, 0, _buffer.Length, cancellationToken);
readTask = _stream!.ReadAsync(_buffer, 0, _buffer.Length, cancellationToken);
if (readTask == null)
{
throw new InvalidOperationException(SR.NotSupported_UnreadableStream);
Expand Down Expand Up @@ -315,20 +304,20 @@ protected override void Dispose(bool disposing)
// In this case, we still need to clean up internal resources, hence the inner finally blocks.
try
{
if (disposing && !_leaveOpen && _stream != null)
if (disposing && _stream != null)
_stream.Dispose();
}
finally
{
_stream = null;
_stream = null!;

try
{
_inflater?.Dispose();
}
finally
{
_inflater = null;
_inflater = null!;
base.Dispose(disposing);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal sealed class HuffmanTree
private readonly short[] _right;
private readonly byte[] _codeLengthArray;
#if DEBUG
private uint[] _codeArrayDebug;
private uint[]? _codeArrayDebug;
#endif

private readonly int _tableMask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ internal sealed class InflaterManaged

private readonly OutputWindow _output;
private readonly InputBuffer _input;
private HuffmanTree _literalLengthTree;
private HuffmanTree _distanceTree;
private HuffmanTree? _literalLengthTree;
private HuffmanTree? _distanceTree;

private InflaterState _state;
private readonly bool _hasFormatReader;
Expand All @@ -72,13 +72,13 @@ internal sealed class InflaterManaged
private readonly byte[] _codeList; // temporary array to store the code length for literal/Length and distance
private readonly byte[] _codeLengthTreeCodeLength;
private readonly bool _deflate64;
private HuffmanTree _codeLengthTree;
private HuffmanTree? _codeLengthTree;
private readonly long _uncompressedSize;
private long _currentInflatedCount;

private readonly IFileFormatReader _formatReader; // class to decode header and footer (e.g. gzip)
private readonly IFileFormatReader? _formatReader; // class to decode header and footer (e.g. gzip)

internal InflaterManaged(IFileFormatReader reader, bool deflate64, long uncompressedSize)
internal InflaterManaged(IFileFormatReader? reader, bool deflate64, long uncompressedSize)
{
_output = new OutputWindow();
_input = new InputBuffer();
Expand Down Expand Up @@ -140,6 +140,7 @@ public int Inflate(byte[] bytes, int offset, int length)
{
if (_hasFormatReader)
{
Debug.Assert(_formatReader != null);
_formatReader.UpdateWithBytesRead(bytes, offset, copied);
}

Expand All @@ -161,6 +162,7 @@ public int Inflate(byte[] bytes, int offset, int length)
// But some data in output window might not be copied out.
if (_output.AvailableBytes == 0)
{
Debug.Assert(_formatReader != null);
_formatReader.Validate();
}
}
Expand Down Expand Up @@ -201,6 +203,7 @@ private bool Decode()

if (_hasFormatReader)
{
Debug.Assert(_formatReader != null);
if (_state == InflaterState.ReadingHeader)
{
if (!_formatReader.ReadHeader(_input))
Expand Down Expand Up @@ -398,6 +401,7 @@ private bool DecodeBlock(out bool end_of_block_code_seen)
case InflaterState.DecodeTop:
// decode an element from the literal tree

Debug.Assert(_literalLengthTree != null);
// TODO: optimize this!!!
symbol = _literalLengthTree.GetNextSymbol(_input);
if (symbol < 0)
Expand Down Expand Up @@ -471,6 +475,7 @@ private bool DecodeBlock(out bool end_of_block_code_seen)
case InflaterState.HaveFullLength:
if (_blockType == BlockType.Dynamic)
{
Debug.Assert(_distanceTree != null);
_distanceCode = _distanceTree.GetNextSymbol(_input);
}
else
Expand Down Expand Up @@ -615,6 +620,7 @@ private bool DecodeDynamicBlockHeader()
{
if (_state == InflaterState.ReadingTreeCodesBefore)
{
Debug.Assert(_codeLengthTree != null);
if ((_lengthCode = _codeLengthTree.GetNextSymbol(_input)) < 0)
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace System.IO.Compression

internal sealed class InputBuffer
{
private byte[] _buffer; // byte array to store input
private byte[]? _buffer; // byte array to store input
private int _start; // start poisition of the buffer
private int _end; // end position of the buffer
private uint _bitBuffer = 0; // store the bits here, we can quickly shift in this buffer
Expand All @@ -44,6 +44,7 @@ public bool EnsureBitsAvailable(int count)
{
return false;
}
Debug.Assert(_buffer != null);
// insert a byte to bitbuffer
_bitBuffer |= (uint)_buffer[_start++] << _bitsInBuffer;
_bitsInBuffer += 8;
Expand Down Expand Up @@ -72,6 +73,7 @@ public bool EnsureBitsAvailable(int count)
/// </summary>
public uint TryLoad16Bits()
{
Debug.Assert(_buffer != null);
if (_bitsInBuffer < 8)
{
if (_start < _end)
Expand Down Expand Up @@ -152,6 +154,7 @@ public int CopyTo(byte[] output, int offset, int length)
length = avail;
}

Debug.Assert(_buffer != null);
Array.Copy(_buffer, _start, output, offset, length);
_start += length;
return bytesFromBitBuffer + length;
Expand Down
Loading

0 comments on commit 69feffc

Please sign in to comment.