mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
37 lines
989 B
C#
37 lines
989 B
C#
using System.IO;
|
|
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;
|
|
}
|
|
catch
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
}
|