Who is responsible for stream positioning?

The samples in question are c#, but it applies to any language.

If you have a function which reads content from a stream. Who should be responsible for ensuring that the stream position is correct before reading?

The caller, or the function doing the reading?

e.g.

Caller positions the stream:

    public byte[] ReadBytesFromStream(Stream inputStream)
    {
        using (var ms = new MemoryStream())
        {
            inputStream.CopyTo(ms);
            return ms.ToArray();
        }
    }

    public void DoThingWithStream(Stream stream)
    {
        // .. do some stuff
        stream.Position = 0;
        var bytes = ReadBytesFromStream(stream);
        // .. do some more stuff
    }

Reader positions the stream:

    public byte[] ReadBytesFromStream(Stream inputStream)
    {
        inputStream.Position = 0;
        using (var ms = new MemoryStream())
        {
            inputStream.CopyTo(ms);
            return ms.ToArray();
        }
    }

    public void DoThingWithStream(Stream stream)
    {
        // .. do some stuff
        var bytes = ReadBytesFromStream(stream);
        // .. do some more stuff
    }

The reader positioning the stream means that I can call the function over an over again with the same stream and get the same bytes out.

The caller positioning the stream means that

  1. It’s a little more work to set up and if you’re throwing the stream around a lot there may be a lot of re-positioning going on. But,
  2. I could use the same reader function to read from an arbitrary point in the stream if I really wanted to.

I’ve pretty much convinced myself that the caller should do the positioning, but if that’s the case then are there any circumstances when it shouldn’t?

1

The caller is responsible for putting the Stream in a suitable state to be consumed by the reader. There are two main reasons:

  1. Not all streams support repositioning. You should only set the Stream.Position property if that Stream.CanSeek. Otherwise, you’ll get an exception. (see the Stream.Position documentation).

  2. “Reading from the beginning” is a special case of simply “reading”. It is generally advisable to write your functions as general as possible (as far as it’s sensible, compare also YAGNI). If we frequently need a special case, we can always add a convenience wrapper with very little effort. However, getting a general function out of a special-cased function is not possible (or requires refactoring of the code).

    Especially when creating a public interface is it important to make this interface composable. One possibility would be to pass in the wanted location as a parameter. This works in general, but not in this specific case because not every Stream.CanSeek.

And then there’s also the matter of avoiding “spooky action at a distance”. It’s irrelevant what your function does as long as the effects are contained inside that function. However, mutating non-local state can lead to hard to locate bugs. If functions have side effects or unexpectedly mutate their arguments, this should perhaps be reflected in their name, but certainly in their documentation. In this case, it’s expected that the reader function will advance the position by reading from the stream. However, it’s probably not expected that the reader will reset the position first.

If we work backwards from that, we can find a few cases where the reader method should set the position:

  • In the context of your application, you’ll only ever want to read streams from the beginning, and read the same stream multiple times. You’ve documented the behaviour that, as a convenience, the reader method will reset the position first.

  • The method is private. That’s a carte blanche to ignore any argument referring to “composability”, since you control all uses of that method.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật