Skip to content

Commit a6d0572

Browse files
committed
Some corrections to latest changes
1 parent 30d3f5a commit a6d0572

File tree

6 files changed

+39
-29
lines changed

6 files changed

+39
-29
lines changed

Library/DiscUtils.Core/Compression/LZNT1.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
//
3030

3131
using System;
32+
using System.Buffers;
3233
using System.Collections.Immutable;
3334
using DiscUtils.Streams;
3435

@@ -41,7 +42,7 @@ namespace DiscUtils.Compression;
4142
/// Due to apparent bugs in Window's LZNT1 decompressor, it is <b>strongly</b> recommended that
4243
/// only the block size of 4096 is used. Other block sizes corrupt data on decompression.
4344
/// </remarks>
44-
internal sealed class LZNT1 : BlockCompressor
45+
public sealed class LZNT1 : BlockCompressor
4546
{
4647
private const ushort SubBlockIsCompressedFlag = 0x8000;
4748
private const ushort SubBlockSizeMask = 0x0fff;
@@ -301,20 +302,28 @@ public override int Decompress(ReadOnlySpan<byte> source, Span<byte> decompresse
301302

302303
private static ImmutableArray<byte> CalcCompressionBits()
303304
{
304-
var result = ImmutableArray.CreateBuilder<byte>(4096);
305-
byte offsetBits = 0;
305+
var result = ArrayPool<byte>.Shared.Rent(4096);
306306

307-
var y = 0x10;
308-
for (var x = 0; x < 4096; x++)
307+
try
309308
{
310-
result[x] = (byte)(4 + offsetBits);
311-
if (x == y)
309+
byte offsetBits = 0;
310+
311+
var y = 0x10;
312+
for (var x = 0; x < 4096; x++)
312313
{
313-
y <<= 1;
314-
offsetBits++;
314+
result[x] = (byte)(4 + offsetBits);
315+
if (x == y)
316+
{
317+
y <<= 1;
318+
offsetBits++;
319+
}
315320
}
316-
}
317321

318-
return result.ToImmutable();
322+
return ImmutableArray.Create(result, 0, 4096);
323+
}
324+
finally
325+
{
326+
ArrayPool<byte>.Shared.Return(result);
327+
}
319328
}
320329
}

Library/DiscUtils.Core/Compression/XpressStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace DiscUtils.Compression;
3333
/// </summary>
3434
/// <remarks>This class is optimized for the case where the entire stream contents
3535
/// fit into memory, it is not suitable for unbounded streams.</remarks>
36-
internal class XpressStream : ReadOnlyCompatibilityStream
36+
public class XpressStream : ReadOnlyCompatibilityStream
3737
{
3838
private readonly byte[] _buffer;
3939
private readonly BufferedStream _compressedStream;

Library/DiscUtils.Core/Internal/Crc32BigEndian.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ internal sealed class Crc32BigEndian : Crc32
3434

3535
static Crc32BigEndian()
3636
{
37-
var tables = ImmutableArray.CreateBuilder<ImmutableArray<uint>>(4);
37+
var tables = new ImmutableArray<uint>[4];
3838

3939
tables[(int)Crc32Algorithm.Common] = CalcTable(0x04C11DB7);
4040
tables[(int)Crc32Algorithm.Castagnoli] = CalcTable(0x1EDC6F41);
4141
tables[(int)Crc32Algorithm.Koopman] = CalcTable(0x741B8CD7);
4242
tables[(int)Crc32Algorithm.Aeronautical] = CalcTable(0x814141AB);
4343

44-
Tables = tables.ToImmutable();
44+
Tables = tables.ToImmutableArray();
4545
}
4646

4747
public Crc32BigEndian(Crc32Algorithm algorithm)
@@ -64,7 +64,7 @@ public override void Process(ReadOnlySpan<byte> buffer)
6464

6565
private static ImmutableArray<uint> CalcTable(uint polynomial)
6666
{
67-
var table = ImmutableArray.CreateBuilder<uint>(256);
67+
Span<uint> table = stackalloc uint[256];
6868

6969
for (uint i = 0; i < 256; ++i)
7070
{
@@ -85,7 +85,7 @@ private static ImmutableArray<uint> CalcTable(uint polynomial)
8585
table[(int)i] = crc;
8686
}
8787

88-
return table.ToImmutable();
88+
return table.ToImmutableArray();
8989
}
9090

9191
private static uint Process(ImmutableArray<uint> table, uint accumulator, ReadOnlySpan<byte> buffer)

Library/DiscUtils.Core/Internal/Crc32LittleEndian.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ internal sealed class Crc32LittleEndian : Crc32
3434

3535
static Crc32LittleEndian()
3636
{
37-
var tables = ImmutableArray.CreateBuilder<ImmutableArray<uint>>(4);
37+
var tables = new ImmutableArray<uint>[4];
3838

3939
tables[(int)Crc32Algorithm.Common] = CalcTable(0xEDB88320);
4040
tables[(int)Crc32Algorithm.Castagnoli] = CalcTable(0x82F63B78);
4141
tables[(int)Crc32Algorithm.Koopman] = CalcTable(0xEB31D82E);
4242
tables[(int)Crc32Algorithm.Aeronautical] = CalcTable(0xD5828281);
4343

44-
Tables = tables.ToImmutable();
44+
Tables = tables.ToImmutableArray();
4545
}
4646

4747
public Crc32LittleEndian(Crc32Algorithm algorithm)
@@ -64,9 +64,8 @@ public override void Process(ReadOnlySpan<byte> buffer)
6464

6565
private static ImmutableArray<uint> CalcTable(uint polynomial)
6666
{
67-
var table = ImmutableArray.CreateBuilder<uint>(256);
67+
Span<uint> table = stackalloc uint[256];
6868

69-
table[0] = 0;
7069
for (uint i = 0; i <= 255; ++i)
7170
{
7271
var crc = i;
@@ -86,7 +85,7 @@ private static ImmutableArray<uint> CalcTable(uint polynomial)
8685
table[(int)i] = crc;
8786
}
8887

89-
return table.ToImmutable();
88+
return table.ToImmutableArray();
9089
}
9190

9291
private static uint Process(ImmutableArray<uint> table, uint accumulator, ReadOnlySpan<byte> buffer)

Library/DiscUtils.Core/Internal/Utilities.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using System;
2727
using System.Collections.Concurrent;
2828
using System.Collections.Generic;
29+
using System.Collections.Immutable;
2930
using System.IO;
3031
using System.Linq;
3132
using System.Runtime.CompilerServices;
@@ -563,4 +564,12 @@ public static UnixFileType ToUnixFileType(this TarFileType entryType)
563564
}
564565

565566
#endregion
567+
568+
#if NETFRAMEWORK && !NET462_OR_GREATER
569+
public static ImmutableArray<T> ToImmutableArray<T>(this ReadOnlySpan<T> source)
570+
=> ImmutableArray.Create(source.ToArray());
571+
572+
public static ImmutableArray<T> ToImmutableArray<T>(this Span<T> source)
573+
=> ImmutableArray.Create(source.ToArray());
574+
#endif
566575
}

Tests/LibraryTests/ThreadSafeStreamTest.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,7 @@ public void Dispose_StopsView()
134134
var altView = tss.OpenView();
135135
tss.Dispose();
136136

137-
try
138-
{
139-
altView.ReadByte();
140-
Assert.Fail("Disposed stream didn't stop view");
141-
}
142-
catch (ObjectDisposedException)
143-
{
144-
}
137+
Assert.Throws<ObjectDisposedException>(() => altView.ReadByte());
145138
}
146139

147140
[Fact]

0 commit comments

Comments
 (0)