/Utilities/Compression/PendingBuffer.cs
C# | 270 lines | 159 code | 27 blank | 84 comment | 6 complexity | b059631fdefa7b64eb69d3b5a1d7b994 MD5 | raw file
Possible License(s): Apache-2.0
- // Based on Mike Krueger's SharpZipLib, Copyright (C) 2001 (GNU license).
- // Authors of the original java version: Jochen Hoenicke, John Leuner
- // See http://www.ISeeSharpCode.com for more information.
-
- using System;
-
- namespace Delta.Utilities.Compression
- {
- /// <summary>
- /// This class is general purpose class for writing data to a buffer.
- /// It allows you to write bits as well as bytes.
- /// </summary>
- public class PendingBuffer
- {
- #region BitCount (Public)
- /// <summary>
- /// Bit count
- /// </summary>
- /// <returns>Int</returns>
- public int BitCount
- {
- get
- {
- return bitCount;
- } // get
- }
- #endregion
-
- #region IsFlushed (Public)
- /// <summary>
- /// Is this buffer already flushed?
- /// </summary>
- /// <returns>True if we have flushed to the end already.</returns>
- public bool IsFlushed
- {
- get
- {
- return end == 0;
- } // get
- }
- #endregion
-
- #region Protected
-
- #region buf (Protected)
- /// <summary>
- /// Buffer used for pending data.
- /// </summary>
- protected byte[] buf;
- #endregion
-
- #endregion
-
- #region Private
-
- #region start (Private)
- /// <summary>
- /// Start
- /// </summary>
- private int start;
- #endregion
-
- #region end (Private)
- /// <summary>
- /// End
- /// </summary>
- private int end;
- #endregion
-
- #region bits (Private)
- /// <summary>
- /// Bits
- /// </summary>
- private uint bits;
- #endregion
-
- #region bitCount (Private)
- /// <summary>
- /// Bit count
- /// </summary>
- private int bitCount;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create pending buffer
- /// </summary>
- public PendingBuffer()
- : this(4096)
- {
- }
-
- // PendingBuffer()
-
- /// <summary>
- /// Create pending buffer
- /// </summary>
- /// <param name="bufsize">Bufsize</param>
- public PendingBuffer(int bufsize)
- {
- buf = new byte[bufsize];
- }
- #endregion
-
- #region Reset (Public)
- /// <summary>
- /// Reset
- /// </summary>
- public void Reset()
- {
- start = end = bitCount = 0;
- }
- #endregion
-
- #region WriteByte (Public)
- /// <summary>
- /// Write byte
- /// </summary>
- /// <param name="b">B</param>
- public void WriteByte(int b)
- {
- buf[end++] = (byte)b;
- }
- #endregion
-
- #region WriteShort (Public)
- /// <summary>
- /// Write short
- /// </summary>
- /// <param name="s">S</param>
- public void WriteShort(int s)
- {
- buf[end++] = (byte)s;
- buf[end++] = (byte)(s >> 8);
- }
- #endregion
-
- #region WriteInt (Public)
- /// <summary>
- /// Write int
- /// </summary>
- /// <param name="s">S</param>
- public void WriteInt(int s)
- {
- buf[end++] = (byte)s;
- buf[end++] = (byte)(s >> 8);
- buf[end++] = (byte)(s >> 16);
- buf[end++] = (byte)(s >> 24);
- }
- #endregion
-
- #region WriteBlock (Public)
- /// <summary>
- /// Write block
- /// </summary>
- /// <param name="block">Block</param>
- /// <param name="offset">Offset</param>
- /// <param name="len">Len</param>
- public void WriteBlock(byte[] block, int offset, int len)
- {
- Array.Copy(block, offset, buf, end, len);
- end += len;
- }
- #endregion
-
- #region AlignToByte (Public)
- /// <summary>
- /// Align to byte
- /// </summary>
- public void AlignToByte()
- {
- if (bitCount > 0)
- {
- buf[end++] = (byte)bits;
- if (bitCount > 8)
- {
- buf[end++] = (byte)(bits >> 8);
- } // if (bitCount)
- } // if (bitCount)
- bits = 0;
- bitCount = 0;
- }
- #endregion
-
- #region WriteBits (Public)
- /// <summary>
- /// Write bits
- /// </summary>
- /// <param name="b">B</param>
- /// <param name="count">Count</param>
- public void WriteBits(int b, int count)
- {
- bits |= (uint)(b << bitCount);
- bitCount += count;
- if (bitCount >= 16)
- {
- buf[end++] = (byte)bits;
- buf[end++] = (byte)(bits >> 8);
- bits >>= 16;
- bitCount -= 16;
- } // if (bitCount)
- }
- #endregion
-
- #region WriteShortMsb (Public)
- /// <summary>
- /// Write short Msb
- /// </summary>
- /// <param name="s">S</param>
- public void WriteShortMsb(int s)
- {
- buf[end++] = (byte)(s >> 8);
- buf[end++] = (byte)s;
- }
- #endregion
-
- #region Flush (Public)
- /// <summary>
- /// Flushes the pending buffer into the given output array. If the
- /// output array is to small, only a partial flush is done.
- /// </summary>
- /// <param name="output">Output array</param>
- /// <param name="offset">Offset into output array</param>
- /// <param name="length">Length the maximum number of bytes to store
- /// </param>
- /// <exception name="ArgumentOutOfRangeException">
- /// IndexOutOfBoundsException if offset or length are invalid.</exception>
- public int Flush(byte[] output, int offset, int length)
- {
- if (bitCount >= 8)
- {
- buf[end++] = (byte)bits;
- bits >>= 8;
- bitCount -= 8;
- } // if (bitCount)
-
- if (length > end - start)
- {
- length = end - start;
- Array.Copy(buf, start, output, offset, length);
- start = 0;
- end = 0;
- } // if (length)
- else
- {
- Array.Copy(buf, start, output, offset, length);
- start += length;
- } // else
-
- return length;
- }
- #endregion
-
- #region ToByteArray (Public)
- /// <summary>
- /// To byte array
- /// </summary>
- public byte[] ToByteArray()
- {
- byte[] ret = new byte[end - start];
- Array.Copy(buf, start, ret, 0, ret.Length);
- start = 0;
- end = 0;
- return ret;
- }
- #endregion
- }
- }