Created
April 23, 2014 14:21
-
-
Save marcinwyszynski/11217122 to your computer and use it in GitHub Desktop.
ChunkedReader is an io.Reader which runs a callback after each read chunk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chunked_reader | |
import "io" | |
// ChunkedReader implements io.Reader interface while allowing the source to be | |
// read in set increments and running a callback function after each increment. | |
type ChunkedReader struct { | |
readSoFar int64 | |
Reader io.Reader // The underlying io.Reader. | |
ChunkSize int // Maximum bytes to read on each Read call. | |
Callback func(int64) error // Callback to run on each non-zero read. | |
} | |
// Read implements implements io.Reader interface. It resizes the buffer to the | |
// ChunkedReader's maximum chunk size and if the number of bytes read is greater | |
// than zero it also runs a provided callback. This method returns a non-nil | |
// error either if the underlying Read operation OR the callback return one. | |
func (c *ChunkedReader) Read(p []byte) (n int, err error) { | |
if len(p) > c.ChunkSize { | |
p = p[:c.ChunkSize] | |
} | |
n, err = c.Reader.Read(p) | |
if n == 0 { | |
return | |
} | |
c.readSoFar += int64(n) | |
err = c.Callback(c.readSoFar) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment