Merge pull request #69 from pnewman8/master

Skip entry stream on dispose
This commit is contained in:
Adam Hathcock
2015-07-20 16:27:55 +01:00
5 changed files with 40 additions and 7 deletions

View File

@@ -236,7 +236,7 @@ namespace SharpCompress.Archive.SevenZip
protected override EntryStream GetEntryStream()
{
return new EntryStream(new ReadOnlySubStream(currentStream, currentItem.Size));
return CreateEntryStream(new ReadOnlySubStream(currentStream, currentItem.Size));
}
}
}

View File

@@ -1,16 +1,19 @@
using System;
using System.IO;
using SharpCompress.Reader;
namespace SharpCompress.Common
{
public class EntryStream : Stream
{
public IReader Reader { get; private set; }
private Stream stream;
private bool completed;
private bool isDisposed;
internal EntryStream(Stream stream)
internal EntryStream(IReader reader, Stream stream)
{
this.Reader = reader;
this.stream = stream;
}
@@ -28,10 +31,9 @@ namespace SharpCompress.Common
protected override void Dispose(bool disposing)
{
if (!completed)
if (!(completed || Reader.Cancelled))
{
throw new InvalidOperationException(
"EntryStream has not been fully consumed. Read the entire stream or use SkipEntry.");
SkipEntry();
}
if (isDisposed)
{

View File

@@ -67,12 +67,32 @@ namespace SharpCompress.Reader
#endregion
public bool Cancelled { get; private set; }
/// <summary>
/// Indicates that the remaining entries are not required.
/// On dispose of an EntryStream, the stream will not skip to the end of the entry.
/// An attempt to move to the next entry will throw an exception, as the compressed stream is not positioned at an entry boundary.
/// </summary>
public void Cancel()
{
if (!completed)
{
Cancelled = true;
}
}
public bool MoveToNextEntry()
{
if (completed)
{
return false;
}
if (Cancelled)
{
throw new InvalidOperationException("Reader has been cancelled.");
}
if (entriesForCurrentReadStream == null)
{
return LoadStreamForReading(RequestInitialStream());
@@ -197,9 +217,17 @@ namespace SharpCompress.Reader
return stream;
}
/// <summary>
/// Retains a reference to the entry stream, so we can check whether it completed later.
/// </summary>
protected EntryStream CreateEntryStream(Stream decompressed)
{
return new EntryStream(this, decompressed);
}
protected virtual EntryStream GetEntryStream()
{
return new EntryStream(Entry.Parts.First().GetCompressedStream());
return CreateEntryStream(Entry.Parts.First().GetCompressedStream());
}
#endregion

View File

@@ -22,6 +22,9 @@ namespace SharpCompress.Reader
/// <param name="writableStream"></param>
void WriteEntryTo(Stream writableStream);
bool Cancelled { get; }
void Cancel();
/// <summary>
/// Moves to the next entry by reading more data from the underlying stream. This skips if data has not been read.
/// </summary>

View File

@@ -73,7 +73,7 @@ namespace SharpCompress.Reader.Rar
protected override EntryStream GetEntryStream()
{
return new EntryStream(new RarStream(pack, Entry.FileHeader,
return CreateEntryStream(new RarStream(pack, Entry.FileHeader,
new MultiVolumeReadOnlyStream(
CreateFilePartEnumerableForCurrentEntry().Cast<RarFilePart>(), this)));
}