mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-11-18 02:11:57 +00:00
remove memorystream
This commit is contained in:
@@ -35,14 +35,7 @@ internal partial class SharpCompressStream
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
// If recording is active or we're reading from the recording buffer, use legacy behavior
|
||||
if (IsRecording || (isRewound && bufferStream.Position != bufferStream.Length))
|
||||
{
|
||||
return await ReadWithRecordingAsync(buffer, offset, count, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// If ring buffer is enabled (and not recording), use ring buffer logic
|
||||
// If ring buffer is enabled, use ring buffer logic
|
||||
if (_ringBuffer is not null)
|
||||
{
|
||||
return await ReadWithRingBufferAsync(buffer, offset, count, cancellationToken)
|
||||
@@ -58,65 +51,6 @@ internal partial class SharpCompressStream
|
||||
return read;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Async version of ReadWithRecording (legacy behavior for format detection).
|
||||
/// </summary>
|
||||
private async Task<int> ReadWithRecordingAsync(
|
||||
byte[] buffer,
|
||||
int offset,
|
||||
int count,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
int read;
|
||||
if (isRewound && bufferStream.Position != bufferStream.Length)
|
||||
{
|
||||
var readCount = Math.Min(count, (int)(bufferStream.Length - bufferStream.Position));
|
||||
read = await bufferStream
|
||||
.ReadAsync(buffer, offset, readCount, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (read < count)
|
||||
{
|
||||
var tempRead = await stream
|
||||
.ReadAsync(buffer, offset + read, count - read, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (IsRecording)
|
||||
{
|
||||
await bufferStream
|
||||
.WriteAsync(buffer, offset + read, tempRead, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
else if (_ringBuffer is not null && tempRead > 0)
|
||||
{
|
||||
// When transitioning out of recording mode, add to ring buffer
|
||||
// so that future rewinds will work
|
||||
_ringBuffer.Write(buffer, offset + read, tempRead);
|
||||
}
|
||||
streamPosition += tempRead;
|
||||
_logicalPosition = streamPosition;
|
||||
read += tempRead;
|
||||
}
|
||||
if (bufferStream.Position == bufferStream.Length)
|
||||
{
|
||||
isRewound = false;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
read = await stream
|
||||
.ReadAsync(buffer, offset, count, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (IsRecording)
|
||||
{
|
||||
await bufferStream
|
||||
.WriteAsync(buffer, offset, read, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
streamPosition += read;
|
||||
_logicalPosition = streamPosition;
|
||||
return read;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Async version of ReadWithRingBuffer.
|
||||
/// </summary>
|
||||
@@ -133,15 +67,25 @@ internal partial class SharpCompressStream
|
||||
while (count > 0 && _logicalPosition < streamPosition)
|
||||
{
|
||||
long bytesFromEnd = streamPosition - _logicalPosition;
|
||||
int available = _ringBuffer!.ReadFromEnd(bytesFromEnd, buffer, offset, count);
|
||||
|
||||
// Verify data is available in ring buffer
|
||||
if (!_ringBuffer!.CanReadFromEnd(bytesFromEnd))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Ring buffer underflow: trying to read {bytesFromEnd} bytes back, "
|
||||
+ $"but buffer only holds {_ringBuffer.Length} bytes."
|
||||
);
|
||||
}
|
||||
|
||||
int available = _ringBuffer.ReadFromEnd(bytesFromEnd, buffer, offset, count);
|
||||
totalRead += available;
|
||||
offset += available;
|
||||
count -= available;
|
||||
_logicalPosition += available;
|
||||
}
|
||||
|
||||
// If more data needed, read from underlying stream
|
||||
if (count > 0)
|
||||
// If more data needed and we're caught up, read from underlying stream
|
||||
if (count > 0 && _logicalPosition == streamPosition)
|
||||
{
|
||||
int read = await stream
|
||||
.ReadAsync(buffer, offset, count, cancellationToken)
|
||||
@@ -183,13 +127,7 @@ internal partial class SharpCompressStream
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
// If recording is active or we're reading from the recording buffer, use legacy behavior
|
||||
if (IsRecording || (isRewound && bufferStream.Position != bufferStream.Length))
|
||||
{
|
||||
return await ReadWithRecordingAsync(buffer, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// If ring buffer is enabled (and not recording), use ring buffer logic
|
||||
// If ring buffer is enabled, use ring buffer logic
|
||||
if (_ringBuffer is not null)
|
||||
{
|
||||
return await ReadWithRingBufferAsync(buffer, cancellationToken).ConfigureAwait(false);
|
||||
@@ -202,63 +140,6 @@ internal partial class SharpCompressStream
|
||||
return read;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Async version of ReadWithRecording for Memory<byte> (legacy behavior for format detection).
|
||||
/// </summary>
|
||||
private async ValueTask<int> ReadWithRecordingAsync(
|
||||
Memory<byte> buffer,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
int read;
|
||||
if (isRewound && bufferStream.Position != bufferStream.Length)
|
||||
{
|
||||
var readCount = (int)
|
||||
Math.Min(buffer.Length, bufferStream.Length - bufferStream.Position);
|
||||
read = await bufferStream
|
||||
.ReadAsync(buffer.Slice(0, readCount), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (read < buffer.Length)
|
||||
{
|
||||
var tempRead = await stream
|
||||
.ReadAsync(buffer.Slice(read), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (IsRecording)
|
||||
{
|
||||
await bufferStream
|
||||
.WriteAsync(buffer.Slice(read, tempRead), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
else if (_ringBuffer is not null && tempRead > 0)
|
||||
{
|
||||
// When transitioning out of recording mode, add to ring buffer
|
||||
// so that future rewinds will work
|
||||
var tempBuffer = buffer.Slice(read, tempRead).ToArray();
|
||||
_ringBuffer.Write(tempBuffer, 0, tempRead);
|
||||
}
|
||||
streamPosition += tempRead;
|
||||
_logicalPosition = streamPosition;
|
||||
read += tempRead;
|
||||
}
|
||||
if (bufferStream.Position == bufferStream.Length)
|
||||
{
|
||||
isRewound = false;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
read = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
|
||||
if (IsRecording)
|
||||
{
|
||||
await bufferStream
|
||||
.WriteAsync(buffer.Slice(0, read), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
streamPosition += read;
|
||||
_logicalPosition = streamPosition;
|
||||
return read;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Async version of ReadWithRingBuffer for Memory<byte>.
|
||||
/// </summary>
|
||||
@@ -276,13 +157,18 @@ internal partial class SharpCompressStream
|
||||
while (count > 0 && _logicalPosition < streamPosition)
|
||||
{
|
||||
long bytesFromEnd = streamPosition - _logicalPosition;
|
||||
|
||||
// Verify data is available in ring buffer
|
||||
if (!_ringBuffer!.CanReadFromEnd(bytesFromEnd))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Ring buffer underflow: trying to read {bytesFromEnd} bytes back, "
|
||||
+ $"but buffer only holds {_ringBuffer.Length} bytes."
|
||||
);
|
||||
}
|
||||
|
||||
var tempBuffer = new byte[Math.Min(count, (int)bytesFromEnd)];
|
||||
int available = _ringBuffer!.ReadFromEnd(
|
||||
bytesFromEnd,
|
||||
tempBuffer,
|
||||
0,
|
||||
tempBuffer.Length
|
||||
);
|
||||
int available = _ringBuffer.ReadFromEnd(bytesFromEnd, tempBuffer, 0, tempBuffer.Length);
|
||||
tempBuffer.AsSpan(0, available).CopyTo(buffer.Span.Slice(offset));
|
||||
|
||||
totalRead += available;
|
||||
@@ -291,8 +177,8 @@ internal partial class SharpCompressStream
|
||||
_logicalPosition += available;
|
||||
}
|
||||
|
||||
// If more data needed, read from underlying stream
|
||||
if (count > 0)
|
||||
// If more data needed and we're caught up, read from underlying stream
|
||||
if (count > 0 && _logicalPosition == streamPosition)
|
||||
{
|
||||
int read = await stream
|
||||
.ReadAsync(buffer.Slice(offset, count), cancellationToken)
|
||||
|
||||
Reference in New Issue
Block a user