Various copilot suggestions. Tests still passing.

This commit is contained in:
Nanook
2025-07-20 19:16:11 +01:00
parent 938775789d
commit 5fe9516c09
5 changed files with 25 additions and 14 deletions

View File

@@ -175,19 +175,11 @@ internal class StreamingZipHeaderFactory : ZipHeaderFactory
} // Check if zip is streaming ( Length is 0 and is declared in PostDataDescriptor )
else if (local_header.Flags.HasFlag(HeaderFlags.UsePostDataDescriptor))
{
//var isRecording = rewindableStream.IsRecording;
//long pos = rewindableStream.Position;
//if (!isRecording)
//{
// rewindableStream.StartRecording();
//}
var nextHeaderBytes = reader.ReadUInt32();
((IStreamStack)rewindableStream).Rewind(4);
((IStreamStack)rewindableStream).Rewind(sizeof(uint));
// Check if next data is PostDataDescriptor, streamed file with 0 length
header.HasData = !IsHeader(nextHeaderBytes);
//((IStreamStack)rewindableStream).StackSeek(pos);
//rewindableStream.Rewind(!isRecording);
}
else // We are not streaming and compressed size is 0, we have no data
{

View File

@@ -78,7 +78,7 @@ public class LzmaStream : Stream, IStreamStack
_isLzma2 = isLzma2;
#if DEBUG_STREAMS
this.DebugConstruct(typeof(LZipStream));
this.DebugConstruct(typeof(LzmaStream));
#endif
if (!isLzma2)
@@ -144,7 +144,7 @@ public class LzmaStream : Stream, IStreamStack
_encoder.SetStreams(null, outputStream, -1, -1);
#if DEBUG_STREAMS
this.DebugConstruct(typeof(LZipStream));
this.DebugConstruct(typeof(LzmaStream));
#endif
if (presetDictionary != null)
@@ -169,7 +169,7 @@ public class LzmaStream : Stream, IStreamStack
}
_isDisposed = true;
#if DEBUG_STREAMS
this.DebugDispose(typeof(LZipStream));
this.DebugDispose(typeof(LzmaStream));
#endif
if (disposing)
{

View File

@@ -64,7 +64,7 @@ namespace SharpCompress.IO
if (current.BufferSize != 0)
{
buffStream = current;
buffStream.BufferPosition -= count;
buffStream.BufferPosition -= Math.Min(buffStream.BufferPosition, count);
}
current = current?.BaseStream() as IStreamStack;
}

View File

@@ -23,6 +23,14 @@ public class SharpCompressStream : Stream, IStreamStack
private bool _bufferingEnabled;
private long _baseInitialPos;
private void ValidateBufferState()
{
if (_bufferPosition < 0 || _bufferPosition > _bufferedLength)
{
throw new InvalidOperationException("Buffer state is inconsistent: _bufferPosition is out of range.");
}
}
int IStreamStack.BufferSize
{
get => _bufferingEnabled ? _bufferSize : 0;
@@ -37,6 +45,10 @@ public class SharpCompressStream : Stream, IStreamStack
_buffer = new byte[_bufferSize];
_bufferPosition = 0;
_bufferedLength = 0;
if (_bufferingEnabled)
{
ValidateBufferState(); // Add here
}
try
{
_internalPosition = Stream.Position;
@@ -61,6 +73,7 @@ public class SharpCompressStream : Stream, IStreamStack
throw new ArgumentOutOfRangeException(nameof(value));
_internalPosition = value;
_bufferPosition = value;
ValidateBufferState(); // Add here
}
}
}
@@ -194,6 +207,8 @@ public class SharpCompressStream : Stream, IStreamStack
if (_bufferingEnabled)
{
ValidateBufferState();
// Fill buffer if needed
if (_bufferedLength == 0)
{
@@ -240,6 +255,11 @@ public class SharpCompressStream : Stream, IStreamStack
public override long Seek(long offset, SeekOrigin origin)
{
if (_bufferingEnabled)
{
ValidateBufferState();
}
long orig = _internalPosition;
long targetPos;
// Calculate the absolute target position based on origin

View File

@@ -20,7 +20,6 @@ public class SharpCompressStreamTests
for (int i = 0; i < ms.Length; i += 4)
{
bw.Write(i);
var buffer = ArrayPool<byte>.Shared.Rent(1024);
}
}
ms.Position = 0;