HandyOrg/HandyControl · error · NotSupportedException

The streams can't be read for comparison

Error message

The streams can't be read for comparison

What it means

Utility.AreStreamsEqual compares two streams byte-by-byte; if either stream has CanRead == false the comparison is impossible, so it throws NotSupportedException. This is a guard against comparing closed or write-only streams.

Solutions

  1. Check left.CanRead && right.CanRead before calling AreStreamsEqual
  2. Reopen the streams if they are disposed or closed
  3. Open the streams with FileAccess.Read / FileMode.Open instead of write-only access
  4. Catch NotSupportedException and treat the comparison as indeterminate

Example fix

// before
bool same = Utility.AreStreamsEqual(writeOnlyStream, otherStream); // throws
// after
bool same = writeOnlyStream.CanRead && otherStream.CanRead && Utility.AreStreamsEqual(writeOnlyStream, otherStream);
Defensive patterns

Strategy: validation

Validate before calling

static bool CanCompare(Stream left, Stream right) =>
    left != null && right != null && left.CanRead && right.CanRead;

Type guard

static bool IsReadable(Stream s) => s != null && s.CanRead;

Try / catch

try { equal = Utility.AreStreamsEqual(a, b); }
catch (NotSupportedException ex) { Log.Warn("Streams not readable for comparison", ex); equal = false; }

Prevention

When it happens

Trigger: Calling AreStreamsEqual with a stream that is closed/disposed, a write-only stream, or a stream positioned/locked such that CanRead returns false.

Common situations: Comparing file streams after disposal, comparing network or memory streams opened for writing only, reuse of streams after an earlier read-only-lifetime ended.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/ff889752d2da627a. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/Microsoft.Windows.Shell/Standard/Utility.cs:95

    {
        return (int) ((short) (i & 65535));
    }

    [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
    [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    public static bool AreStreamsEqual(Stream left, Stream right)
    {
        if (left == null)
        {
            return right == null;
        }
        if (right == null)
        {
            return false;
        }
        if (!left.CanRead || !right.CanRead)
        {
            throw new NotSupportedException("The streams can't be read for comparison");
        }
        if (left.Length != right.Length)
        {
            return false;
        }
        int num = (int) left.Length;
        left.Position = 0L;
        right.Position = 0L;
        int i = 0;
        int num2 = 0;
        byte[] array = new byte[512];
        byte[] array2 = new byte[512];
        GCHandle gchandle = GCHandle.Alloc(array, GCHandleType.Pinned);
        IntPtr left2 = gchandle.AddrOfPinnedObject();
        GCHandle gchandle2 = GCHandle.Alloc(array2, GCHandleType.Pinned);
        IntPtr right2 = gchandle2.AddrOfPinnedObject();
        bool result;
        try

View on GitHub (pinned to 2c0875ebd6)