Esempio n. 1
0
 public override void Write(byte[] buffer, int offset, int count)
 {
     _stream.Write(buffer, offset, count);
     if (count == 0)
     {
         return;
     }
     try
     {
         long position = _stream.Position;
         if (position == _position + count)
         {
             if (count > _buffer.Capacity - _buffer.Count)
             {
                 _buffer.RemoveRange(0, Comparable.Min(count - (_buffer.Capacity - _buffer.Count), _buffer.Count));
             }
             _buffer.AddRange(buffer.Skip(offset + Comparable.Max(count - _buffer.Capacity, 0)).Take(Comparable.Min(count, _buffer.Capacity - _buffer.Count)));
             _position = position;
             _offset   = _buffer.Count;
         }
     }
     catch (NotSupportedException)
     {
     }
 }
Esempio n. 2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            try
            {
                long position = _stream.Position;
                if (position != _position - (_buffer.Count - _offset))
                {
                    _buffer.Clear();
                    _offset   = _buffer.Count;
                    _position = position;
                }
            }
            catch (NotSupportedException)
            {
            }
            int cached = Comparable.Min(count, _buffer.Count - _offset);

            if (cached > 0)
            {
                //buffer.MoveRange();
                buffer.Fill(i => _buffer[_offset + i], new Range(offset, cached));
            }
            int read = count - cached;

            if (read > 0)
            {
                read = _stream.Read(buffer, offset, read);
                if (read > _buffer.Capacity - _buffer.Count)
                {
                    _buffer.RemoveRange(0, Comparable.Min(read - (_buffer.Capacity - _buffer.Count), _buffer.Count));
                }
                _buffer.AddRange(buffer.Skip(offset + cached + Comparable.Max(0, read - (_buffer.Capacity - _buffer.Count))).Take(Comparable.Min(read, _buffer.Capacity - _buffer.Count)));
            }
            return(cached + read);
        }
Esempio n. 3
0
        public static int Compare(this Stream xStream, Stream yStream, long count, int buffSize, Comparison <byte> comparison)
        {
            if (xStream == null)
            {
                throw new ArgumentNullException("xStream");
            }
            if (yStream == null)
            {
                throw new ArgumentNullException("yStream");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            if (buffSize <= 0)
            {
                throw new ArgumentOutOfRangeException("buffSize");
            }
            if (comparison == null)
            {
                comparison = Comparer <byte> .Default.Compare;
            }

            byte[] xBuffer = new byte[buffSize];
            byte[] yBuffer = new byte[buffSize];
            long   total = 0;
            int    xRead, yRead, result;

            do
            {
                xRead  = xStream.Read(xBuffer, 0, (int)Comparable.Min(xBuffer.Length, count - total));
                yRead  = yStream.Read(yBuffer, 0, (int)Comparable.Min(yBuffer.Length, count - total));
                result = xBuffer.Take(xRead).SequenceCompare(yBuffer.Take(yRead), comparison);
                if (result == 0)
                {
                    total += Comparable.Max(xRead, yRead);
                }
            }while (result == 0 && xRead > 0 && yRead > 0 && total < count);
            return(result);
        }