Skip to content

Commit

Permalink
Fix for issue adamhathcock#73
Browse files Browse the repository at this point in the history
7z archives may require alternating reads from multiple substreams so it is important to seek before reading from the underlying stream. To keep performance at an acceptable level it is necessary to perform buffering because seeking on every single one-byte-read will destroy performance.
  • Loading branch information
weltkante committed Jul 20, 2016
1 parent 8d16925 commit f334786
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/SharpCompress/Compressor/LZMA/DecoderStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ internal static Stream CreateDecoderStream(Stream inStream, long startPos, long[
Stream[] inStreams = new Stream[folderInfo.PackStreams.Count];
for (int j = 0; j < folderInfo.PackStreams.Count; j++)
{
inStreams[j] = new ReadOnlySubStream(inStream, startPos, packSizes[j]);
inStreams[j] = new BufferedSubStream(inStream, startPos, packSizes[j]);
startPos += packSizes[j];
}

Expand Down
105 changes: 105 additions & 0 deletions src/SharpCompress/IO/BufferedSubStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.IO;

namespace SharpCompress.IO
{
internal class BufferedSubStream : Stream
{
private long position;
private int cacheOffset;
private int cacheLength;
private byte[] cache;

public BufferedSubStream(Stream stream, long origin, long bytesToRead)
{
Stream = stream;
position = origin;
BytesLeftToRead = bytesToRead;
cache = new byte[32 << 10];
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
//Stream.Dispose();
}
}

private long BytesLeftToRead { get; set; }

public Stream Stream { get; private set; }

public override bool CanRead
{
get { return true; }
}

public override bool CanSeek
{
get { return false; }
}

public override bool CanWrite
{
get { return false; }
}

public override void Flush()
{
throw new System.NotSupportedException();
}

public override long Length
{
get { throw new System.NotSupportedException(); }
}

public override long Position
{
get { throw new System.NotSupportedException(); }
set { throw new System.NotSupportedException(); }
}

public override int Read(byte[] buffer, int offset, int count)
{
if (count > BytesLeftToRead)
count = (int)BytesLeftToRead;

if (count > 0)
{
if (cacheLength == 0)
{
cacheOffset = 0;
Stream.Position = position;
cacheLength = Stream.Read(cache, 0, cache.Length);
position += cacheLength;
}

if (count > cacheLength)
count = cacheLength;

System.Buffer.BlockCopy(cache, cacheOffset, buffer, offset, count);
cacheOffset += count;
cacheLength -= count;
BytesLeftToRead -= count;
}

return count;
}

public override long Seek(long offset, SeekOrigin origin)
{
throw new System.NotSupportedException();
}

public override void SetLength(long value)
{
throw new System.NotSupportedException();
}

public override void Write(byte[] buffer, int offset, int count)
{
throw new System.NotSupportedException();
}
}
}

0 comments on commit f334786

Please sign in to comment.