Index: src/0dev.org/ioutil/ioutil.go ================================================================== --- src/0dev.org/ioutil/ioutil.go +++ src/0dev.org/ioutil/ioutil.go @@ -25,69 +25,70 @@ // that it is never called with less bytes than the specified amount. // // Calls with fewer bytes are buffered while a call with a nil slice // causes the buffer to be flushed to the underlying writer. func SizedWriter(writer io.Writer, size int) io.Writer { - var buffer []byte = make([]byte, 0, size) - var write WriterFunc - - write = func(input []byte) (int, error) { - var ( - count int - err error - ) - - // Flush the buffer when called with no bytes to write - if input == nil { - // Call the writer with whatever we have in store.. - count, err = writer.Write(buffer) - - // Advance the buffer - buffer = buffer[:copy(buffer, buffer[count:])] - - return 0, err - } - - // Delegate to the writer if the size is right - if len(buffer) == 0 && len(input) >= size { - reduced := (len(input) / size) * size - count, err = writer.Write(input[:reduced]) - if count < reduced || err != nil { - return count, err - } - - // Stage any remaining data in the buffer - buffer = append(buffer, input[count:]...) - return len(input), nil - } - - // Append data to the buffer - count = copy(buffer[len(buffer):size], input) - buffer = buffer[:len(buffer)+count] - - // Return if we don't have enough bytes to write - if len(buffer) < size { - return len(input), nil - } - - // Flush the buffer as it is filled - _, err = write(nil) - if err != nil { - return count, err - } - - // Handle the rest of the input - return write(input[count:]) - } - - return write -} - -type sizedReader struct { - reader io.Reader - buffer []byte - from, to, size int + var sw sizedWriter + sw.writer = writer + sw.buffer = make([]byte, 0, size) + sw.size = size + return &sw +} + +type sizedWriter struct { + writer io.Writer + buffer []byte + size int +} + +func (sw *sizedWriter) Write(input []byte) (int, error) { + var ( + count int + err error + ) + + // Flush the buffer when called with no bytes to write + if input == nil { + // Call the writer with whatever we have in store.. + count, err = sw.writer.Write(sw.buffer) + + // Advance the buffer + sw.buffer = sw.buffer[:copy(sw.buffer, sw.buffer[count:])] + + return 0, err + } + + // Delegate to the writer if the size is right + if len(sw.buffer) == 0 && len(input) >= sw.size { + reduced := (len(input) / sw.size) * sw.size + count, err = sw.writer.Write(input[:reduced]) + if count < reduced || err != nil { + return count, err + } + + // Stage any remaining data in the buffer + sw.buffer = append(sw.buffer, input[count:]...) + return len(input), nil + } + + // Append data to the buffer + count = copy(sw.buffer[len(sw.buffer):sw.size], input) + sw.buffer = sw.buffer[:len(sw.buffer)+count] + + // Return if we don't have enough bytes to write + if len(sw.buffer) < sw.size { + return len(input), nil + } + + // Flush the buffer as it is filled + _, err = sw.Write(nil) + if err != nil { + return count, err + } + + // Handle the rest of the input + return sw.Write(input[count:]) } // Returns a reader that delegates calls to Read(...) while ensuring // that the output buffer is never smaller than the required size // and is downsized to a multiple of the required size if larger. @@ -96,10 +97,16 @@ sr.reader = reader sr.buffer = make([]byte, size) sr.size, sr.from, sr.to = size, 0, 0 return &sr } + +type sizedReader struct { + reader io.Reader + buffer []byte + from, to, size int +} func (sr *sizedReader) Read(output []byte) (int, error) { var ( count int err error