From 93063b64a3c39bd8879bdf9465a1830b9fdc0598 Mon Sep 17 00:00:00 2001 From: Joseph Hager Date: Fri, 13 Jul 2012 14:57:50 -0400 Subject: [PATCH] Fix Writers returning n-1 bytes written. ByteWriter and StringWriter were returning n-1 bytes written which was causing io.Copy to error out with a ShortWrite. This would cause files that were larger than the io.Copy internal buffer (32 * 1024) to stop after the first chunk. --- bytewriter.go | 72 +++++++++++++++++++++++++------------------------ stringwriter.go | 72 +++++++++++++++++++++++++------------------------ 2 files changed, 74 insertions(+), 70 deletions(-) diff --git a/bytewriter.go b/bytewriter.go index 8ddaeec..218c939 100644 --- a/bytewriter.go +++ b/bytewriter.go @@ -1,35 +1,37 @@ -// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication -// license. Its contents can be found at: -// http://creativecommons.org/publicdomain/zero/1.0/ - -package main - -import ( - "fmt" - "io" -) - -var newline = []byte{'\n'} - -type ByteWriter struct { - io.Writer - c int -} - -func (w *ByteWriter) Write(p []byte) (n int, err error) { - if len(p) == 0 { - return - } - - for n = range p { - if w.c%12 == 0 { - w.Writer.Write(newline) - w.c = 0 - } - - fmt.Fprintf(w.Writer, "0x%02x,", p[n]) - w.c++ - } - - return -} +// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication +// license. Its contents can be found at: +// http://creativecommons.org/publicdomain/zero/1.0/ + +package main + +import ( + "fmt" + "io" +) + +var newline = []byte{'\n'} + +type ByteWriter struct { + io.Writer + c int +} + +func (w *ByteWriter) Write(p []byte) (n int, err error) { + if len(p) == 0 { + return + } + + for n = range p { + if w.c%12 == 0 { + w.Writer.Write(newline) + w.c = 0 + } + + fmt.Fprintf(w.Writer, "0x%02x,", p[n]) + w.c++ + } + + n++ + + return +} diff --git a/stringwriter.go b/stringwriter.go index 1c1fad0..0e771c7 100644 --- a/stringwriter.go +++ b/stringwriter.go @@ -1,35 +1,37 @@ -// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication -// license. Its contents can be found at: -// http://creativecommons.org/publicdomain/zero/1.0/ - -package main - -import ( - "fmt" - "io" -) - -var line = []byte("\"+\n\"") - -type StringWriter struct { - io.Writer - c int -} - -func (w *StringWriter) Write(p []byte) (n int, err error) { - if len(p) == 0 { - return - } - - for n = range p { - if w.c%16 == 0 { - w.Writer.Write(line) - w.c = 0 - } - - fmt.Fprintf(w.Writer, "\\x%02x", p[n]) - w.c++ - } - - return -} +// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication +// license. Its contents can be found at: +// http://creativecommons.org/publicdomain/zero/1.0/ + +package main + +import ( + "fmt" + "io" +) + +var line = []byte("\"+\n\"") + +type StringWriter struct { + io.Writer + c int +} + +func (w *StringWriter) Write(p []byte) (n int, err error) { + if len(p) == 0 { + return + } + + for n = range p { + if w.c%16 == 0 { + w.Writer.Write(line) + w.c = 0 + } + + fmt.Fprintf(w.Writer, "\\x%02x", p[n]) + w.c++ + } + + n++ + + return +}