Skip to content

Commit

Permalink
Throw ArgumentNullException from public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-visionaid committed Nov 28, 2024
1 parent 5c35810 commit 13a3c05
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
5 changes: 5 additions & 0 deletions OpenMcdf/CfbStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public override void Flush()

public override int Read(byte[] buffer, int offset, int count)
{
ThrowHelper.ThrowIfStreamArgumentsAreInvalid(buffer, offset, count);

this.ThrowIfDisposed(isDisposed);

return stream.Read(buffer, offset, count);
Expand All @@ -83,6 +85,9 @@ private void EnsureLengthToWrite(int count)

public override void SetLength(long value)
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value));

this.ThrowIfDisposed(isDisposed);
this.ThrowIfNotWritable();

Expand Down
24 changes: 24 additions & 0 deletions OpenMcdf/RootStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ private static IOContextFlags ToIOContextFlags(StorageModeFlags flags)

public static RootStorage Create(string fileName, Version version = Version.V3, StorageModeFlags flags = StorageModeFlags.None)
{
if (fileName is null)
throw new ArgumentNullException(nameof(fileName));

ThrowIfLeaveOpen(flags);

FileStream stream = File.Create(fileName);
Expand All @@ -62,6 +65,9 @@ public static RootStorage Create(string fileName, Version version = Version.V3,

public static RootStorage Create(Stream stream, Version version = Version.V3, StorageModeFlags flags = StorageModeFlags.None)
{
if (stream is null)
throw new ArgumentNullException(nameof(stream));

stream.ThrowIfNotSeekable();
stream.SetLength(0);
stream.Position = 0;
Expand All @@ -76,6 +82,9 @@ public static RootStorage Create(Stream stream, Version version = Version.V3, St

public static RootStorage Open(string fileName, FileMode mode, StorageModeFlags flags = StorageModeFlags.None)
{
if (fileName is null)
throw new ArgumentNullException(nameof(fileName));

ThrowIfInvalid(mode);
ThrowIfLeaveOpen(flags);

Expand All @@ -85,6 +94,9 @@ public static RootStorage Open(string fileName, FileMode mode, StorageModeFlags

public static RootStorage Open(string fileName, FileMode mode, FileAccess access, StorageModeFlags flags = StorageModeFlags.None)
{
if (fileName is null)
throw new ArgumentNullException(nameof(fileName));

ThrowIfInvalid(mode);
ThrowIfInvalid(access);
ThrowIfLeaveOpen(flags);
Expand All @@ -95,6 +107,9 @@ public static RootStorage Open(string fileName, FileMode mode, FileAccess access

public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageModeFlags.None)
{
if (stream is null)
throw new ArgumentNullException(nameof(stream));

stream.ThrowIfNotSeekable();
stream.Position = 0;

Expand All @@ -106,6 +121,9 @@ public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageMo

public static RootStorage OpenRead(string fileName, StorageModeFlags flags = StorageModeFlags.None)
{
if (fileName is null)
throw new ArgumentNullException(nameof(fileName));

ThrowIfLeaveOpen(flags);

FileStream stream = File.OpenRead(fileName);
Expand Down Expand Up @@ -203,11 +221,17 @@ private void SwitchToCore(Stream stream, bool allowLeaveOpen)

public void SwitchTo(Stream stream)
{
if (stream is null)
throw new ArgumentNullException(nameof(stream));

SwitchToCore(stream, true);
}

public void SwitchTo(string fileName)
{
if (fileName is null)
throw new ArgumentNullException(nameof(fileName));

FileStream stream = File.Create(fileName);
SwitchToCore(stream, false);
}
Expand Down
30 changes: 29 additions & 1 deletion OpenMcdf/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ IEnumerable<DirectoryEntry> EnumerateDirectoryEntries()

public bool ContainsEntry(string name)
{
if (name is null)
throw new ArgumentNullException(nameof(name));

ThrowHelper.ThrowIfNameIsInvalid(name);

this.ThrowIfDisposed(Context.IsDisposed);
Expand All @@ -105,6 +108,9 @@ public bool ContainsEntry(string name)

public bool TryGetEntryInfo(string name, out EntryInfo entryInfo)
{
if (name is null)
throw new ArgumentNullException(nameof(name));

ThrowHelper.ThrowIfNameIsInvalid(name);

this.ThrowIfDisposed(Context.IsDisposed);
Expand All @@ -130,6 +136,9 @@ DirectoryEntry AddDirectoryEntry(StorageType storageType, string name)

public Storage CreateStorage(string name)
{
if (name is null)
throw new ArgumentNullException(nameof(name));

ThrowHelper.ThrowIfNameIsInvalid(name);

this.ThrowIfDisposed(Context.IsDisposed);
Expand All @@ -141,6 +150,9 @@ public Storage CreateStorage(string name)

public CfbStream CreateStream(string name)
{
if (name is null)
throw new ArgumentNullException(nameof(name));

ThrowHelper.ThrowIfNameIsInvalid(name);

this.ThrowIfDisposed(Context.IsDisposed);
Expand All @@ -157,6 +169,9 @@ public Storage OpenStorage(string name)

public bool TryOpenStorage(string name, [MaybeNullWhen(false)] out Storage storage)
{
if (name is null)
throw new ArgumentNullException(nameof(name));

ThrowHelper.ThrowIfNameIsInvalid(name);

this.ThrowIfDisposed(Context.IsDisposed);
Expand All @@ -179,10 +194,14 @@ public CfbStream OpenStream(string name)

public bool TryOpenStream(string name, [MaybeNullWhen(false)] out CfbStream stream)
{
if (name is null)
throw new ArgumentNullException(nameof(name));

ThrowHelper.ThrowIfNameIsInvalid(name);

this.ThrowIfDisposed(Context.IsDisposed);
directoryTree.TryGetDirectoryEntry(name, out DirectoryEntry? entry);

directoryTree.TryGetDirectoryEntry(name, out DirectoryEntry? entry);
if (entry is null || entry.Type is not StorageType.Stream)
{
stream = null;
Expand All @@ -195,6 +214,12 @@ public bool TryOpenStream(string name, [MaybeNullWhen(false)] out CfbStream stre

public void CopyTo(Storage destination)
{
if (destination is null)
throw new ArgumentNullException(nameof(destination));

if (destination == this)
throw new ArgumentException("A storage cannot be copied to itself.", nameof(destination));

foreach (DirectoryEntry entry in EnumerateDirectoryEntries())
{
if (entry.Type is StorageType.Storage)
Expand All @@ -214,6 +239,9 @@ public void CopyTo(Storage destination)

public void Delete(string name)
{
if (name is null)
throw new ArgumentNullException(nameof(name));

ThrowHelper.ThrowIfNameIsInvalid(name);

this.ThrowIfDisposed(Context.IsDisposed);
Expand Down

0 comments on commit 13a3c05

Please sign in to comment.