// QuickLZCompressionStream
// Public domain by Pascal Craponne (picrap@gmail.com). No copyright pending
namespace QuickLZ
{
using System;
using System.IO;
using QuickLZSharp;
///
/// Stream to compress and write back data
///
public class QuickLZCompressionStream : Stream
{
#region overrides without any interested
///
/// Sets the position within the current stream.
///
/// A byte offset relative to the parameter.
/// A value of type indicating the reference point used to obtain the new position.
///
/// The new position within the current stream.
///
///
/// An I/O error occurs.
///
///
/// The stream does not support seeking, such as if the stream is constructed from a pipe or console output.
///
///
/// Methods were called after the stream was closed.
///
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
///
/// Sets the length of the current stream.
///
/// The desired length of the current stream in bytes.
///
/// An I/O error occurs.
///
///
/// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.
///
///
/// Methods were called after the stream was closed.
///
public override void SetLength(long value)
{
throw new NotSupportedException();
}
///
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
///
/// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source.
/// The zero-based byte offset in at which to begin storing the data read from the current stream.
/// The maximum number of bytes to be read from the current stream.
///
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
///
///
/// The sum of and is larger than the buffer length.
///
///
/// is null.
///
///
/// or is negative.
///
///
/// An I/O error occurs.
///
///
/// The stream does not support reading.
///
///
/// Methods were called after the stream was closed.
///
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
///
/// When overridden in a derived class, gets a value indicating whether the current stream supports reading.
///
///
/// true if the stream supports reading; otherwise, false.
///
public override bool CanRead
{
get { return false; }
}
///
/// Gets a value indicating whether the current stream supports seeking.
///
///
/// true if the stream supports seeking; otherwise, false.
///
public override bool CanSeek
{
get { return false; }
}
///
/// Gets a value indicating whether the current stream supports writing.
///
///
/// true if the stream supports writing; otherwise, false.
///
public override bool CanWrite
{
get { return true; }
}
///
/// Gets the length in bytes of the stream.
///
///
///
/// A long value representing the length of the stream in bytes.
///
///
/// A class derived from Stream does not support seeking.
///
///
/// Methods were called after the stream was closed.
///
public override long Length
{
get { throw new NotSupportedException(); }
}
///
/// Gets or sets the position within the current stream.
///
///
///
/// The current position within the stream.
///
///
/// An I/O error occurs.
///
///
/// The stream does not support seeking.
///
///
/// Methods were called after the stream was closed.
///
public override long Position
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
#endregion
///
/// QuickLZ engine instance
///
private readonly QuickLZ _quickLZ;
///
/// The Stream we're writing to
///
private readonly Stream _targetStream;
///
/// Write buffer
///
private readonly byte[] _writeBuffer;
///
/// Current position in write buffer
///
private int _writeBufferOffset;
///
/// Buffer where compressed data is stored
///
private readonly byte[] _compressedBuffer;
///
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
///
///
/// An I/O error occurs.
///
public override void Flush()
{
if (_writeBufferOffset > 0)
{
int compressedLength = _quickLZ.compress(_writeBuffer, _compressedBuffer, _writeBufferOffset);
_targetStream.Write(_compressedBuffer, 0, compressedLength);
_writeBufferOffset = 0;
}
}
///
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
///
/// An array of bytes. This method copies bytes from to the current stream.
/// The zero-based byte offset in at which to begin copying bytes to the current stream.
/// The number of bytes to be written to the current stream.
///
/// The sum of and is greater than the buffer length.
///
///
/// is null.
///
///
/// or is negative.
///
///
/// An I/O error occurs.
///
///
/// The stream does not support writing.
///
///
/// Methods were called after the stream was closed.
///
public override void Write(byte[] buffer, int offset, int count)
{
// we have 3 options here:
// buffer can still be filled --> we fill
// buffer is full --> we flush
// buffer is overflood --> we flush and refill
// 1. there is enough room, the buffer is not full
int writeLength = _writeBufferOffset + count;
if (writeLength <= _writeBuffer.Length)
{
Buffer.BlockCopy(buffer, offset, _writeBuffer, _writeBufferOffset, count);
_writeBufferOffset += count;
// 2. same size: write
if (_writeBufferOffset == _writeBuffer.Length)
Flush();
}
// 3. buffer overflow: we split
else
{
int lengthToCauseFlush = _writeBuffer.Length - _writeBufferOffset;
// this first Write will cause a flush
Write(buffer, offset, lengthToCauseFlush);
// this one will refill
Write(buffer, offset + lengthToCauseFlush, count - lengthToCauseFlush);
}
}
///
/// Releases the unmanaged resources used by the and optionally releases the managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected override void Dispose(bool disposing)
{
if (disposing)
{
Flush();
Pool.Default.Free(_quickLZ);
}
base.Dispose(disposing);
_targetStream.Dispose();
}
///
/// Initializes a new instance of the class.
///
/// The target stream.
/// The write buffer.
/// The compression buffer.
public QuickLZCompressionStream(Stream targetStream, byte[] writeBuffer, byte[] compressionBuffer)
{
_quickLZ = Pool.Default.Alloc();
_targetStream = targetStream;
_writeBuffer = writeBuffer;
_compressedBuffer = compressionBuffer;
}
///
/// Initializes a new instance of the class.
///
/// The target.
/// Size of the buffer.
public QuickLZCompressionStream(Stream targetStream, int bufferSize)
: this(targetStream, new byte[bufferSize], new byte[bufferSize + 400])
{
}
///
/// Initializes a new instance of the class.
///
/// The target.
public QuickLZCompressionStream(Stream targetStream)
: this(targetStream, 1 << 20) // Yes, this is 1 MB
{
}
}
}