{"record":{"id":"ff889752d2da627a","repo":"HandyOrg/HandyControl","slug":"the-streams-can-t-be-read-for-comparison","errorCode":null,"errorMessage":"The streams can't be read for comparison","messagePattern":"The streams can't be read for comparison","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Shared/Microsoft.Windows.Shell/Standard/Utility.cs","lineNumber":95,"sourceCode":"    {\n        return (int) ((short) (i & 65535));\n    }\n\n    [SuppressMessage(\"Microsoft.Security\", \"CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands\")]\n    [SuppressMessage(\"Microsoft.Performance\", \"CA1811:AvoidUncalledPrivateCode\")]\n    public static bool AreStreamsEqual(Stream left, Stream right)\n    {\n        if (left == null)\n        {\n            return right == null;\n        }\n        if (right == null)\n        {\n            return false;\n        }\n        if (!left.CanRead || !right.CanRead)\n        {\n            throw new NotSupportedException(\"The streams can't be read for comparison\");\n        }\n        if (left.Length != right.Length)\n        {\n            return false;\n        }\n        int num = (int) left.Length;\n        left.Position = 0L;\n        right.Position = 0L;\n        int i = 0;\n        int num2 = 0;\n        byte[] array = new byte[512];\n        byte[] array2 = new byte[512];\n        GCHandle gchandle = GCHandle.Alloc(array, GCHandleType.Pinned);\n        IntPtr left2 = gchandle.AddrOfPinnedObject();\n        GCHandle gchandle2 = GCHandle.Alloc(array2, GCHandleType.Pinned);\n        IntPtr right2 = gchandle2.AddrOfPinnedObject();\n        bool result;\n        try","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/Microsoft.Windows.Shell/Standard/Utility.cs#L77-L113","documentation":"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.","triggerScenarios":"Calling AreStreamsEqual with a stream that is closed/disposed, a write-only stream, or a stream positioned/locked such that CanRead returns false.","commonSituations":"Comparing file streams after disposal, comparing network or memory streams opened for writing only, reuse of streams after an earlier read-only-lifetime ended.","solutions":["Check left.CanRead && right.CanRead before calling AreStreamsEqual","Reopen the streams if they are disposed or closed","Open the streams with FileAccess.Read / FileMode.Open instead of write-only access","Catch NotSupportedException and treat the comparison as indeterminate"],"exampleFix":"// before\nbool same = Utility.AreStreamsEqual(writeOnlyStream, otherStream); // throws\n// after\nbool same = writeOnlyStream.CanRead && otherStream.CanRead && Utility.AreStreamsEqual(writeOnlyStream, otherStream);","handlingStrategy":"validation","validationCode":"static bool CanCompare(Stream left, Stream right) =>\n    left != null && right != null && left.CanRead && right.CanRead;","typeGuard":"static bool IsReadable(Stream s) => s != null && s.CanRead;","tryCatchPattern":"try { equal = Utility.AreStreamsEqual(a, b); }\ncatch (NotSupportedException ex) { Log.Warn(\"Streams not readable for comparison\", ex); equal = false; }","preventionTips":["Check CanRead on both streams before comparison","Reopen disposed streams instead of reusing them","Open streams with FileAccess.Read for comparison workloads"],"tags":["streams","io","comparison","not-supported"],"backgroundTag":"unsupported-operation","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}