Move SeekIfPossible to a better location

This commit is contained in:
Matt Nadareski
2024-04-25 14:11:19 -04:00
parent 69a41b2487
commit 904aed1c44
3 changed files with 46 additions and 40 deletions

View File

@@ -0,0 +1,46 @@
using System.IO;
namespace SabreTools.IO.Extensions
{
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)
{
// 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)
{
try
{
return input.Position;
}
catch
{
return -1;
}
}
// Attempt to seek to the offset
try
{
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;
}
}
}
}

View File

@@ -454,45 +454,6 @@ namespace SabreTools.IO.Extensions
return data;
}
/// <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)
{
// 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)
{
try
{
return input.Position;
}
catch
{
return -1;
}
}
// Attempt to seek to the offset
try
{
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;
}
}
/// <summary>
/// Read a number of bytes from the stream to a buffer
/// </summary>

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET7_0_OR_GREATER
using System.Numerics;