using System;
using System.IO;
using System.Threading.Tasks;
using TestLibrary.Extension;
namespace TestLibrary.Decoding
{
/// <summary>
/// GIF 애플리케이션 확장
/// </summary>
public class GIFApplicationExtension : GIFExtension
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Public
#region Field
/// <summary>
/// 확장 레이블
/// </summary>
public const int ExtensionLabel = 0xff;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 블럭 크기 - BlockSize
/// <summary>
/// 블럭 크기
/// </summary>
public int BlockSize { get; private set; }
#endregion
#region 애플리케이션 식별자 - ApplicationIdentifier
/// <summary>
/// 애플리케이션 식별자
/// </summary>
public string ApplicationIdentifier { get; private set; }
#endregion
#region 인증 코드 - AuthenticationCode
/// <summary>
/// 인증 코드
/// </summary>
public byte[] AuthenticationCode { get; private set; }
#endregion
#region 데이터 - Data
/// <summary>
/// 데이터
/// </summary>
public byte[] Data { get; private set; }
#endregion
#region GIF 블럭 종류 - Kind
/// <summary>
/// GIF 블럭 종류
/// </summary>
public override GIFBlockKind Kind
{
get
{
return GIFBlockKind.SpecialPurpose;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 생성자 - GIFApplicationExtension()
/// <summary>
/// 생성자
/// </summary>
private GIFApplicationExtension()
{
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 읽기 (비동기) - ReadAsync(stream)
/// <summary>
/// 읽기 (비동기)
/// </summary>
/// <param name="stream">스트림</param>
/// <returns>GIF 애플리케이션 확장 태스크</returns>
public static async Task<GIFApplicationExtension> ReadAsync(Stream stream)
{
GIFApplicationExtension extension = new GIFApplicationExtension();
await extension.ReadInternalAsync(stream).ConfigureAwait(false);
return extension;
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Private
#region 읽기 (내부용, 비동기) - ReadInternalAsync(stream)
/// <summary>
/// 읽기 (내부용, 비동기)
/// </summary>
/// <param name="stream">스트림</param>
/// <returns>태스크</returns>
private async Task ReadInternalAsync(Stream stream)
{
byte[] byteArray = new byte[12];
await stream.ReadAllAsync(byteArray, 0, byteArray.Length).ConfigureAwait(false);
BlockSize = byteArray[0];
if(BlockSize != 11)
{
throw GIFHelper.ThrowInvalidBlockSizeException("Application Extension", 11, BlockSize);
}
ApplicationIdentifier = GIFHelper.GetString(byteArray, 1, 8);
byte[] authenticationCode = new byte[3];
Array.Copy(byteArray, 9, authenticationCode, 0, 3);
AuthenticationCode = authenticationCode;
Data = await GIFHelper.ReadDataBlocksAsync(stream).ConfigureAwait(false);
}
#endregion
}
}