stride3d/stride · error · NotSupportedException

Operation 'Write' is not supported

Error message

Operation 'Write' is not supported

What it means

LZ4Stream.WriteByte checks CanWrite before appending the byte to the current compression block and throws NotSupportedException ('Write') when the stream was not opened for writing. Writing to a read/decompress-mode LZ4 stream is invalid by design.

Solutions

  1. Open the LZ4Stream in write/compress mode before writing
  2. Check CanWrite before writes and route to the correct stream
  3. Verify compress/decompress directions weren't swapped when constructing the pair

Example fix

// before
var s = new LZ4Stream(fs, CompressionMode.Decompress); s.WriteByte(b);
// after
var s = new LZ4Stream(fs, CompressionMode.Compress); if (s.CanWrite) s.WriteByte(b);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!lz4Stream.CanWrite) throw new InvalidOperationException("LZ4Stream was not opened for writing; use Compress/write mode");

Type guard

static bool IsWritable(Stream s) => s is { CanWrite: true };

Try / catch

try { lz4Stream.WriteByte(value); }
catch (NotSupportedException ex) when (ex.Message.Contains("'Write' is not supported")) { throw new InvalidOperationException("Stream is read-mode; open LZ4Stream in compress/write mode.", ex); }

Prevention

When it happens

Trigger: Calling WriteByte on an LZ4Stream created in read/decompress mode, or after Dispose.

Common situations: Passing a decompression stream to code that writes output (e.g. a serializer given the wrong end of a compress/decompress pair).

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/c1e4f1e012df0927. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Serialization/Serialization/LZ4/LZ4Stream.cs:466

        }
        else
        {
            throw NotSupported("Seek");
        }

        return Position;
    }

    /// <inheritdoc/>
    public override void SetLength(long value)
    {
        throw NotSupported("SetLength");
    }

    /// <inheritdoc/>
    public override void WriteByte(byte value)
    {
        if (!CanWrite) throw NotSupported("Write");

        position++;

        if (dataBuffer == null)
        {
            dataBuffer = new byte[blockSize];
            bufferLength = blockSize;
            bufferOffset = 0;
        }

        if (bufferOffset >= bufferLength)
        {
            FlushCurrentChunk();
        }

        dataBuffer[bufferOffset++] = value;
    }

View on GitHub (pinned to 96fad776d2)