Files
SabreTools/SabreTools.IO/StreamExtensions.cs

37 lines
989 B
C#
Raw Normal View History

2020-12-08 00:13:22 -08:00
using System.IO;
2020-12-07 22:32:37 -08:00
using System.Linq;
namespace SabreTools.IO
{
/// <summary>
/// Extensions to Stream functionality
/// </summary>
public static class StreamExtensions
{
/// <summary>
/// Seek to a specific point in the stream, if possible
/// </summary>
/// <param name="input">Input stream to try seeking on</param>
/// <param name="offset">Optional offset to seek to</param>
public static long SeekIfPossible(this Stream input, long offset = 0)
{
try
{
if (input.CanSeek)
{
if (offset < 0)
return input.Seek(offset, SeekOrigin.End);
else if (offset >= 0)
return input.Seek(offset, SeekOrigin.Begin);
}
return input.Position;
}
2020-12-08 00:13:22 -08:00
catch
2020-12-07 22:32:37 -08:00
{
2020-12-08 00:13:22 -08:00
return -1;
2020-12-07 22:32:37 -08:00
}
}
}
}