mirror of https://github.com/SixLabors/ImageSharp
9 changed files with 188 additions and 76 deletions
@ -0,0 +1,29 @@ |
|||||
|
// <copyright file="DecoderErrorCode.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Formats |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Represents "recoverable" decoder errors.
|
||||
|
/// </summary>
|
||||
|
internal enum DecoderErrorCode |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// NoError
|
||||
|
/// </summary>
|
||||
|
NoError, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// MissingFF00
|
||||
|
/// </summary>
|
||||
|
// ReSharper disable once InconsistentNaming
|
||||
|
MissingFF00, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// End of stream reached unexpectedly
|
||||
|
/// </summary>
|
||||
|
UnexpectedEndOfFile |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,49 @@ |
|||||
|
// <copyright file="DecoderThrowHelper.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Formats.Jpg |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Encapsulates exception thrower methods for the Jpeg Encoder
|
||||
|
/// </summary>
|
||||
|
internal static class DecoderThrowHelper |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Throws an exception that belongs to the given <see cref="DecoderErrorCode"/>
|
||||
|
/// </summary>
|
||||
|
/// <param name="errorCode">The <see cref="DecoderErrorCode"/></param>
|
||||
|
[MethodImpl(MethodImplOptions.NoInlining)] |
||||
|
public static void ThrowExceptionForErrorCode(this DecoderErrorCode errorCode) |
||||
|
{ |
||||
|
switch (errorCode) |
||||
|
{ |
||||
|
case DecoderErrorCode.NoError: |
||||
|
throw new ArgumentException("ThrowExceptionForErrorCode() called with NoError!", nameof(errorCode)); |
||||
|
case DecoderErrorCode.MissingFF00: |
||||
|
throw new MissingFF00Exception(); |
||||
|
case DecoderErrorCode.UnexpectedEndOfFile: |
||||
|
throw new EOFException(); |
||||
|
default: |
||||
|
throw new ArgumentOutOfRangeException(nameof(errorCode), errorCode, null); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Throws an exception if the given <see cref="DecoderErrorCode"/> defines an error.
|
||||
|
/// </summary>
|
||||
|
/// <param name="errorCode">The <see cref="DecoderErrorCode"/></param>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static void EnsureNoError(this DecoderErrorCode errorCode) |
||||
|
{ |
||||
|
if (errorCode != DecoderErrorCode.NoError) |
||||
|
{ |
||||
|
ThrowExceptionForErrorCode(errorCode); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
// <copyright file="EOFException.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Formats.Jpg |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The EOF (End of File exception).
|
||||
|
/// Thrown when the decoder encounters an EOF marker without a proceeding EOI (End Of Image) marker
|
||||
|
/// </summary>
|
||||
|
internal class EOFException : Exception |
||||
|
{ |
||||
|
public EOFException() |
||||
|
: base("Reached end of stream before proceeding EOI marker!") |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
// <copyright file="MissingFF00Exception.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Formats.Jpg |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The missing ff00 exception.
|
||||
|
/// </summary>
|
||||
|
// ReSharper disable once InconsistentNaming
|
||||
|
internal class MissingFF00Exception : Exception |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue