Skip to content

Instantly share code, notes, and snippets.

@joshuarubin
Last active July 18, 2016 18:04
Show Gist options
  • Save joshuarubin/18e9bb27cb78a672dd4899bc73eab3de to your computer and use it in GitHub Desktop.
Save joshuarubin/18e9bb27cb78a672dd4899bc73eab3de to your computer and use it in GitHub Desktop.
hodor5
// DefaultChunkSize is the default amount of data read from an io.Reader
const DefaultChunkSize = 1024 * 16
// Process starts to stream data
func Process(r io.Reader) (io.Reader, error) {
var buf, rbuf, result bytes.Buffer
for {
_, err := io.CopyN(&buf, r, DefaultChunkSize)
if err != nil && err != io.EOF {
return nil, err
}
if rerr := Replace(buf.Bytes(), []byte("hodor"), []byte("hold the door"), -1, &rbuf); rerr != nil {
return nil, rerr
}
buf.Reset()
if _, werr := result.Write(rbuf.Bytes()); werr != nil {
return nil, werr
}
if err == io.EOF {
return &result, nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment