1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/piperutils/ioUtils.go
Sven Merk ab9e154d10
Replace io.Copy (#2934)
* Replace io.Copy

* Test coverage

* Improve test

* Fix fmt

* Improve error handling in test

* Fix code

* Improve test error log

* Fix fmt

* Fix unix file handles

* Fix error message

* Resolve code climate issue
2021-06-23 14:41:52 +02:00

40 lines
839 B
Go

package piperutils
import (
"fmt"
"io"
"github.com/pkg/errors"
)
// CopyData transfers the bytes from src to dst without doing close handling implicitly.
func CopyData(dst io.Writer, src io.Reader) (int64, error) {
tmp := make([]byte, 256)
bytesRead := int64(0)
bytesWritten := int64(0)
done := false
for {
nr, err := src.Read(tmp)
bytesRead += int64(nr)
if err != nil {
if err != io.EOF {
return bytesRead, errors.Wrap(err, "read error")
}
done = true
}
nw, err := dst.Write(tmp[:nr])
bytesWritten += int64(nw)
if err != nil {
return bytesWritten, errors.Wrap(err, "write error")
}
if done {
break
}
}
if bytesRead != bytesWritten {
return bytesRead, errors.New(fmt.Sprintf("transfer error: read %v bytes but wrote %v bytes", bytesRead, bytesWritten))
}
return bytesWritten, nil
}