Created
June 16, 2017 00:16
-
-
Save dakerfp/e1c5b6eb2fa3f6d8e920b7c87dc490f7 to your computer and use it in GitHub Desktop.
Utility to pad a writer with zeros
This file contains 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 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