4
4
using System . Buffers ;
5
5
using System . Collections ;
6
6
using System . Runtime . CompilerServices ;
7
+ using System . Runtime . InteropServices ;
7
8
using SixLabors . ImageSharp . Memory ;
8
9
9
10
namespace SixLabors . ImageSharp . IO ;
@@ -13,11 +14,10 @@ namespace SixLabors.ImageSharp.IO;
13
14
/// Chunks are allocated by the <see cref="MemoryAllocator"/> assigned via the constructor
14
15
/// and is designed to take advantage of buffer pooling when available.
15
16
/// </summary>
16
- /// <summary>Provides an in-memory stream composed of non-contiguous chunks.</summary>
17
17
public class ChunkedMemoryStream : Stream
18
18
{
19
19
private readonly MemoryChunkBuffer memoryChunkBuffer ;
20
- private readonly byte [ ] singleReadBuffer = new byte [ 1 ] ;
20
+ private readonly byte [ ] singleByteBuffer = new byte [ 1 ] ;
21
21
22
22
private long length ;
23
23
private long position ;
@@ -101,8 +101,8 @@ public override int ReadByte()
101
101
return - 1 ;
102
102
}
103
103
104
- _ = this . Read ( this . singleReadBuffer , 0 , 1 ) ;
105
- return this . singleReadBuffer [ ^ 1 ] ;
104
+ _ = this . Read ( this . singleByteBuffer , 0 , 1 ) ;
105
+ return MemoryMarshal . GetReference < byte > ( this . singleByteBuffer ) ;
106
106
}
107
107
108
108
/// <inheritdoc/>
@@ -129,17 +129,17 @@ public override int Read(Span<byte> buffer)
129
129
int count = buffer . Length ;
130
130
131
131
long remaining = this . length - this . position ;
132
- if ( remaining > count )
133
- {
134
- remaining = count ;
135
- }
136
-
137
132
if ( remaining <= 0 )
138
133
{
139
134
// Already at the end of the stream, nothing to read
140
135
return 0 ;
141
136
}
142
137
138
+ if ( remaining > count )
139
+ {
140
+ remaining = count ;
141
+ }
142
+
143
143
int bytesToRead = ( int ) remaining ;
144
144
int bytesRead = 0 ;
145
145
while ( bytesToRead != 0 && this . currentChunk != this . memoryChunkBuffer . Length )
@@ -175,6 +175,14 @@ public override int Read(Span<byte> buffer)
175
175
return bytesRead ;
176
176
}
177
177
178
+ /// <inheritdoc/>
179
+ public override void WriteByte ( byte value )
180
+ {
181
+ this . EnsureNotDisposed ( ) ;
182
+ MemoryMarshal . Write ( this . singleByteBuffer , ref value ) ;
183
+ this . Write ( this . singleByteBuffer , 0 , 1 ) ;
184
+ }
185
+
178
186
/// <inheritdoc/>
179
187
public override void Write ( byte [ ] buffer , int offset , int count )
180
188
{
@@ -309,7 +317,7 @@ public byte[] ToArray()
309
317
byte [ ] copy = new byte [ this . length ] ;
310
318
311
319
this . Position = 0 ;
312
- this . Read ( copy , 0 , copy . Length ) ;
320
+ _ = this . Read ( copy , 0 , copy . Length ) ;
313
321
this . Position = position ;
314
322
return copy ;
315
323
}
0 commit comments