Skip to content

Commit

Permalink
Remove lingering uses of non disposing stream
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed May 8, 2018
1 parent 2919ec2 commit 3f94c1a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/SharpCompress/IO/NonDisposingStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SharpCompress.IO
{
internal class NonDisposingStream : Stream
public class NonDisposingStream : Stream
{
public NonDisposingStream(Stream stream, bool throwOnDispose = false)
{
Expand All @@ -15,6 +15,7 @@ public NonDisposingStream(Stream stream, bool throwOnDispose = false)

protected override void Dispose(bool disposing)
{
GC.SuppressFinalize(this);
if (ThrowOnDispose)
{
throw new InvalidOperationException();
Expand Down
4 changes: 0 additions & 4 deletions src/SharpCompress/Readers/IReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ public static void WriteEntryToFile(this IReader reader, string destinationFileN
using (FileStream fs = File.Open(destinationFileName, fm))
{
reader.WriteEntryTo(fs);
//using (Stream s = reader.OpenEntryStream())
//{
// s.TransferTo(fs);
//}
}
reader.Entry.PreserveExtractionOptions(destinationFileName, options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Readers/Zip/ZipReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ZipReader : AbstractReader<ZipEntry, ZipVolume>
{
private readonly StreamingZipHeaderFactory _headerFactory;

internal ZipReader(Stream stream, ReaderOptions options)
private ZipReader(Stream stream, ReaderOptions options)
: base(options, ArchiveType.Zip)
{
Volume = new ZipVolume(stream, options);
Expand Down
12 changes: 6 additions & 6 deletions src/SharpCompress/Writers/AbstractWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SharpCompress.Writers
{
public abstract class AbstractWriter : IWriter
{
private bool isDisposed;
private bool _isDisposed;

protected AbstractWriter(ArchiveType type, WriterOptions writerOptions)
{
Expand All @@ -29,28 +29,28 @@ protected void InitalizeStream(Stream stream)

protected virtual void Dispose(bool isDisposing)
{
if (isDisposing && !WriterOptions.LeaveStreamOpen)
if (isDisposing)
{
OutputStream.Dispose();
}
}

public void Dispose()
{
if (!isDisposed)
if (!_isDisposed)
{
GC.SuppressFinalize(this);
Dispose(true);
isDisposed = true;
_isDisposed = true;
}
}

~AbstractWriter()
{
if (!isDisposed)
if (!_isDisposed)
{
Dispose(false);
isDisposed = true;
_isDisposed = true;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/SharpCompress.Test/ReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ protected void Read(IEnumerable<string> testArchives, CompressionType expectedCo
foreach (var path in testArchives)
{
using (var stream = new NonDisposingStream(new ForwardOnlyStream(File.OpenRead(path)), true))
using (var reader = ReaderFactory.Open(stream))
using (var reader = ReaderFactory.Open(stream, new ReaderOptions()
{
LeaveStreamOpen = true
}))
{
UseReader(this, reader, expectedCompression);
stream.ThrowOnDispose = false;
Expand Down
58 changes: 13 additions & 45 deletions tests/SharpCompress.Test/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,26 @@ namespace SharpCompress.Test
{
public class TestBase
{
protected string SOLUTION_BASE_PATH = null;
private string SOLUTION_BASE_PATH;
protected string TEST_ARCHIVES_PATH;
protected string ORIGINAL_FILES_PATH;
protected string MISC_TEST_FILES_PATH;
public string SCRATCH_FILES_PATH;
protected string SCRATCH2_FILES_PATH;
protected IEnumerable<string> GetRarArchives()
{
yield return Path.Combine(TEST_ARCHIVES_PATH, "Rar.none.rar");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Rar.rar");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Rar.solid.rar");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Rar.multi.part01.rar");
}
protected IEnumerable<string> GetZipArchives()
{
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.dd.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd-.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate64.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.lzma.dd.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.lzma.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.none.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.ppmd.dd.zip");
yield return Path.Combine(TEST_ARCHIVES_PATH, "Zip.ppmd.zip");
}
protected IEnumerable<string> GetTarArchives()
{
yield return Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar");
}
protected IEnumerable<string> GetTarBz2Archives()
{
yield return Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.bz2");
}
protected IEnumerable<string> GetTarGzArchives()


public TestBase()
{
yield return Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz");
}
var index = AppDomain.CurrentDomain.BaseDirectory.IndexOf("SharpCompress.Test", StringComparison.OrdinalIgnoreCase);
SOLUTION_BASE_PATH = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.Substring(0, index));

TEST_ARCHIVES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "Archives");
ORIGINAL_FILES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "Original");
MISC_TEST_FILES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "MiscTest");
SCRATCH_FILES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "Scratch");
SCRATCH2_FILES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "Scratch2");
}

public void ResetScratch()
{
if (Directory.Exists(SCRATCH_FILES_PATH))
Expand Down Expand Up @@ -271,17 +251,5 @@ protected bool IsFileLocked(FileInfo file)
//file is not locked
return false;
}

public TestBase()
{
var index = AppDomain.CurrentDomain.BaseDirectory.IndexOf("SharpCompress.Test", StringComparison.OrdinalIgnoreCase);
SOLUTION_BASE_PATH = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.Substring(0, index));

TEST_ARCHIVES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "Archives");
ORIGINAL_FILES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "Original");
MISC_TEST_FILES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "MiscTest");
SCRATCH_FILES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "Scratch");
SCRATCH2_FILES_PATH = Path.Combine(SOLUTION_BASE_PATH, "TestArchives", "Scratch2");
}
}
}
3 changes: 2 additions & 1 deletion tests/SharpCompress.Test/Zip/ZipReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using SharpCompress.Common;
using SharpCompress.IO;
using SharpCompress.Readers;
using SharpCompress.Readers.Zip;
using SharpCompress.Writers;
Expand Down Expand Up @@ -295,7 +296,7 @@ public void TestSharpCompressWithEmptyStream()
stream = new MemoryStream(stream.ToArray());
File.WriteAllBytes(Path.Combine(SCRATCH_FILES_PATH, "foo.zip"), stream.ToArray());

using (IReader zipReader = ZipReader.Open(stream))
using (IReader zipReader = ZipReader.Open(new NonDisposingStream(stream, true)))
{
while (zipReader.MoveToNextEntry())
{
Expand Down

0 comments on commit 3f94c1a

Please sign in to comment.