Skip to content

Instantly share code, notes, and snippets.

@dakerfp
Created June 16, 2017 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dakerfp/e1c5b6eb2fa3f6d8e920b7c87dc490f7 to your computer and use it in GitHub Desktop.
Save dakerfp/e1c5b6eb2fa3f6d8e920b7c87dc490f7 to your computer and use it in GitHub Desktop.
Utility to pad a writer with zeros
package zero
import (
"io"
)
type ZeroPad struct {
w io.WriteCloser
nleft int
}
func (z *ZeroPad) Fill() (err error) {
var i, n int
for i = 0; i < z.nleft; i += n {
n, err = z.w.Write([]byte{0})
if err != nil {
return
}
}
// ZeroPad should be invalid after this
z.nleft -= i
return
}
func (z *ZeroPad) Write(b []byte) (int, error) {
n, err := z.w.Write(b)
z.nleft -= n
return n, err
}
func (z *ZeroPad) Close() error {
err := z.Fill()
if err != nil {
return err
}
return z.w.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment