Cancel moved from EntryStream to Reader

Relates to previous commit. Following discussion with Adam, moved the Cancel() to the reader.

Example:

while (reader.MoveToNextEntry())
{
  using (var data = new StreamReader(reader.OpenEntryStream()))
  {
    try
    {
      DoSomething(data.ReadLine());
    }
    catch
    {
      reader.Cancel();
      throw;
    }
  }
}
This commit is contained in:
Paul Newman
2015-07-15 18:13:46 +01:00
parent afff386622
commit 8faebc78d0
3 changed files with 29 additions and 27 deletions

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;
}
@@ -26,25 +29,9 @@ namespace SharpCompress.Common
completed = true;
}
public bool Cancelled { get; private set; }
/// <summary>
/// Indicates that the remainder of the stream is not required.
/// On dispose, the entry will not be skipped, so it helps with efficiency.
/// The downside is that subsequent entries are not usable, as the compressed stream is not positioned at an entry boundary.
/// </summary>
public void Cancel()
{
if (!completed)
{
Cancelled = true;
stream.Close();
}
}
protected override void Dispose(bool disposing)
{
if (!(completed || Cancelled))
if (!(completed || Reader.Cancelled))
{
SkipEntry();
}

View File

@@ -67,22 +67,36 @@ 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());
}
if (currentEntryStream != null && currentEntryStream.Cancelled)
{
throw new InvalidOperationException("EntryStream has not been fully consumed. Read the entire stream or use SkipEntry.");
}
if (!wroteCurrentEntry)
{
SkipEntry();
@@ -203,14 +217,12 @@ namespace SharpCompress.Reader
return stream;
}
private EntryStream currentEntryStream;
/// <summary>
/// Retains a reference to the entry stream, so we can check whether it completed later.
/// </summary>
protected EntryStream CreateEntryStream(Stream decompressed)
{
return currentEntryStream = new EntryStream(decompressed);
return new EntryStream(this, decompressed);
}
protected virtual EntryStream GetEntryStream()

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>