Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like _state should be nullable, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, good catch. Fixed (both here and in the corresponding decoder field).

}
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