Skip to content

Non-Seekable SubReadStream should not support the Length property. #63040

@JimBobSquarePants

Description

@JimBobSquarePants

Description

According to the documentation for Stream.Length#Exceptions a NotSupportedException should be thrown when .

NotSupportedException
A class derived from Stream does not support seeking.

Reproduction Steps

The following test code should be enough to reproduce. ZipArchive sample taken from SixLabors/ImageSharp#1903

using var zipFile = new ZipArchive(new MemoryStream(
    new byte[]
    {
        0x50, 0x4B, 0x03, 0x04, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xAF,
        0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x6D, 0x79, 0x73, 0x74, 0x65, 0x72,
        0x79, 0x50, 0x4B, 0x01, 0x02, 0x3F, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x77, 0xAF, 0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D,
        0x79, 0x73, 0x74, 0x65, 0x72, 0x79, 0x0A, 0x00, 0x20, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x46, 0x82, 0xFF, 0x91, 0x27, 0xF6,
        0xD7, 0x01, 0x55, 0xA1, 0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x55, 0xA1,
        0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x50, 0x4B, 0x05, 0x06, 0x00, 0x00,
        0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x59, 0x00, 0x00, 0x00, 0x25, 0x00,
        0x00, 0x00, 0x00, 0x00
    }));
using Stream stream = zipFile.Entries[0].Open();
Assert.Throws<NotSupportedException>(() => stream.Length);

Expected behavior

The stream should throw a NotSupportedException

Actual behavior

The stream returns the length.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6 Any OS and architecture.

Other information

No response

Metadata

Metadata

Assignees

Labels

area-System.IO.CompressiondocumentationDocumentation bug or enhancement, does not impact product or test codein-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions