Skip to content

Commit b79e61f

Browse files
committed
Remove nullable disable from ChunkedMemoryStream
1 parent 37f5cc5 commit b79e61f

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/ImageSharp/IO/ChunkedMemoryStream.cs

Lines changed: 21 additions & 18 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;
@@ -71,10 +70,10 @@ public override long Length
7170
this.EnsureNotDisposed();
7271

7372
int length = 0;
74-
MemoryChunk chunk = this.memoryChunk;
73+
MemoryChunk? chunk = this.memoryChunk;
7574
while (chunk != null)
7675
{
77-
MemoryChunk next = chunk.Next;
76+
MemoryChunk? next = chunk.Next;
7877
if (next != null)
7978
{
8079
length += chunk.Length;
@@ -104,8 +103,8 @@ public override long Position
104103
}
105104

106105
int pos = 0;
107-
MemoryChunk chunk = this.memoryChunk;
108-
while (chunk != this.readChunk)
106+
MemoryChunk? chunk = this.memoryChunk;
107+
while (chunk != this.readChunk && chunk is not null)
109108
{
110109
pos += chunk.Length;
111110
chunk = chunk.Next;
@@ -126,14 +125,14 @@ public override long Position
126125
}
127126

128127
// Back up current position in case new position is out of range
129-
MemoryChunk backupReadChunk = this.readChunk;
128+
MemoryChunk? backupReadChunk = this.readChunk;
130129
int backupReadOffset = this.readOffset;
131130

132131
this.readChunk = null;
133132
this.readOffset = 0;
134133

135134
int leftUntilAtPos = (int)value;
136-
MemoryChunk chunk = this.memoryChunk;
135+
MemoryChunk? chunk = this.memoryChunk;
137136
while (chunk != null)
138137
{
139138
if ((leftUntilAtPos < chunk.Length)
@@ -365,6 +364,8 @@ private void WriteImpl(ReadOnlySpan<byte> buffer)
365364
this.writeOffset = 0;
366365
}
367366

367+
Guard.NotNull(this.writeChunk);
368+
368369
Span<byte> chunkBuffer = this.writeChunk.Buffer.GetSpan();
369370
int chunkSize = this.writeChunk.Length;
370371
int count = buffer.Length;
@@ -402,6 +403,8 @@ public override void WriteByte(byte value)
402403
this.writeOffset = 0;
403404
}
404405

406+
Guard.NotNull(this.writeChunk);
407+
405408
IMemoryOwner<byte> chunkBuffer = this.writeChunk.Buffer;
406409
int chunkSize = this.writeChunk.Length;
407410

@@ -426,7 +429,7 @@ public byte[] ToArray()
426429
int length = (int)this.Length; // This will throw if stream is closed
427430
byte[] copy = new byte[this.Length];
428431

429-
MemoryChunk backupReadChunk = this.readChunk;
432+
MemoryChunk? backupReadChunk = this.readChunk;
430433
int backupReadOffset = this.readOffset;
431434

432435
this.readChunk = this.memoryChunk;
@@ -522,15 +525,14 @@ private MemoryChunk AllocateMemoryChunk()
522525
// available contiguous buffer size.
523526
IMemoryOwner<byte> buffer = this.allocator.Allocate<byte>(Math.Min(this.allocatorCapacity, GetChunkSize(this.chunkCount++)));
524527

525-
return new MemoryChunk
528+
return new MemoryChunk(buffer)
526529
{
527-
Buffer = buffer,
528530
Next = null,
529531
Length = buffer.Length()
530532
};
531533
}
532534

533-
private static void ReleaseMemoryChunks(MemoryChunk chunk)
535+
private static void ReleaseMemoryChunks(MemoryChunk? chunk)
534536
{
535537
while (chunk != null)
536538
{
@@ -555,11 +557,13 @@ private sealed class MemoryChunk : IDisposable
555557
{
556558
private bool isDisposed;
557559

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

560-
public MemoryChunk Next { get; set; }
564+
public MemoryChunk? Next { get; set; }
561565

562-
public int Length { get; set; }
566+
public int Length { get; init; }
563567

564568
private void Dispose(bool disposing)
565569
{
@@ -570,7 +574,6 @@ private void Dispose(bool disposing)
570574
this.Buffer.Dispose();
571575
}
572576

573-
this.Buffer = null;
574577
this.isDisposed = true;
575578
}
576579
}

0 commit comments

Comments
 (0)