EllanJiang/GameFramework · error · GameFrameworkException

Stream is invalid.

Error message

Stream is invalid.

What it means

The stream-based Utility.Verifier.GetCrc32(Stream) computes a CRC32 by streaming through the stream in chunks. It throws GameFrameworkException("Stream is invalid.") when the stream argument is null. The guard ensures the library never dereferences a null stream.

Solutions

  1. Ensure the stream is opened successfully before calling GetCrc32; null-check the result of File.Open/asset load
  2. Fix the loader so it throws on failure rather than returning null
  3. Catch GameFrameworkException and surface a friendly 'file could not be verified' error

Example fix

// before
Stream s = OpenFile(path); // may return null
int crc = Utility.Verifier.GetCrc32(s);
// after
using (Stream s = File.OpenRead(path))
{
    int crc = Utility.Verifier.GetCrc32(s);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (stream == null) throw new FileNotFoundException("stream not opened");

Type guard

static bool IsOpen(Stream stream) => stream != null && stream.CanRead;

Try / catch

try { return Utility.Verifier.GetCrc32(stream); }
catch (GameFrameworkException ex) { Log.Warning("Stream CRC failed: {0}", ex.Message); return 0; }

Prevention

When it happens

Trigger: Calling Utility.Verifier.GetCrc32(stream) with a null Stream — e.g. File.OpenRead failed upstream and null was propagated, or a resource loader returned null on failure.

Common situations: Verifying a downloaded package file before the stream was opened; a Unity WWW/WebRequest path where the response stream is null on error; passing a disposed-or-never-created stream stored in a field.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/091a11724fc7d460. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/Utility/Utility.Verifier.cs:73

                    throw new GameFrameworkException("Offset or length is invalid.");
                }

                s_Algorithm.HashCore(bytes, offset, length);
                int result = (int)s_Algorithm.HashFinal();
                s_Algorithm.Initialize();
                return result;
            }

            /// <summary>
            /// 计算二进制流的 CRC32。
            /// </summary>
            /// <param name="stream">指定的二进制流。</param>
            /// <returns>计算后的 CRC32。</returns>
            public static int GetCrc32(Stream stream)
            {
                if (stream == null)
                {
                    throw new GameFrameworkException("Stream is invalid.");
                }

                while (true)
                {
                    int bytesRead = stream.Read(s_CachedBytes, 0, CachedBytesLength);
                    if (bytesRead > 0)
                    {
                        s_Algorithm.HashCore(s_CachedBytes, 0, bytesRead);
                    }
                    else
                    {
                        break;
                    }
                }

                int result = (int)s_Algorithm.HashFinal();
                s_Algorithm.Initialize();
                Array.Clear(s_CachedBytes, 0, CachedBytesLength);

View on GitHub (pinned to d0c010b051)