Browse Source

Merge pull request #2352 from stefannikolei/sn/nullable/ChunkedMemoryStream

Remove nullable disable from ChunkedMemoryStream
pull/2355/head
James Jackson-South 3 years ago
committed by GitHub
parent
commit
16903fa9ea
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 41
      src/ImageSharp/IO/ChunkedMemoryStream.cs

41
src/ImageSharp/IO/ChunkedMemoryStream.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using System.Buffers; using System.Buffers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -19,7 +18,7 @@ internal sealed class ChunkedMemoryStream : Stream
private readonly MemoryAllocator allocator; private readonly MemoryAllocator allocator;
// Data // Data
private MemoryChunk memoryChunk; private MemoryChunk? memoryChunk;
// The total number of allocated chunks // The total number of allocated chunks
private int chunkCount; private int chunkCount;
@ -31,13 +30,13 @@ internal sealed class ChunkedMemoryStream : Stream
private bool isDisposed; private bool isDisposed;
// Current chunk to write to // Current chunk to write to
private MemoryChunk writeChunk; private MemoryChunk? writeChunk;
// Offset into chunk to write to // Offset into chunk to write to
private int writeOffset; private int writeOffset;
// Current chunk to read from // Current chunk to read from
private MemoryChunk readChunk; private MemoryChunk? readChunk;
// Offset into chunk to read from // Offset into chunk to read from
private int readOffset; private int readOffset;
@ -48,8 +47,6 @@ internal sealed class ChunkedMemoryStream : Stream
/// <param name="allocator">The memory allocator.</param> /// <param name="allocator">The memory allocator.</param>
public ChunkedMemoryStream(MemoryAllocator allocator) public ChunkedMemoryStream(MemoryAllocator allocator)
{ {
Guard.NotNull(allocator, nameof(allocator));
this.allocatorCapacity = allocator.GetBufferCapacityInBytes(); this.allocatorCapacity = allocator.GetBufferCapacityInBytes();
this.allocator = allocator; this.allocator = allocator;
} }
@ -71,10 +68,10 @@ internal sealed class ChunkedMemoryStream : Stream
this.EnsureNotDisposed(); this.EnsureNotDisposed();
int length = 0; int length = 0;
MemoryChunk chunk = this.memoryChunk; MemoryChunk? chunk = this.memoryChunk;
while (chunk != null) while (chunk != null)
{ {
MemoryChunk next = chunk.Next; MemoryChunk? next = chunk.Next;
if (next != null) if (next != null)
{ {
length += chunk.Length; length += chunk.Length;
@ -104,8 +101,8 @@ internal sealed class ChunkedMemoryStream : Stream
} }
int pos = 0; int pos = 0;
MemoryChunk chunk = this.memoryChunk; MemoryChunk? chunk = this.memoryChunk;
while (chunk != this.readChunk) while (chunk != this.readChunk && chunk is not null)
{ {
pos += chunk.Length; pos += chunk.Length;
chunk = chunk.Next; chunk = chunk.Next;
@ -126,14 +123,14 @@ internal sealed class ChunkedMemoryStream : Stream
} }
// Back up current position in case new position is out of range // Back up current position in case new position is out of range
MemoryChunk backupReadChunk = this.readChunk; MemoryChunk? backupReadChunk = this.readChunk;
int backupReadOffset = this.readOffset; int backupReadOffset = this.readOffset;
this.readChunk = null; this.readChunk = null;
this.readOffset = 0; this.readOffset = 0;
int leftUntilAtPos = (int)value; int leftUntilAtPos = (int)value;
MemoryChunk chunk = this.memoryChunk; MemoryChunk? chunk = this.memoryChunk;
while (chunk != null) while (chunk != null)
{ {
if ((leftUntilAtPos < chunk.Length) if ((leftUntilAtPos < chunk.Length)
@ -365,6 +362,8 @@ internal sealed class ChunkedMemoryStream : Stream
this.writeOffset = 0; this.writeOffset = 0;
} }
Guard.NotNull(this.writeChunk);
Span<byte> chunkBuffer = this.writeChunk.Buffer.GetSpan(); Span<byte> chunkBuffer = this.writeChunk.Buffer.GetSpan();
int chunkSize = this.writeChunk.Length; int chunkSize = this.writeChunk.Length;
int count = buffer.Length; int count = buffer.Length;
@ -402,6 +401,8 @@ internal sealed class ChunkedMemoryStream : Stream
this.writeOffset = 0; this.writeOffset = 0;
} }
Guard.NotNull(this.writeChunk);
IMemoryOwner<byte> chunkBuffer = this.writeChunk.Buffer; IMemoryOwner<byte> chunkBuffer = this.writeChunk.Buffer;
int chunkSize = this.writeChunk.Length; int chunkSize = this.writeChunk.Length;
@ -426,7 +427,7 @@ internal sealed class ChunkedMemoryStream : Stream
int length = (int)this.Length; // This will throw if stream is closed int length = (int)this.Length; // This will throw if stream is closed
byte[] copy = new byte[this.Length]; byte[] copy = new byte[this.Length];
MemoryChunk backupReadChunk = this.readChunk; MemoryChunk? backupReadChunk = this.readChunk;
int backupReadOffset = this.readOffset; int backupReadOffset = this.readOffset;
this.readChunk = this.memoryChunk; this.readChunk = this.memoryChunk;
@ -522,15 +523,14 @@ internal sealed class ChunkedMemoryStream : Stream
// available contiguous buffer size. // available contiguous buffer size.
IMemoryOwner<byte> buffer = this.allocator.Allocate<byte>(Math.Min(this.allocatorCapacity, GetChunkSize(this.chunkCount++))); IMemoryOwner<byte> buffer = this.allocator.Allocate<byte>(Math.Min(this.allocatorCapacity, GetChunkSize(this.chunkCount++)));
return new MemoryChunk return new MemoryChunk(buffer)
{ {
Buffer = buffer,
Next = null, Next = null,
Length = buffer.Length() Length = buffer.Length()
}; };
} }
private static void ReleaseMemoryChunks(MemoryChunk chunk) private static void ReleaseMemoryChunks(MemoryChunk? chunk)
{ {
while (chunk != null) while (chunk != null)
{ {
@ -555,11 +555,13 @@ internal sealed class ChunkedMemoryStream : Stream
{ {
private bool isDisposed; private bool isDisposed;
public IMemoryOwner<byte> Buffer { get; set; } public MemoryChunk(IMemoryOwner<byte> buffer) => this.Buffer = buffer;
public IMemoryOwner<byte> Buffer { get; }
public MemoryChunk Next { get; set; } public MemoryChunk? Next { get; set; }
public int Length { get; set; } public int Length { get; init; }
private void Dispose(bool disposing) private void Dispose(bool disposing)
{ {
@ -570,7 +572,6 @@ internal sealed class ChunkedMemoryStream : Stream
this.Buffer.Dispose(); this.Buffer.Dispose();
} }
this.Buffer = null;
this.isDisposed = true; this.isDisposed = true;
} }
} }

Loading…
Cancel
Save