1
1
// Copyright (c) Six Labors.
2
2
// Licensed under the Six Labors Split License.
3
- #nullable disable
4
3
5
4
using System . Buffers ;
6
5
using System . Runtime . CompilerServices ;
@@ -19,7 +18,7 @@ internal sealed class ChunkedMemoryStream : Stream
19
18
private readonly MemoryAllocator allocator ;
20
19
21
20
// Data
22
- private MemoryChunk memoryChunk ;
21
+ private MemoryChunk ? memoryChunk ;
23
22
24
23
// The total number of allocated chunks
25
24
private int chunkCount ;
@@ -31,13 +30,13 @@ internal sealed class ChunkedMemoryStream : Stream
31
30
private bool isDisposed ;
32
31
33
32
// Current chunk to write to
34
- private MemoryChunk writeChunk ;
33
+ private MemoryChunk ? writeChunk ;
35
34
36
35
// Offset into chunk to write to
37
36
private int writeOffset ;
38
37
39
38
// Current chunk to read from
40
- private MemoryChunk readChunk ;
39
+ private MemoryChunk ? readChunk ;
41
40
42
41
// Offset into chunk to read from
43
42
private int readOffset ;
@@ -48,8 +47,6 @@ internal sealed class ChunkedMemoryStream : Stream
48
47
/// <param name="allocator">The memory allocator.</param>
49
48
public ChunkedMemoryStream ( MemoryAllocator allocator )
50
49
{
51
- Guard . NotNull ( allocator , nameof ( allocator ) ) ;
52
-
53
50
this . allocatorCapacity = allocator . GetBufferCapacityInBytes ( ) ;
54
51
this . allocator = allocator ;
55
52
}
@@ -71,10 +68,10 @@ public override long Length
71
68
this . EnsureNotDisposed ( ) ;
72
69
73
70
int length = 0 ;
74
- MemoryChunk chunk = this . memoryChunk ;
71
+ MemoryChunk ? chunk = this . memoryChunk ;
75
72
while ( chunk != null )
76
73
{
77
- MemoryChunk next = chunk . Next ;
74
+ MemoryChunk ? next = chunk . Next ;
78
75
if ( next != null )
79
76
{
80
77
length += chunk . Length ;
@@ -104,8 +101,8 @@ public override long Position
104
101
}
105
102
106
103
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 )
109
106
{
110
107
pos += chunk . Length ;
111
108
chunk = chunk . Next ;
@@ -126,14 +123,14 @@ public override long Position
126
123
}
127
124
128
125
// Back up current position in case new position is out of range
129
- MemoryChunk backupReadChunk = this . readChunk ;
126
+ MemoryChunk ? backupReadChunk = this . readChunk ;
130
127
int backupReadOffset = this . readOffset ;
131
128
132
129
this . readChunk = null ;
133
130
this . readOffset = 0 ;
134
131
135
132
int leftUntilAtPos = ( int ) value ;
136
- MemoryChunk chunk = this . memoryChunk ;
133
+ MemoryChunk ? chunk = this . memoryChunk ;
137
134
while ( chunk != null )
138
135
{
139
136
if ( ( leftUntilAtPos < chunk . Length )
@@ -365,6 +362,8 @@ private void WriteImpl(ReadOnlySpan<byte> buffer)
365
362
this . writeOffset = 0 ;
366
363
}
367
364
365
+ Guard . NotNull ( this . writeChunk ) ;
366
+
368
367
Span < byte > chunkBuffer = this . writeChunk . Buffer . GetSpan ( ) ;
369
368
int chunkSize = this . writeChunk . Length ;
370
369
int count = buffer . Length ;
@@ -402,6 +401,8 @@ public override void WriteByte(byte value)
402
401
this . writeOffset = 0 ;
403
402
}
404
403
404
+ Guard . NotNull ( this . writeChunk ) ;
405
+
405
406
IMemoryOwner < byte > chunkBuffer = this . writeChunk . Buffer ;
406
407
int chunkSize = this . writeChunk . Length ;
407
408
@@ -426,7 +427,7 @@ public byte[] ToArray()
426
427
int length = ( int ) this . Length ; // This will throw if stream is closed
427
428
byte [ ] copy = new byte [ this . Length ] ;
428
429
429
- MemoryChunk backupReadChunk = this . readChunk ;
430
+ MemoryChunk ? backupReadChunk = this . readChunk ;
430
431
int backupReadOffset = this . readOffset ;
431
432
432
433
this . readChunk = this . memoryChunk ;
@@ -522,15 +523,14 @@ private MemoryChunk AllocateMemoryChunk()
522
523
// available contiguous buffer size.
523
524
IMemoryOwner < byte > buffer = this . allocator . Allocate < byte > ( Math . Min ( this . allocatorCapacity , GetChunkSize ( this . chunkCount ++ ) ) ) ;
524
525
525
- return new MemoryChunk
526
+ return new MemoryChunk ( buffer )
526
527
{
527
- Buffer = buffer ,
528
528
Next = null ,
529
529
Length = buffer . Length ( )
530
530
} ;
531
531
}
532
532
533
- private static void ReleaseMemoryChunks ( MemoryChunk chunk )
533
+ private static void ReleaseMemoryChunks ( MemoryChunk ? chunk )
534
534
{
535
535
while ( chunk != null )
536
536
{
@@ -555,11 +555,13 @@ private sealed class MemoryChunk : IDisposable
555
555
{
556
556
private bool isDisposed ;
557
557
558
- public IMemoryOwner < byte > Buffer { get ; set ; }
558
+ public MemoryChunk ( IMemoryOwner < byte > buffer ) => this . Buffer = buffer ;
559
+
560
+ public IMemoryOwner < byte > Buffer { get ; }
559
561
560
- public MemoryChunk Next { get ; set ; }
562
+ public MemoryChunk ? Next { get ; set ; }
561
563
562
- public int Length { get ; set ; }
564
+ public int Length { get ; init ; }
563
565
564
566
private void Dispose ( bool disposing )
565
567
{
@@ -570,7 +572,6 @@ private void Dispose(bool disposing)
570
572
this . Buffer . Dispose ( ) ;
571
573
}
572
574
573
- this . Buffer = null ;
574
575
this . isDisposed = true ;
575
576
}
576
577
}
0 commit comments