mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-10-03 20:28:57 +00:00
add extension methods for checks
This commit is contained in:
58
src/SharpCompress/StreamValidationExtensions.cs
Normal file
58
src/SharpCompress/StreamValidationExtensions.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace SharpCompress;
|
||||
|
||||
internal static class StreamValidationExtensions
|
||||
{
|
||||
internal static Stream RequireReadable(this Stream stream)
|
||||
{
|
||||
stream.NotNull(nameof(stream));
|
||||
|
||||
if (!stream.CanRead)
|
||||
{
|
||||
throw new ArgumentException("Stream must be readable", nameof(stream));
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
internal static Stream RequireSeekable(this Stream stream)
|
||||
{
|
||||
stream.NotNull(nameof(stream));
|
||||
|
||||
if (!stream.CanSeek)
|
||||
{
|
||||
throw new ArgumentException("Stream must be seekable", nameof(stream));
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
internal static IReadOnlyList<Stream> RequireSeekable(this IReadOnlyList<Stream> streams)
|
||||
{
|
||||
streams.NotNull(nameof(streams));
|
||||
|
||||
foreach (var stream in streams)
|
||||
{
|
||||
stream.RequireSeekable();
|
||||
}
|
||||
|
||||
return streams;
|
||||
}
|
||||
|
||||
internal static IReadOnlyList<Stream> RequireReadable(this IEnumerable<Stream> streams)
|
||||
{
|
||||
streams.NotNull(nameof(streams));
|
||||
|
||||
var streamArray = streams as IReadOnlyList<Stream> ?? streams.ToArray();
|
||||
foreach (var stream in streamArray)
|
||||
{
|
||||
stream.RequireReadable();
|
||||
}
|
||||
|
||||
return streamArray;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user