{"record":{"id":"9efcf6cd6bd2d88c","repo":"stride3d/stride","slug":"can-t-resize-virtualfilestream-if-endposition-is-not-1","errorCode":null,"errorMessage":"Can't resize VirtualFileStream if endPosition is not -1.","messagePattern":"Can't resize VirtualFileStream if endPosition is not -1\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.IO/VirtualFileStream.cs","lineNumber":203,"sourceCode":"                break;\n        }\n\n        var newPosition = InternalStream.Position;\n        if (newPosition < startPosition || (endPosition != -1 && newPosition > endPosition))\n        {\n            InternalStream.Position = startPosition;\n            throw new IOException(\"Cannot seek to the specified position.\");\n        }\n\n        return newPosition;\n    }\n\n    /// <inheritdoc/>\n    public override void SetLength(long value)\n    {\n        if (endPosition != -1)\n        {\n            throw new NotSupportedException(\"Can't resize VirtualFileStream if endPosition is not -1.\");\n        }\n\n        InternalStream.SetLength(value);\n    }\n\n    /// <inheritdoc/>\n    /// <exception cref=\"IOException\">The remaining capacity in the stream\n    /// is not large enough to write the specified <paramref name=\"buffer\"/>.</exception>\n    public override void Write(byte[] buffer, int offset, int count)\n    {\n        if (endPosition != -1 && count > endPosition - InternalStream.Position)\n        {\n            throw new IOException(\"Can't write beyond end of stream.\");\n        }\n\n        InternalStream.Write(buffer, offset, count);\n    }\n","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.IO/VirtualFileStream.cs#L185-L221","documentation":"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.","triggerScenarios":"Calling SetLength(value) on a VirtualFileStream constructed with an endPosition other than -1 (a bounded sub-stream).","commonSituations":"Generic stream-truncation code paths (e.g. 'reset file' logic) applied to a virtual sub-stream; writing frameworks that truncate before writing.","solutions":["Don't resize a bounded slice; operate on the underlying parent stream instead","Construct the VirtualFileStream without an endPosition bound if resizing is required","Guard with a capability check before calling SetLength"],"exampleFix":"// before\nvirtualStream.SetLength(0); // NotSupportedException when bounded\n// after\nif (virtualStream.CanWrite && virtualStream is not VirtualFileStream vfs || vfs.IsUnbounded)\n{\n    stream.SetLength(0);\n}\nelse\n{\n    // fall back to writing zeros over the region instead of truncating\n}","handlingStrategy":"type-guard","validationCode":"if (stream is VirtualFileStream vfs && !vfs.IsUnbounded) { /* use parent stream or skip truncation */ }","typeGuard":"static bool CanResize(Stream s) => s is not VirtualFileStream vfs || vfs.EndPosition == -1;","tryCatchPattern":"try { stream.SetLength(value); } catch (NotSupportedException) { /* bounded slice; resize parent instead */ }","preventionTips":["Only truncate unbounded streams","Expose a parent-stream resize path for slices","Check bounds before generic truncation logic"],"tags":["stream","resize","bounds"],"backgroundTag":"operation-not-supported","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}