/// <summary>
        /// Creates a TeaFile, using the specified stream as the underlying storage media.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="ownsStream">If true, the instance returned owns the stream, such that disposing the returned instance also disposes <paramref name="stream"/>.</param>
        /// <param name="contentDescription">The content description.</param>
        /// <param name="nameValues">The name values.</param>
        /// <param name="includeItemDescription">if set to <c>true</c> [include item description].</param>
        /// <returns></returns>
        /// <remarks>
        /// Instead of creating a new <see cref="FileStream"/>, this method takes the <see cref="Stream"/> passed. This provides
        /// more control over the stream, like setting specific <see cref="FileShare"/> attributes. It also allows usage of
        /// alternative storage medias like <see cref="MemoryStream"/>.
        /// </remarks>
        public static TeaFile <T> Create(Stream stream, bool ownsStream, string contentDescription = null, NameValueCollection nameValues = null, bool includeItemDescription = true)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            var tf = new TeaFile <T>();

            try
            {
                tf.core   = new TeaFileCore(stream, ownsStream);
                tf.buffer = new SafeBuffer <T>(stream);
                if (includeItemDescription)
                {
                    tf.Description.ItemDescription = ItemDescription.FromAnalysis <T>();
                }
                tf.Description.ContentDescription = contentDescription;
                tf.Description.NameValues         = nameValues;
                tf.Description.Timescale          = Time.Scale; // The
                tf.core.WriteHeader();
                tf.Flush();
                return(tf);
            }
            catch (Exception)
            {
                tf.Dispose();
                throw;
            }
        }