123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
-
- using System;
- using System.IO;
- using SevenZip.Sdk.Compression.Lzma;
- namespace SevenZip
- {
- #if LZMA_STREAM
-
-
-
- public class LzmaDecodeStream : Stream
- {
- private readonly MemoryStream _buffer = new MemoryStream();
- private readonly Decoder _decoder = new Decoder();
- private readonly Stream _input;
- private byte[] _commonProperties;
- private bool _error;
- private bool _firstChunkRead;
-
-
-
-
- public LzmaDecodeStream(Stream encodedStream)
- {
- if (!encodedStream.CanRead)
- {
- throw new ArgumentException("The specified stream can not read.", "encodedStream");
- }
- _input = encodedStream;
- }
-
-
-
- public int ChunkSize
- {
- get
- {
- return (int) _buffer.Length;
- }
- }
-
-
-
- public override bool CanRead
- {
- get
- {
- return true;
- }
- }
-
-
-
- public override bool CanSeek
- {
- get
- {
- return false;
- }
- }
-
-
-
- public override bool CanWrite
- {
- get
- {
- return false;
- }
- }
-
-
-
- public override long Length
- {
- get
- {
- if (_input.CanSeek)
- {
- return _input.Length;
- }
- return _buffer.Length;
- }
- }
-
-
-
- public override long Position
- {
- get
- {
- if (_input.CanSeek)
- {
- return _input.Position;
- }
- return _buffer.Position;
- }
- set
- {
- throw new NotSupportedException();
- }
- }
- private void ReadChunk()
- {
- long size;
- byte[] properties;
- try
- {
- properties = SevenZipExtractor.GetLzmaProperties(_input, out size);
- }
- catch (LzmaException)
- {
- _error = true;
- return;
- }
- if (!_firstChunkRead)
- {
- _commonProperties = properties;
- }
- if (_commonProperties[0] != properties[0] ||
- _commonProperties[1] != properties[1] ||
- _commonProperties[2] != properties[2] ||
- _commonProperties[3] != properties[3] ||
- _commonProperties[4] != properties[4])
- {
- _error = true;
- return;
- }
- if (_buffer.Capacity < (int) size)
- {
- _buffer.Capacity = (int) size;
- }
- _buffer.SetLength(size);
- _decoder.SetDecoderProperties(properties);
- _buffer.Position = 0;
- _decoder.Code(
- _input, _buffer, 0, size, null);
- _buffer.Position = 0;
- }
-
-
-
- public override void Flush() {}
-
-
-
-
-
-
-
- public override int Read(byte[] buffer, int offset, int count)
- {
- if (_error)
- {
- return 0;
- }
- if (!_firstChunkRead)
- {
- ReadChunk();
- _firstChunkRead = true;
- }
- int readCount = 0;
- while (count > _buffer.Length - _buffer.Position && !_error)
- {
- var buf = new byte[_buffer.Length - _buffer.Position];
- _buffer.Read(buf, 0, buf.Length);
- buf.CopyTo(buffer, offset);
- offset += buf.Length;
- count -= buf.Length;
- readCount += buf.Length;
- ReadChunk();
- }
- if (!_error)
- {
- _buffer.Read(buffer, offset, count);
- readCount += count;
- }
- return readCount;
- }
-
-
-
-
-
-
- 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();
- }
- }
- #endif
- }
|