Commit 0e368363 authored by Kirill Smelkov's avatar Kirill Smelkov

xio: Convert CountReader from io.Reader -> to xio.Reader

Done toprovide uniform API in xio package.
Compensate for API change in places where xio.CountReader was used.
parent 7ad867a3
// Copyright (C) 2017 Nexedi SA and Contributors.
// Copyright (C) 2017-2019 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
......@@ -22,6 +22,7 @@ package xbufio
import (
"bufio"
"context"
"io"
"lab.nexedi.com/kirr/go123/xio"
......@@ -40,12 +41,13 @@ func NewReader(r io.Reader) *Reader {
}
// idempotent(xio.CountedReader)
cr, ok := r.(*xio.CountedReader)
xr := xio.WithCtxR(r)
cr, ok := xr.(*xio.CountedReader)
if !ok {
cr = xio.CountReader(r)
cr = xio.CountReader(xr)
}
return &Reader{bufio.NewReader(cr), cr}
return &Reader{bufio.NewReader(xio.BindCtxR(cr, context.Background())), cr}
}
// InputOffset returns current logical position in input stream.
......
......@@ -26,6 +26,10 @@
// It is the opposite operation for BindCtx, but for arbitrary io.X
// returned xio.X handles context only on best-effort basis. In
// particular IO cancellation is not reliably handled for os.File .
//
// Miscellaneous utilities:
//
// - CountReader provides InputOffset for a Reader.
package xio
import (
......@@ -272,14 +276,14 @@ func (s *stubCtxRWC) Close() error { return s.rw.Close() }
// ----------------------------------------
// CountedReader is an io.Reader that count total bytes read.
// CountedReader is a Reader that count total bytes read.
type CountedReader struct {
r io.Reader
r Reader
nread int64
}
func (cr *CountedReader) Read(p []byte) (int, error) {
n, err := cr.r.Read(p)
func (cr *CountedReader) Read(ctx context.Context, p []byte) (int, error) {
n, err := cr.r.Read(ctx, p)
cr.nread += int64(n)
return n, err
}
......@@ -290,6 +294,6 @@ func (cr *CountedReader) InputOffset() int64 {
}
// CountReader wraps r with CountedReader.
func CountReader(r io.Reader) *CountedReader {
func CountReader(r Reader) *CountedReader {
return &CountedReader{r, 0}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment