stride3d/stride · error · NotSupportedException

Can't resize VirtualFileStream if endPosition is not -1.

Error message

Can't resize VirtualFileStream if endPosition is not -1.

What it means

VirtualFileStream.SetLength only works when the stream is unbounded (endPosition == -1). When the stream is a bounded slice of a parent stream, resizing it is meaningless and a NotSupportedException is thrown.

Solutions

  1. Don't resize a bounded slice; operate on the underlying parent stream instead
  2. Construct the VirtualFileStream without an endPosition bound if resizing is required
  3. Guard with a capability check before calling SetLength

Example fix

// before
virtualStream.SetLength(0); // NotSupportedException when bounded
// after
if (virtualStream.CanWrite && virtualStream is not VirtualFileStream vfs || vfs.IsUnbounded)
{
    stream.SetLength(0);
}
else
{
    // fall back to writing zeros over the region instead of truncating
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (stream is VirtualFileStream vfs && !vfs.IsUnbounded) { /* use parent stream or skip truncation */ }

Type guard

static bool CanResize(Stream s) => s is not VirtualFileStream vfs || vfs.EndPosition == -1;

Try / catch

try { stream.SetLength(value); } catch (NotSupportedException) { /* bounded slice; resize parent instead */ }

Prevention

When it happens

Trigger: Calling SetLength(value) on a VirtualFileStream constructed with an endPosition other than -1 (a bounded sub-stream).

Common situations: Generic stream-truncation code paths (e.g. 'reset file' logic) applied to a virtual sub-stream; writing frameworks that truncate before writing.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.IO/VirtualFileStream.cs:203

                break;
        }

        var newPosition = InternalStream.Position;
        if (newPosition < startPosition || (endPosition != -1 && newPosition > endPosition))
        {
            InternalStream.Position = startPosition;
            throw new IOException("Cannot seek to the specified position.");
        }

        return newPosition;
    }

    /// <inheritdoc/>
    public override void SetLength(long value)
    {
        if (endPosition != -1)
        {
            throw new NotSupportedException("Can't resize VirtualFileStream if endPosition is not -1.");
        }

        InternalStream.SetLength(value);
    }

    /// <inheritdoc/>
    /// <exception cref="IOException">The remaining capacity in the stream
    /// is not large enough to write the specified <paramref name="buffer"/>.</exception>
    public override void Write(byte[] buffer, int offset, int count)
    {
        if (endPosition != -1 && count > endPosition - InternalStream.Position)
        {
            throw new IOException("Can't write beyond end of stream.");
        }

        InternalStream.Write(buffer, offset, count);
    }

View on GitHub (pinned to 96fad776d2)