示例#1
0
        public void CopyTo(BitwiseStream destination, int bufferSize)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            if (!CanRead)
            {
                throw new NotSupportedException("This stream does not support reading");
            }
            if (!destination.CanWrite)
            {
                throw new NotSupportedException("This destination stream does not support writing");
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize");
            }

            var buffer = new byte[bufferSize];
            int nread;

            while ((nread = Read(buffer, 0, bufferSize)) != 0)
            {
                destination.Write(buffer, 0, nread);
            }

            ulong bits;

            nread = ReadBits(out bits, 7);
            destination.WriteBits(bits, nread);
        }
示例#2
0
        public BitWriter(BitwiseStream stream, bool leaveOpen)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            BaseStream     = stream;
            this.leaveOpen = leaveOpen;
        }
示例#3
0
        /// <summary>
        /// Reports the position of the first occurrence of the specified BitStream in this
        /// instance. The search starts at a specified BitStream position.
        /// </summary>
        /// <param name="value">The BitStream to seek.</param>
        /// <param name="offsetBits">The search starting position.</param>
        /// <returns>
        /// The zero-based index position of value if that BitStream is found, or -1 if it is not.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="value"/> is <null/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="offsetBits"/> specifies a position not within this instance.</exception>
        public long IndexOf(BitwiseStream value, long offsetBits)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (value.LengthBits % 8 != 0)
            {
                throw new ArgumentOutOfRangeException("value");
            }
            if (offsetBits < 0 || offsetBits > LengthBits)
            {
                throw new ArgumentOutOfRangeException("offsetBits");
            }

            var  needle = new byte[value.Length];
            long pos    = value.PositionBits;

            value.SeekBits(0, SeekOrigin.Begin);
            int len = value.Read(needle, 0, needle.Length);

            System.Diagnostics.Debug.Assert(len == needle.Length);
            value.SeekBits(pos, SeekOrigin.Begin);

            pos = PositionBits;
            SeekBits(offsetBits, SeekOrigin.Begin);

            int  idx = 0;
            long ret = -1;
            long end = (LengthBits - PositionBits) / 8;

            for (long i = 0; i < end; ++i)
            {
                int b = ReadByte();

                if (b != needle[idx])
                {
                    SeekBits(idx * -8, SeekOrigin.Current);
                    i  -= idx;
                    idx = 0;
                }
                else if (++idx == needle.Length)
                {
                    ret = 8 * (i - idx + 1) + offsetBits;
                    break;
                }
            }

            SeekBits(pos, SeekOrigin.Begin);
            return(ret);
        }
示例#4
0
 public BitWriter(BitwiseStream stream)
     : this(stream, false)
 {
 }
示例#5
0
 public BitReader(BitwiseStream stream)
     : this(stream, false)
 {
 }
示例#6
0
 public void CopyTo(BitwiseStream destination)
 {
     CopyTo(destination, BlockCopySize);
 }