Skip to content

Commit

Permalink
Annotate System.IO.Compression.Brotli for nullable reference types (d…
Browse files Browse the repository at this point in the history
…otnet#218)

* Annotate System.IO.Compression.Brotli for nullable reference types

* Address PR feedback
  • Loading branch information
stephentoub authored Nov 23, 2019
1 parent 4bc6819 commit 7bc6a67
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public BrotliStream(System.IO.Stream stream, System.IO.Compression.CompressionMo
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[] buffer, 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[] buffer, int offset, int count, System.AsyncCallback? asyncCallback, object? asyncState) { throw null; }
protected override void Dispose(bool disposing) { }
public override System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
public override int EndRead(System.IAsyncResult asyncResult) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Nullable>enable</Nullable>
<Configurations>netcoreapp-Debug;netcoreapp-Release</Configurations>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<AssemblyName>System.IO.Compression.Brotli</AssemblyName>
<OutputType>Library</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<Configurations>netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release</Configurations>
</PropertyGroup>
<!-- Default configurations to help VS understand the options -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ public override async ValueTask DisposeAsync()

private void ReleaseStateForDispose()
{
_stream = null;
_stream = null!;
_encoder.Dispose();
_decoder.Dispose();

byte[] buffer = _buffer;
if (buffer != null)
{
_buffer = null;
_buffer = null!;
if (!AsyncOperationIsActive)
{
ArrayPool<byte>.Shared.Return(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using size_t = System.IntPtr;
Expand All @@ -11,7 +12,7 @@ namespace System.IO.Compression
{
public struct BrotliDecoder : IDisposable
{
private SafeBrotliDecoderHandle _state;
private SafeBrotliDecoderHandle? _state;
private bool _disposed;

internal void InitializeDecoder()
Expand Down Expand Up @@ -43,6 +44,8 @@ private void EnsureNotDisposed()
public OperationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten)
{
EnsureInitialized();
Debug.Assert(_state != null);

bytesConsumed = 0;
bytesWritten = 0;
if (Interop.Brotli.BrotliDecoderIsFinished(_state))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public override int Read(Span<byte> buffer)
return totalWritten;
}

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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using size_t = System.IntPtr;
Expand All @@ -11,7 +12,7 @@ namespace System.IO.Compression
{
public partial struct BrotliEncoder : IDisposable
{
internal SafeBrotliEncoderHandle _state;
internal SafeBrotliEncoderHandle? _state;
private bool _disposed;

public BrotliEncoder(int quality, int window)
Expand Down Expand Up @@ -64,6 +65,7 @@ internal void SetQuality(int quality)
if (_state == null || _state.IsInvalid || _state.IsClosed)
{
InitializeEncoder();
Debug.Assert(_state != null && !_state.IsInvalid && !_state.IsClosed);
}
if (quality < BrotliUtils.Quality_Min || quality > BrotliUtils.Quality_Max)
{
Expand All @@ -81,6 +83,7 @@ internal void SetWindow(int window)
if (_state == null || _state.IsInvalid || _state.IsClosed)
{
InitializeEncoder();
Debug.Assert(_state != null && !_state.IsInvalid && !_state.IsClosed);
}
if (window < BrotliUtils.WindowBits_Min || window > BrotliUtils.WindowBits_Max)
{
Expand Down Expand Up @@ -119,6 +122,8 @@ public static int GetMaxCompressedLength(int inputSize)
internal OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, BrotliEncoderOperation operation)
{
EnsureInitialized();
Debug.Assert(_state != null);

bytesWritten = 0;
bytesConsumed = 0;
size_t availableOutput = (size_t)destination.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal void WriteCore(ReadOnlySpan<byte> buffer, bool isFinalBlock = false)
}
}

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

public override void EndWrite(IAsyncResult asyncResult) =>
Expand Down

0 comments on commit 7bc6a67

Please sign in to comment.