示例#1
0
        /// <summary>
        /// Retrieve a new MemoryStream object with the given tag and with contents copied from the provided
        /// buffer. The provided buffer is not wrapped or used after construction.
        /// </summary>
        /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
        /// <param name="tag">A tag which can be used to track the source of the stream.</param>
        /// <param name="buffer">The byte buffer to copy data from.</param>
        /// <param name="offset">The offset from the start of the buffer to copy from.</param>
        /// <param name="count">The number of bytes to copy from the buffer.</param>
        /// <returns>A MemoryStream.</returns>
        public MemoryStream GetStream(string tag, byte[] buffer, int offset, int count)
        {
            RecyclableMemoryStream stream = null;

            try
            {
                stream = new RecyclableMemoryStream(this, tag, count);
                stream.Write(buffer, offset, count);
                stream.Position = 0;
                return(stream);
            }
            catch
            {
                stream?.Dispose();
                throw;
            }
        }
        /// <summary>
        /// Retrieve a new <c>MemoryStream</c> object with the given tag and with contents copied from the provided
        /// buffer. The provided buffer is not wrapped or used after construction.
        /// </summary>
        /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
        /// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
        /// <param name="tag">A tag which can be used to track the source of the stream.</param>
        /// <param name="buffer">The byte buffer to copy data from.</param>
        /// <returns>A <c>MemoryStream</c>.</returns>
        public MemoryStream GetStream(Guid id, string tag, Memory <byte> buffer)
        {
            RecyclableMemoryStream stream = null;

            try
            {
                stream = new RecyclableMemoryStream(this, id, tag, buffer.Length);
                stream.Write(buffer.Span);
                stream.Position = 0;
                return(stream);
            }
            catch
            {
                stream?.Dispose();
                throw;
            }
        }