Test with ForwardOnlyStream. RewindableStream shouldn't corrupt a ForwardOnlyStream (#161)

This commit is contained in:
Adam Hathcock 2016-08-12 11:56:49 +01:00 committed by GitHub
parent 3a52c68270
commit bd8ba7b854
4 changed files with 71 additions and 7 deletions

View file

@ -31,7 +31,7 @@ namespace SharpCompress.Common.Zip
FlagUtility.HasFlag(lastEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
{
reader = (lastEntryHeader.Part as StreamingZipFilePart).FixStreamedFileLocation(ref rewindableStream);
long pos = rewindableStream.Position;
long? pos = rewindableStream.CanSeek ? (long?)rewindableStream.Position : null;
uint crc = reader.ReadUInt32();
if (crc == POST_DATA_DESCRIPTOR)
{
@ -40,7 +40,10 @@ namespace SharpCompress.Common.Zip
lastEntryHeader.Crc = crc;
lastEntryHeader.CompressedSize = reader.ReadUInt32();
lastEntryHeader.UncompressedSize = reader.ReadUInt32();
lastEntryHeader.DataStartPosition = pos - lastEntryHeader.CompressedSize;
if (pos.HasValue)
{
lastEntryHeader.DataStartPosition = pos - lastEntryHeader.CompressedSize;
}
}
lastEntryHeader = null;
uint headerBytes = reader.ReadUInt32();

View file

@ -72,10 +72,7 @@ namespace SharpCompress.IO
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanSeek => stream.CanSeek;
public override bool CanWrite
{

View file

@ -0,0 +1,64 @@
using System;
using System.IO;
namespace SharpCompress.Test
{
public class ForwardOnlyStream : Stream
{
private readonly Stream stream;
public bool IsDisposed { get; private set; }
public ForwardOnlyStream(Stream stream)
{
this.stream = stream;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
stream.Dispose();
IsDisposed = true;
}
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override void Flush()
{
throw new NotSupportedException();
}
public override long Length
{
get { throw new NotSupportedException(); }
}
public override long Position
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public override int Read(byte[] buffer, int offset, int count)
{
return stream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{throw new NotSupportedException();
}
}
}

View file

@ -18,7 +18,7 @@ namespace SharpCompress.Test
{
foreach (var path in testArchives)
{
using (Stream stream = File.OpenRead(path))
using (Stream stream = new ForwardOnlyStream(File.OpenRead(path)))
using (IReader reader = ReaderFactory.Open(stream))
{
UseReader(this, reader, expectedCompression);