123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- namespace SevenZip
- {
- using System;
- using System.IO;
- using SevenZip.Sdk.Compression.Lzma;
-
-
-
- public class LzmaEncodeStream : Stream
- {
- private const int MAX_BUFFER_CAPACITY = 1 << 30;
- private readonly MemoryStream _buffer = new MemoryStream();
- private readonly int _bufferCapacity = 1 << 18;
- private readonly bool _ownOutput;
- private bool _disposed;
- private Encoder _lzmaEncoder;
- private Stream _output;
-
-
-
- public LzmaEncodeStream()
- {
- _output = new MemoryStream();
- _ownOutput = true;
- Init();
- }
-
-
-
-
- public LzmaEncodeStream(int bufferCapacity)
- {
- _output = new MemoryStream();
- _ownOutput = true;
- if (bufferCapacity > MAX_BUFFER_CAPACITY)
- {
- throw new ArgumentException("Too large capacity.", "bufferCapacity");
- }
- _bufferCapacity = bufferCapacity;
- Init();
- }
-
-
-
-
- public LzmaEncodeStream(Stream outputStream)
- {
- if (!outputStream.CanWrite)
- {
- throw new ArgumentException("The specified stream can not write.", "outputStream");
- }
- _output = outputStream;
- Init();
- }
-
-
-
-
-
- public LzmaEncodeStream(Stream outputStream, int bufferCapacity)
- {
- if (!outputStream.CanWrite)
- {
- throw new ArgumentException("The specified stream can not write.", "outputStream");
- }
- _output = outputStream;
- if (bufferCapacity > 1 << 30)
- {
- throw new ArgumentException("Too large capacity.", "bufferCapacity");
- }
- _bufferCapacity = bufferCapacity;
- Init();
- }
-
-
-
- public override bool CanRead => false;
-
-
-
- public override bool CanSeek => false;
-
-
-
- public override bool CanWrite
- {
- get
- {
- DisposedCheck();
- return _buffer.CanWrite;
- }
- }
-
-
-
- public override long Length
- {
- get
- {
- DisposedCheck();
- if (_output.CanSeek)
- {
- return _output.Length;
- }
- return _buffer.Position;
- }
- }
-
-
-
- public override long Position
- {
- get
- {
- DisposedCheck();
- if (_output.CanSeek)
- {
- return _output.Position;
- }
- return _buffer.Position;
- }
- set => throw new NotSupportedException();
- }
- private void Init()
- {
- _buffer.Capacity = _bufferCapacity;
- SevenZipCompressor.LzmaDictionarySize = _bufferCapacity;
- _lzmaEncoder = new Encoder();
- SevenZipCompressor.WriteLzmaProperties(_lzmaEncoder);
- }
-
-
-
-
- private void DisposedCheck()
- {
- if (_disposed)
- {
- throw new ObjectDisposedException("SevenZipExtractor");
- }
- }
- private void WriteChunk()
- {
- _lzmaEncoder.WriteCoderProperties(_output);
- long streamSize = _buffer.Position;
- if (_buffer.Length != _buffer.Position)
- {
- _buffer.SetLength(_buffer.Position);
- }
- _buffer.Position = 0;
- for (int i = 0; i < 8; i++)
- {
- _output.WriteByte((byte) (streamSize >> (8*i)));
- }
- _lzmaEncoder.Code(_buffer, _output, -1, -1, null);
- _buffer.Position = 0;
- }
-
-
-
-
- public LzmaDecodeStream ToDecodeStream()
- {
- DisposedCheck();
- Flush();
- return new LzmaDecodeStream(_output);
- }
-
-
-
- public override void Flush()
- {
- DisposedCheck();
- WriteChunk();
- }
-
-
-
- protected override void Dispose(bool disposing)
- {
- if (!_disposed)
- {
- if (disposing)
- {
- Flush();
- _buffer.Close();
- if (_ownOutput)
- {
- _output.Dispose();
- }
- _output = null;
- }
- _disposed = true;
- }
- }
-
-
-
-
-
-
-
- public override int Read(byte[] buffer, int offset, int count)
- {
- DisposedCheck();
- throw new NotSupportedException();
- }
-
-
-
-
-
-
- public override long Seek(long offset, SeekOrigin origin)
- {
- DisposedCheck();
- throw new NotSupportedException();
- }
-
-
-
-
- public override void SetLength(long value)
- {
- DisposedCheck();
- throw new NotSupportedException();
- }
-
-
-
-
-
-
- public override void Write(byte[] buffer, int offset, int count)
- {
- DisposedCheck();
- int dataLength = Math.Min(buffer.Length - offset, count);
- while (_buffer.Position + dataLength >= _bufferCapacity)
- {
- int length = _bufferCapacity - (int) _buffer.Position;
- _buffer.Write(buffer, offset, length);
- offset = length + offset;
- dataLength -= length;
- WriteChunk();
- }
- _buffer.Write(buffer, offset, dataLength);
- }
- }
- }
|