Example #1
0
        public void WriteBlock(Stream streamIn, long length, int bufferSize = 81920)
        {
            if (streamIn == null)
            {
                throw new ArgumentNullException(nameof(streamIn));
            }
            if (!streamIn.CanRead)
            {
                throw new ArgumentException("streamIn must be readable", nameof(streamIn));
            }
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), "length must be 0 or larger");
            }
            if (bufferSize < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize), "bufferSize must be 1 or larger");
            }

            //Write the length of the block
            byte[] bytes = BitConverter.GetBytes(length);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }
            streamOut.Write(bytes, 0, sizeof(long));

            //Write the data
            using (Stream limitedReader = new MultiBlockLimitedStreamReader(streamIn, length))
            {
                limitedReader.CopyTo(streamOut, bufferSize);
            }
        }
        public Stream NextBlockAsStream()
        {
            if (activeBlock != null && activeBlock.Position != activeBlock.Length)
            {
                throw new InvalidOperationException("Unable to retrieve the next block untill the previous block has been fully read");
            }

            byte[] bytes = new byte[sizeof(long)];
            streamIn.Read(bytes, 0, sizeof(long));
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            long length = BitConverter.ToInt64(bytes, 0);

            activeBlock = new MultiBlockLimitedStreamReader(streamIn, length);

            return(activeBlock);
        }