﻿using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net.Http;

namespace EasyApi.Http.Content
{
	public class SyncByteArrayContent : ByteArrayContent, ISyncHttpContent
	{
		private LazyReadStream _readStream;

		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "The content parameter is validated in the base constructor.")]
		public SyncByteArrayContent(byte[] content)
			: base(content)
		{
			Initialize(content, 0, content.Length);
		}

		public SyncByteArrayContent(byte[] content, int offset, int count)
			: base(content, offset, count)
		{
			Initialize(content, offset, count);
		}

		private void Initialize(byte[] content, int offset, int count)
		{
			_readStream = new LazyReadStream(this, content, offset, count);
		}

		public Stream ReadAsStream()
		{
			return _readStream.Stream;
		}

		protected override void Dispose(bool disposing)
		{
			base.Dispose(disposing);

			if (disposing)
			{
				_readStream.Dispose();
			}
		}
	}
}