﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReadableDataStorage.cs" company="Microsoft Corporation">
//   Copyright (c) 2008, 2009, 2010 All Rights Reserved, Microsoft Corporation
//
//   This source is subject to the Microsoft Permissive License.
//   Please see the License.txt file for more information.
//   All other rights reserved.
//
//   THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
//   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//   PARTICULAR PURPOSE.
// </copyright>
// <summary>
//   Base class for data storage objects which support read operations.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Exchange.Data.Internal
{
    using System;
    using System.IO;

    using Strings = CtsResources.SharedStrings;

    /// <summary>
    /// Base class for data storage objects which support read operations.
    /// </summary>
    internal abstract class ReadableDataStorage : DataStorage
    {
        /// <summary>
        /// Gets the length of the storage buffer.
        /// </summary>
        /// <value>The length.</value>
        public abstract long Length
        {
            get;
        }

        /// <summary>
        /// Reads from the storage at the specified position.
        /// </summary>
        /// <param name="position">The position to start reading at.</param>
        /// <param name="buffer">The buffer to read into.</param>
        /// <param name="offset">The offset to read into.</param>
        /// <param name="count">The number of bytes to read .</param>
        /// <returns>The actual number of bytes read.</returns>
        public abstract int Read(long position, byte[] buffer, int offset, int count);

        /// <summary>
        /// Opens the read stream.
        /// </summary>
        /// <param name="start">The start position of the read stream.</param>
        /// <param name="end">The end position of the read stream.</param>
        /// <returns>The stream as a read-only stream</returns>
        public override Stream OpenReadStream(long start, long end)
        {
            return new StreamOnReadableDataStorage(this, start, end);
        }

        /// <summary>
        /// Copies content into the storage stream.
        /// </summary>
        /// <param name="start">The start position to copy from.</param>
        /// <param name="end">The end position to copy to.</param>
        /// <param name="destStream">The destination stream.</param>
        /// <param name="scratchBuffer">The scratch buffer to use.</param>
        /// <returns>The number of bytes copied.</returns>
        public override long CopyContentToStream(
                            long start,
                            long end,
                            Stream destStream,
                            ref byte[] scratchBuffer)
        {
            if (scratchBuffer == null || scratchBuffer.Length < 4096)
            {
                scratchBuffer = new byte[4096];
            }

            long written = 0;
            long remaining = end == long.MaxValue ? long.MaxValue : end - start;

            while (remaining != 0)
            {
                int toRead = (int)Math.Min(remaining, (long)scratchBuffer.Length);

                int read = this.Read(start, scratchBuffer, 0, toRead);
                if (0 == read)
                {
                    break;
                }

                start += read;

                destStream.Write(scratchBuffer, 0, read);

                written += read;

                if (remaining != long.MaxValue)
                {
                    remaining -= read;
                }
            }

            return written;
        }
    }
}