/// <summary> /// Initializes a new instance of the <see cref="HeifContext"/> class with the specified stream to read from, and optionally leaves the stream open. /// </summary> /// <param name="stream">The stream to read from.</param> /// <param name="leaveOpen"> /// <see langword="true"/> to leave the stream open after the <see cref="HeifContext"/> object is disposed; otherwise, <see langword="false"/>. /// </param> /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="stream"/> must support reading and seeking.</exception> /// <exception cref="HeifException"> /// Unable to create the native HeifContext. /// /// -or- /// /// The LibHeif version is not supported. /// /// -or- /// /// A LibHeif error occurred. /// </exception> public HeifContext(Stream stream, bool leaveOpen = false) { Validate.IsNotNull(stream, nameof(stream)); LibHeifVersion.ThrowIfNotSupported(); if (!stream.CanRead || !stream.CanSeek) { ExceptionUtil.ThrowArgumentException(Resources.StreamCannotReadAndSeek); } this.context = CreateNativeContext(); try { this.reader = HeifReaderFactory.CreateFromStream(stream, !leaveOpen); InitializeContextFromReader(); } catch (Exception ex) { this.context.Dispose(); this.reader?.Dispose(); if (ex is ArgumentException || ex is HeifException) { throw; } else { throw new HeifException(ex.Message, ex); } } }
/// <summary> /// Determines whether the specified parameter is empty or contains only whitespace characters, the parameter can be null. /// </summary> /// <param name="param">The parameter.</param> /// <param name="paramName">The name of the parameter.</param> /// <exception cref="ArgumentException">The parameter is empty or contains only whitespace characters.</exception> public static void IsNotEmptyOrWhiteSpace(string param, string paramName) { if (param != null && param.IsEmptyOrWhiteSpace()) { ExceptionUtil.ThrowArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.ParameterStringIsEmptyOrWhitespaceFormat, paramName)); } }
/// <summary> /// Determines whether the specified array is null or empty. /// </summary> /// <param name="param">The parameter.</param> /// <param name="paramName">The name of the parameter.</param> /// <exception cref="ArgumentNullException">The parameter is null.</exception> /// <exception cref="ArgumentException">The parameter is a zero-length array.</exception> public static void IsNotNullOrEmptyArray <T>(T[] param, string paramName) { if (param is null) { ExceptionUtil.ThrowArgumentNullException(paramName); } else if (param.Length == 0) { ExceptionUtil.ThrowArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.ParameterIsEmptyArrayFormat, paramName)); } }
/// <summary> /// Initializes a new instance of the <see cref="HeifContext"/> class with the specified stream to read from, and optionally leaves the stream open. /// </summary> /// <param name="stream">The stream to read from.</param> /// <param name="leaveOpen"> /// <see langword="true"/> to leave the stream open after the <see cref="HeifContext"/> object is disposed; otherwise, <see langword="false"/>. /// </param> /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="stream"/> must support reading and seeking.</exception> /// <exception cref="HeifException"> /// Unable to create the native HeifContext. /// /// -or- /// /// The LibHeif version is not supported. /// /// -or- /// /// A LibHeif error occurred. /// </exception> public HeifContext(Stream stream, bool leaveOpen = false) { Validate.IsNotNull(stream, nameof(stream)); LibHeifVersion.ThrowIfNotSupported(); if (!stream.CanRead || !stream.CanSeek) { ExceptionUtil.ThrowArgumentException(Resources.StreamCannotReadAndSeek); } this.context = CreateNativeContext(); try { this.readerStreamIO = new HeifStreamIO(stream, !leaveOpen); InitializeContextFromReader(); } catch { this.context.Dispose(); this.readerStreamIO?.Dispose(); throw; } }