-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
8d16925
commit 9224237
Showing
2 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |