Skip to content

Sizable Stream Readers (Range Requests)

Compare
Choose a tag to compare
@djherbis djherbis released this 03 Mar 06:30
· 33 commits to master since this release

Stream Reader supports a new method called Size(). Size returns the size of the entire stream at this moment, and a bool which is true if and only if the stream is closed. If the stream is closed, then the size will be constant from then on, and range requests can be done via an io.SectionReader over the reader like so:

var r stream.Reader
if size, isClosed := r.Size(); isClosed {
    sr := io.NewSectionReader(r, 0, size)
    http.ServeContent(rw, req, name, modTime, sr)
}
// otherwise just do regular request writing