This commit is contained in:
Adam Hathcock
2026-01-23 10:32:11 +00:00
parent 060b1ed5dd
commit abe0087cfd
5 changed files with 82 additions and 64 deletions

View File

@@ -58,10 +58,11 @@ public class ExplodeStream : Stream, IStreamStack
private int distance;
private int length;
private ExplodeStream(Stream inStr,
long compressedSize,
long uncompressedSize,
HeaderFlags generalPurposeBitFlag
private ExplodeStream(
Stream inStr,
long compressedSize,
long uncompressedSize,
HeaderFlags generalPurposeBitFlag
)
{
inStream = inStr;
@@ -74,13 +75,17 @@ public class ExplodeStream : Stream, IStreamStack
windowsBuffer = new byte[WSIZE];
}
internal static ExplodeStream Create(Stream inStr, long compressedSize, long uncompressedSize, HeaderFlags generalPurposeBitFlag)
internal static ExplodeStream Create(
Stream inStr,
long compressedSize,
long uncompressedSize,
HeaderFlags generalPurposeBitFlag
)
{
var ex = new ExplodeStream(inStr, compressedSize, uncompressedSize, generalPurposeBitFlag);
ex.explode_SetTables();
ex.
explode_var_init();
return ex;
ex.explode_var_init();
return ex;
}
protected override void Dispose(bool disposing)

View File

@@ -1,4 +1,3 @@
using System;
using System.Buffers.Binary;
using System.IO;
@@ -94,14 +93,18 @@ public partial class LzmaStream : Stream, IStreamStack
}
}
public static LzmaStream Create(
byte[] properties,
Stream inputStream,
bool leaveOpen = false
) => Create(properties, inputStream, -1, -1, null, properties.Length < 5, leaveOpen);
public static LzmaStream Create(byte[] properties, Stream inputStream, bool leaveOpen = false)
=> Create(properties, inputStream, -1, -1, null, properties.Length < 5, leaveOpen);
public static LzmaStream Create(byte[] properties, Stream inputStream, long inputSize, bool leaveOpen = false)
=> Create(properties, inputStream, inputSize, -1, null, properties.Length < 5, leaveOpen);
public static LzmaStream Create(
byte[] properties,
Stream inputStream,
long inputSize,
bool leaveOpen = false
) => Create(properties, inputStream, inputSize, -1, null, properties.Length < 5, leaveOpen);
public static LzmaStream Create(
byte[] properties,
@@ -109,8 +112,8 @@ public partial class LzmaStream : Stream, IStreamStack
long inputSize,
long outputSize,
bool leaveOpen = false
)
=> Create(
) =>
Create(
properties,
inputStream,
inputSize,
@@ -130,7 +133,14 @@ public partial class LzmaStream : Stream, IStreamStack
bool leaveOpen = false
)
{
var lzma = new LzmaStream(properties, inputStream, inputSize, outputSize, isLzma2, leaveOpen);
var lzma = new LzmaStream(
properties,
inputStream,
inputSize,
outputSize,
isLzma2,
leaveOpen
);
if (!isLzma2)
{
if (presetDictionary != null)
@@ -144,17 +154,14 @@ public partial class LzmaStream : Stream, IStreamStack
{
if (presetDictionary != null)
{
lzma. _outWindow.Train(presetDictionary);
lzma. _needDictReset = false;
lzma._outWindow.Train(presetDictionary);
lzma._needDictReset = false;
}
}
return lzma;
}
private LzmaStream(
LzmaEncoderProperties properties,
bool isLzma2,
Stream? presetDictionary
)
private LzmaStream(LzmaEncoderProperties properties, bool isLzma2, Stream? presetDictionary)
{
_isLzma2 = isLzma2;
_availableBytes = 0;
@@ -176,8 +183,12 @@ public partial class LzmaStream : Stream, IStreamStack
_encoder.Train(presetDictionary);
}
}
public static LzmaStream Create(LzmaEncoderProperties properties, bool isLzma2, Stream outputStream)
=> Create(properties, isLzma2, null, outputStream);
public static LzmaStream Create(
LzmaEncoderProperties properties,
bool isLzma2,
Stream outputStream
) => Create(properties, isLzma2, null, outputStream);
public static LzmaStream Create(
LzmaEncoderProperties properties,
@@ -186,7 +197,6 @@ public partial class LzmaStream : Stream, IStreamStack
Stream outputStream
)
{
var lzma = new LzmaStream(properties, isLzma2, presetDictionary);
lzma._encoder!.SetStreams(null, outputStream, -1, -1);

View File

@@ -540,7 +540,11 @@ public class LzmaStreamAsyncTests
{
using var inputStream = new MemoryStream(LzmaResultData);
using MemoryStream outputStream = new();
using var lzmaStream = LzmaStream.Create(LzmaEncoderProperties.Default, false, outputStream);
using var lzmaStream = LzmaStream.Create(
LzmaEncoderProperties.Default,
false,
outputStream
);
await inputStream.CopyToAsync(lzmaStream).ConfigureAwait(false);
lzmaStream.Close();
Assert.NotEqual(0, outputStream.Length);
@@ -551,7 +555,11 @@ public class LzmaStreamAsyncTests
{
var input = new MemoryStream(LzmaResultData);
var compressed = new MemoryStream();
var lzmaEncodingStream = LzmaStream.Create(LzmaEncoderProperties.Default, false, compressed);
var lzmaEncodingStream = LzmaStream.Create(
LzmaEncoderProperties.Default,
false,
compressed
);
await input.CopyToAsync(lzmaEncodingStream).ConfigureAwait(false);
lzmaEncodingStream.Close();
compressed.Position = 0;

View File

@@ -536,7 +536,11 @@ public class LzmaStreamTests
{
using var inputStream = new MemoryStream(LzmaResultData);
using MemoryStream outputStream = new();
using var lzmaStream = LzmaStream.Create(LzmaEncoderProperties.Default, false, outputStream);
using var lzmaStream = LzmaStream.Create(
LzmaEncoderProperties.Default,
false,
outputStream
);
inputStream.CopyTo(lzmaStream);
lzmaStream.Close();
Assert.NotEqual(0, outputStream.Length);
@@ -547,7 +551,11 @@ public class LzmaStreamTests
{
var input = new MemoryStream(LzmaResultData);
var compressed = new MemoryStream();
var lzmaEncodingStream = LzmaStream.Create(LzmaEncoderProperties.Default, false, compressed);
var lzmaEncodingStream = LzmaStream.Create(
LzmaEncoderProperties.Default,
false,
compressed
);
input.CopyTo(lzmaEncodingStream);
lzmaEncodingStream.Close();
compressed.Position = 0;

View File

@@ -99,11 +99,7 @@ public class SharpCompressStreamDisposalTests
// Arrange
var data = new byte[0x10000];
var innerStream = new MemoryStream(data);
var sharpStream = new SharpCompressStream(
innerStream,
leaveOpen: true,
bufferSize: 0x1000
);
var sharpStream = new SharpCompressStream(innerStream, leaveOpen: true, bufferSize: 0x1000);
// Get initial pool stats
var initialRented = ArrayPool<byte>.Shared.ToString();
@@ -177,11 +173,7 @@ public class SharpCompressStreamDisposalTests
// Arrange
var data = new byte[0x10000];
var innerStream = new TestStream(new MemoryStream(data));
var sharpStream = new SharpCompressStream(
innerStream,
leaveOpen: true,
bufferSize: 0x2000
);
var sharpStream = new SharpCompressStream(innerStream, leaveOpen: true, bufferSize: 0x2000);
// Act - read some data to fill buffer
var buffer = new byte[100];
@@ -261,8 +253,8 @@ public class SharpCompressStreamDisposalTests
);
// Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
() => sharpStream.DisposeAsync().AsTask()
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() =>
sharpStream.DisposeAsync().AsTask()
);
Assert.Contains("ThrowOnDispose", ex.Message);
}
@@ -289,11 +281,7 @@ public class SharpCompressStreamDisposalTests
// Arrange
var data = new byte[0x10000];
var innerStream = new MemoryStream(data);
var sharpStream = new SharpCompressStream(
innerStream,
leaveOpen: true,
bufferSize: 0x1000
);
var sharpStream = new SharpCompressStream(innerStream, leaveOpen: true, bufferSize: 0x1000);
// Act
await sharpStream.DisposeAsync();
@@ -342,11 +330,7 @@ public class SharpCompressStreamDisposalTests
// Arrange
var data = new byte[0x10000];
var innerStream = new TestStream(new MemoryStream(data));
var sharpStream = new SharpCompressStream(
innerStream,
leaveOpen: true,
bufferSize: 0x2000
);
var sharpStream = new SharpCompressStream(innerStream, leaveOpen: true, bufferSize: 0x2000);
// Act - read some data to fill buffer
var buffer = new byte[100];
@@ -402,10 +386,12 @@ public class SharpCompressStreamDisposalTests
{
// Arrange & Act
TestStream innerStream;
using (var sharpStream = new SharpCompressStream(
innerStream = new TestStream(new MemoryStream()),
leaveOpen: false
))
using (
var sharpStream = new SharpCompressStream(
innerStream = new TestStream(new MemoryStream()),
leaveOpen: false
)
)
{
// Use the stream
var buffer = new byte[10];
@@ -423,10 +409,12 @@ public class SharpCompressStreamDisposalTests
{
// Arrange & Act
TestStream innerStream;
await using (var sharpStream = new SharpCompressStream(
innerStream = new TestStream(new MemoryStream()),
leaveOpen: false
))
await using (
var sharpStream = new SharpCompressStream(
innerStream = new TestStream(new MemoryStream()),
leaveOpen: false
)
)
{
// Use the stream
var buffer = new byte[10];
@@ -436,7 +424,6 @@ public class SharpCompressStreamDisposalTests
// Assert
Assert.True(innerStream.IsDisposed);
}
#endif
[Fact]