stride3d/stride · error · IOException
Can't write beyond end of stream.
Error message
Can't write beyond end of stream.
What it means
VirtualFileStream.Write (byte[] overload) enforces the slice's endPosition: if the requested count would push the internal stream's position past the end of the bounded region, an IOException is thrown instead of silently overwriting data that belongs after the slice.
Solutions
- Cap the write count to the remaining capacity: Math.Min(count, (int)(endPosition - InternalStream.Position))
- Extend the slice's endPosition when constructing the stream if more capacity is needed
- Write to an unbounded stream or the parent stream for larger payloads
- Split writes into chunks that fit the remaining capacity
Example fix
// before virtualStream.Write(buffer, 0, buffer.Length); // IOException if it overruns // after var remaining = end - virtualStream.Position; var toWrite = (int)Math.Min(buffer.Length, remaining); virtualStream.Write(buffer, 0, toWrite);
Defensive patterns
Strategy: validation
Validate before calling
long remaining = end - stream.Position; int toWrite = (int)Math.Min(count, Math.Max(0, remaining)); if (toWrite > 0) stream.Write(buffer, offset, toWrite);
Try / catch
try { stream.Write(buffer, offset, count); } catch (IOException ex) when (ex.Message == "Can't write beyond end of stream.") { /* truncate or grow slice */ } Prevention
- Track remaining capacity in writer loops
- Chunk large writes by remaining capacity
- Size slices large enough for the payload up front
When it happens
Trigger: Writing a buffer whose count exceeds endPosition - current position on a bounded VirtualFileStream (endPosition != -1).
Common situations: Buffered writers flushing buffers larger than the remaining slice capacity; copy loops with a fixed chunk size that overruns the slice on the last iteration.
Understand the failure class
Background: "File too large" / "file size exceeds limit" errors: why libraries cap file sizes and how to fix them — this error's family across 46 libraries.
Related errors
- Cannot seek to the specified position.
- Can't resize VirtualFileStream if endPosition is not -1.
- Stream cannot seek
- Stream cannot be written
- You cannot modify this stream.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/8897c03f97e7a5b9.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.IO/VirtualFileStream.cs:216
/// <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);
}
/// <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(ReadOnlySpan<byte> buffer)
{
if (endPosition != -1 && buffer.Length > endPosition - InternalStream.Position)
{
throw new IOException("Can't write beyond end of stream.");
}
InternalStream.Write(buffer);
}
View on GitHub (pinned to 96fad776d2)