buf [14]byte
used int // number of bytes in the current line
n uint // number of bytes, total
+ closed bool
}
func toChar(b byte) byte {
}
func (h *dumper) Write(data []byte) (n int, err error) {
+ if h.closed {
+ return 0, errors.New("encoding/hex: dumper closed")
+ }
+
// Output lines look like:
// 00000010 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d |./0123456789:;<=|
// ^ offset ^ extra space ^ ASCII of line.
func (h *dumper) Close() (err error) {
// See the comments in Write() for the details of this format.
- if h.used == 0 {
+ if h.used == 0 || h.closed {
return
}
+ h.closed = true
h.buf[0] = ' '
h.buf[1] = ' '
h.buf[2] = ' '
}
}
+func TestDumper_doubleclose(t *testing.T) {
+ var out bytes.Buffer
+ dumper := Dumper(&out)
+
+ dumper.Write([]byte(`gopher`))
+ dumper.Close()
+ dumper.Close()
+ dumper.Write([]byte(`gopher`))
+ dumper.Close()
+
+ expected := "00000000 67 6f 70 68 65 72 |gopher|\n"
+ if out.String() != expected {
+ t.Fatalf("got:\n%#v\nwant:\n%#v", out.String(), expected)
+ }
+}
+
func TestDump(t *testing.T) {
var in [40]byte
for i := range in {