Skip to content

Commit 16903fa

Browse files
Merge pull request #2352 from stefannikolei/sn/nullable/ChunkedMemoryStream
Remove nullable disable from ChunkedMemoryStream
2 parents dd1fc5c + 806b149 commit 16903fa

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/ImageSharp/IO/ChunkedMemoryStream.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers;
65
using System.Runtime.CompilerServices;
@@ -19,7 +18,7 @@ internal sealed class ChunkedMemoryStream : Stream
1918
private readonly MemoryAllocator allocator;
2019

2120
// Data
22-
private MemoryChunk memoryChunk;
21+
private MemoryChunk? memoryChunk;
2322

2423
// The total number of allocated chunks
2524
private int chunkCount;
@@ -31,13 +30,13 @@ internal sealed class ChunkedMemoryStream : Stream
3130
private bool isDisposed;
3231

3332
// Current chunk to write to
34-
private MemoryChunk writeChunk;
33+
private MemoryChunk? writeChunk;
3534

3635
// Offset into chunk to write to
3736
private int writeOffset;
3837

3938
// Current chunk to read from
40-
private MemoryChunk readChunk;
39+
private MemoryChunk? readChunk;
4140

4241
// Offset into chunk to read from
4342
private int readOffset;
@@ -48,8 +47,6 @@ internal sealed class ChunkedMemoryStream : Stream
4847
/// <param name="allocator">The memory allocator.</param>
4948
public ChunkedMemoryStream(MemoryAllocator allocator)
5049
{
51-
Guard.NotNull(allocator, nameof(allocator));
52-
5350
this.allocatorCapacity = allocator.GetBufferCapacityInBytes();
5451
this.allocator = allocator;
5552
}
@@ -71,10 +68,10 @@ public override long Length
7168
this.EnsureNotDisposed();
7269

7370
int length = 0;
74-
MemoryChunk chunk = this.memoryChunk;
71+
MemoryChunk? chunk = this.memoryChunk;
7572
while (chunk != null)
7673
{
77-
MemoryChunk next = chunk.Next;
74+
MemoryChunk? next = chunk.Next;
7875
if (next != null)
7976
{
8077
length += chunk.Length;
@@ -104,8 +101,8 @@ public override long Position
104101
}
105102

106103
int pos = 0;
107-
MemoryChunk chunk = this.memoryChunk;
108-
while (chunk != this.readChunk)
104+
MemoryChunk? chunk = this.memoryChunk;
105+
while (chunk != this.readChunk && chunk is not null)
109106
{
110107
pos += chunk.Length;
111108
chunk = chunk.Next;
@@ -126,14 +123,14 @@ public override long Position
126123
}
127124

128125
// Back up current position in case new position is out of range
129-
MemoryChunk backupReadChunk = this.readChunk;
126+
MemoryChunk? backupReadChunk = this.readChunk;
130127
int backupReadOffset = this.readOffset;
131128

132129
this.readChunk = null;
133130
this.readOffset = 0;
134131

135132
int leftUntilAtPos = (int)value;
136-
MemoryChunk chunk = this.memoryChunk;
133+
MemoryChunk? chunk = this.memoryChunk;
137134
while (chunk != null)
138135
{
139136
if ((leftUntilAtPos < chunk.Length)
@@ -365,6 +362,8 @@ private void WriteImpl(ReadOnlySpan<byte> buffer)
365362
this.writeOffset = 0;
366363
}
367364

365+
Guard.NotNull(this.writeChunk);
366+
368367
Span<byte> chunkBuffer = this.writeChunk.Buffer.GetSpan();
369368
int chunkSize = this.writeChunk.Length;
370369
int count = buffer.Length;
@@ -402,6 +401,8 @@ public override void WriteByte(byte value)
402401
this.writeOffset = 0;
403402
}
404403

404+
Guard.NotNull(this.writeChunk);
405+
405406
IMemoryOwner<byte> chunkBuffer = this.writeChunk.Buffer;
406407
int chunkSize = this.writeChunk.Length;
407408

@@ -426,7 +427,7 @@ public byte[] ToArray()
426427
int length = (int)this.Length; // This will throw if stream is closed
427428
byte[] copy = new byte[this.Length];
428429

429-
MemoryChunk backupReadChunk = this.readChunk;
430+
MemoryChunk? backupReadChunk = this.readChunk;
430431
int backupReadOffset = this.readOffset;
431432

432433
this.readChunk = this.memoryChunk;
@@ -522,15 +523,14 @@ private MemoryChunk AllocateMemoryChunk()
522523
// available contiguous buffer size.
523524
IMemoryOwner<byte> buffer = this.allocator.Allocate<byte>(Math.Min(this.allocatorCapacity, GetChunkSize(this.chunkCount++)));
524525

525-
return new MemoryChunk
526+
return new MemoryChunk(buffer)
526527
{
527-
Buffer = buffer,
528528
Next = null,
529529
Length = buffer.Length()
530530
};
531531
}
532532

533-
private static void ReleaseMemoryChunks(MemoryChunk chunk)
533+
private static void ReleaseMemoryChunks(MemoryChunk? chunk)
534534
{
535535
while (chunk != null)
536536
{
@@ -555,11 +555,13 @@ private sealed class MemoryChunk : IDisposable
555555
{
556556
private bool isDisposed;
557557

558-
public IMemoryOwner<byte> Buffer { get; set; }
558+
public MemoryChunk(IMemoryOwner<byte> buffer) => this.Buffer = buffer;
559+
560+
public IMemoryOwner<byte> Buffer { get; }
559561

560-
public MemoryChunk Next { get; set; }
562+
public MemoryChunk? Next { get; set; }
561563

562-
public int Length { get; set; }
564+
public int Length { get; init; }
563565

564566
private void Dispose(bool disposing)
565567
{
@@ -570,7 +572,6 @@ private void Dispose(bool disposing)
570572
this.Buffer.Dispose();
571573
}
572574

573-
this.Buffer = null;
574575
this.isDisposed = true;
575576
}
576577
}

0 commit comments

Comments
 (0)