dotnet/wpf · error · NotSupportedException
Stream does not support SetLength.
Error message
Stream does not support SetLength.
What it means
ByteStream.SetLength is explicitly documented as 'Not Supported in this implementation' and unconditionally throws NotSupportedException(SR.SetLengthNotSupported). ByteStream is a read-oriented wrapper around IStream and never supports resizing.
Solutions
- Do not call SetLength on ByteStream; wrap content resizing logic with a CanLength-style check or try/catch.
- Copy to a FileStream/MemoryStream when resizing or writing is needed.
- Restructure code so ByteStream is only used for read/seek operations.
Example fix
// before if (stream.Length != newSize) stream.SetLength(newSize); // after if (stream.CanWrite && stream is not ByteStream) stream.SetLength(newSize);
Defensive patterns
Strategy: validation
Validate before calling
if (!stream.CanWrite) throw new InvalidOperationException("Stream does not support SetLength/write operations"); Type guard
bool SupportsResize(Stream s) => s.CanWrite && s.CanSeek;
Try / catch
try { stream.SetLength(newLength); }
catch (NotSupportedException) { /* switch to a writable FileStream */ } Prevention
- Never call SetLength on read-only/package streams
- Guard resize code with CanWrite
- Copy to a writable stream when mutation is required
When it happens
Trigger: Any call to SetLength(newLength) on a ByteStream, e.g. generic code that truncates or pre-allocates streams via Stream.SetLength before writing.
Common situations: Frameworks or copy utilities that call SetLength to pre-size output when using ByteStream as a destination; code paths that work with FileStream but break when swapped to ByteStream.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- NotSupportedException
- SR.SetLengthNotSupported
- SR.SetPositionNotSupported
- SR.WriteNotSupported
- Stream does not support ReadByte
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/663012cbf19a69d3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs:233
throw new System.ComponentModel.InvalidEnumArgumentException("origin",
(int)origin,
typeof(SeekOrigin));
}
_securitySuppressedIStream.Seek(offset, translatedSeekOrigin, out seekPos);
return seekPos;
}
/// <summary>
/// Sets the length of the current stream.
///
/// Not Supported in this implementation.
/// </summary>
/// <param name="newLength">New length</param>
public override void SetLength(long newLength)
{
throw new NotSupportedException(SR.SetLengthNotSupported);
}
/// <summary>
/// Reads a sequence of bytes from the current stream and advances the
/// position within the stream by the number of bytes read.
/// </summary>
/// <param name="buffer">Read data buffer</param>
/// <param name="offset">Buffer start position</param>
/// <param name="count">Number of bytes to read</param>
/// <returns>Number of bytes actually read</returns>
public override int Read(byte[] buffer, int offset, int count)
{
CheckDisposedStatus();
if (!CanRead)
{
throw new NotSupportedException(SR.ReadNotSupported);
}View on GitHub (pinned to 81131a70a4)