Library.Io.UniteStream.Read C# (CSharp) Method

Read() public method

public Read ( byte buffer, int offset, int count ) : int
buffer byte
offset int
count int
return int
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_disposed) throw new ObjectDisposedException(this.GetType().FullName);
            if (offset < 0 || buffer.Length < offset) throw new ArgumentOutOfRangeException(nameof(offset));
            if (count < 0 || (buffer.Length - offset) < count) throw new ArgumentOutOfRangeException(nameof(count));

            count = (int)Math.Min(count, this.Length - this.Position);
            if (count == 0) return 0;

            int index = 0;
            long position = 0;

            for (long p = 0; index < _streams.Count; index++)
            {
                p += _streams[index].Length;

                if (this.Position < p)
                {
                    position = _streams[index].Length - (p - this.Position);
                    break;
                }
            }

            int readSumLength = 0;

            for (; count > 0 && index < _streams.Count; index++)
            {
                var length = (int)Math.Min(_streams[index].Length - position, count);

                _streams[index].Seek(position, SeekOrigin.Begin);
                position = 0;

                int readLength = 0;

                while (length > 0 && (readLength = _streams[index].Read(buffer, offset, length)) > 0)
                {
                    offset += readLength;
                    length -= readLength;
                    count -= readLength;
                    readSumLength += readLength;
                }
            }

            this.Position += readSumLength;
            return readSumLength;
        }

Usage Example

        public void Test_UniteStream()
        {
            using (MemoryStream memoryStream1 = new MemoryStream(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }))
            using (MemoryStream memoryStream2 = new MemoryStream(new byte[] { 8, 9, 10, 11, 12, 13, 14 }))
            {
                using (UniteStream addStream = new UniteStream(new RangeStream(memoryStream1, 2, 4), new RangeStream(memoryStream2, 2, 4)))
                {
                    byte[] buffer1 = new byte[2];
                    addStream.Read(buffer1, 0, buffer1.Length);
                    Assert.IsTrue(CollectionUtilities.Equals(new byte[] { 2, 3 }, buffer1), "UniteStream #1");

                    byte[] buffer2 = new byte[2];
                    addStream.Read(buffer2, 0, buffer2.Length);
                    Assert.IsTrue(CollectionUtilities.Equals(new byte[] { 4, 5 }, buffer2), "UniteStream #2");

                    byte[] buffer3 = new byte[2];
                    addStream.Read(buffer3, 0, buffer3.Length);
                    Assert.IsTrue(CollectionUtilities.Equals(new byte[] { 10, 11 }, buffer3), "UniteStream #3");

                    byte[] buffer4 = new byte[2];
                    addStream.Read(buffer4, 0, buffer4.Length);
                    Assert.IsTrue(CollectionUtilities.Equals(new byte[] { 12, 13 }, buffer4), "UniteStream #4");
                }
            }
        }
All Usage Examples Of Library.Io.UniteStream::Read