Ejemplo n.º 1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_decStream == null)
            {
                throw new Exception("Decoder stream is null");
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "Need non-negitive number");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "Need non-negitive number");
            }
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException("Invalid offset and length");
            }

            int startOffset = offset;

            while (count > 0)
            {
                if (_currentStream == _streamsBytes.Length)
                {
                    // EOF
                    break;
                }
                int toRead;
                if (_streamSkipBytes[_currentStream] != 0)
                {
                    byte[] skipBytes = new byte[_streamSkipBytes[_currentStream] < 0x8000 ? _streamSkipBytes[_currentStream] : 0x8000];
                    while (_streamSkipBytes[_currentStream] > 0)
                    {
                        toRead = _streamSkipBytes[_currentStream] < skipBytes.Length ? _streamSkipBytes[_currentStream] : skipBytes.Length;
                        if (_decStream.Read(skipBytes, 0, toRead) != toRead)
                        {
                            throw new Exception("Bad stream, read unexcepted amount of samples");
                        }
                        _streamSkipBytes[_currentStream] -= toRead;
                    }
                }
                toRead = (count < _streamsBytes[_currentStream]) ? count : _streamsBytes[_currentStream];
                if (_decStream.Read(buffer, offset, toRead) != toRead)
                {
                    throw new Exception("Bad stream, read unexcepted amount of samples");
                }
                _streamsBytes[_currentStream] -= toRead;
                count  -= toRead;
                offset += toRead;
                if (_streamsBytes[_currentStream] == 0)
                {
                    // empty the current stream
                    byte[] skipBytes = new byte[0x8000];
                    while (_decStream.Read(skipBytes, 0, skipBytes.Length) != 0)
                    {
                        ;
                    }
                    ++_currentStream;
                    _rawStream.NextStream();
                }
            }

            return(offset - startOffset);
        }