Make SeekIfPossible a little more streamlined

This commit is contained in:
Matt Nadareski
2021-01-29 10:49:27 -08:00
parent 6c66b58af2
commit d59962a812

View File

@@ -14,15 +14,21 @@ namespace SabreTools.IO
/// <param name="offset">Optional offset to seek to</param> /// <param name="offset">Optional offset to seek to</param>
public static long SeekIfPossible(this Stream input, long offset = 0) public static long SeekIfPossible(this Stream input, long offset = 0)
{ {
// If the stream is null, don't even try
if (input == null)
return -1;
// If the input is not seekable, just return the current position
if (!input.CanSeek)
return input.Position;
// Attempt to seek to the offset
try try
{ {
if (input.CanSeek) if (offset < 0)
{ return input.Seek(offset, SeekOrigin.End);
if (offset < 0) else if (offset >= 0)
return input.Seek(offset, SeekOrigin.End); return input.Seek(offset, SeekOrigin.Begin);
else if (offset >= 0)
return input.Seek(offset, SeekOrigin.Begin);
}
return input.Position; return input.Position;
} }