// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReadableWritableDataStorage.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 and write operations.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Exchange.Data.Internal
{
    /// <summary>
    /// Base class for data storage objects which support read and write operations.
    /// </summary>
    internal abstract class ReadableWritableDataStorage : ReadableDataStorage
    {
#if DEBUG
        /// <summary>
        /// Value indicating if the stream is open.
        /// </summary>
        private bool writeStreamOpen;
#endif
      
        /// <summary>
        /// Writes the specified buffer into the stream.
        /// </summary>
        /// <param name="position">The position in the stream to write at.</param>
        /// <param name="buffer">The buffer to write.</param>
        /// <param name="offset">The offset within the buffer to write from.</param>
        /// <param name="count">The number of bytes to write.</param>
        public abstract void Write(long position, byte[] buffer, int offset, int count);
        
        /// <summary>
        /// Sets the length of the stream.
        /// </summary>
        /// <param name="length">The length of the stream.</param>
        public abstract void SetLength(long length);

        /// <summary>
        /// Opens a stream for writing.
        /// </summary>
        /// <param name="append">if set to <c>true</c> writes will be appended to the existing stream.</param>
        /// <returns>A stream for writing.</returns>
        public virtual StreamOnDataStorage OpenWriteStream(bool append)
        {
            if (append)
            {
                return new AppendStreamOnDataStorage(this);
            }

            return new ReadWriteStreamOnDataStorage(this);
        }

#if DEBUG
        /// <summary>
        /// Marks the stream as open.
        /// </summary>
        internal void SignalWriteStreamOpen()
        {
            InternalDebug.Assert(!this.writeStreamOpen);
            this.writeStreamOpen = true;
        }

        /// <summary>
        /// Marks the stream as closed.
        /// </summary>
        internal void SignalWriteStreamClose()
        {
            InternalDebug.Assert(this.writeStreamOpen);
            this.writeStreamOpen = false;
        }
#endif
    }
}

