Remove unused function copyNWithBuf.

This commit is contained in:
Chen Yufei
2015-06-03 00:47:10 +08:00
parent df909fe80e
commit 5325d01407
2 changed files with 0 additions and 115 deletions

61
util.go
View File

@@ -281,67 +281,6 @@ func copyN(dst io.Writer, src *bufio.Reader, n, rdSize int) (err error) {
return err
}
// copyNWithBuf copys N bytes from src to dst, using the specified buf as buffer. pre and
// end are written to w before and after the n bytes. copyN will try to
// minimize number of writes.
// No longer used now.
func copyNWithBuf(dst io.Writer, src io.Reader, n int, buf, pre, end []byte) (err error) {
// XXX well, this is complicated in order to save writes
var nn int
bufLen := len(buf)
var b []byte
for n != 0 {
if pre != nil {
if len(pre) >= bufLen {
// pre is larger than bufLen, can't save write operation here
if _, err = dst.Write(pre); err != nil {
return
}
pre = nil
continue
}
// append pre to buf to save one write
copy(buf, pre)
if len(pre)+n < bufLen {
// only need to read n bytes
b = buf[len(pre) : len(pre)+n]
} else {
b = buf[len(pre):]
}
} else {
if n < bufLen {
b = buf[:n]
} else {
b = buf
}
}
if nn, err = src.Read(b); err != nil {
return
}
n -= nn
if pre != nil {
// nn is how much we need to write next
nn += len(pre)
pre = nil
}
// see if we can append end in buffer to save one write
if n == 0 && end != nil && nn+len(end) <= bufLen {
copy(buf[nn:], end)
nn += len(end)
end = nil
}
if _, err = dst.Write(buf[:nn]); err != nil {
return
}
}
if end != nil {
if _, err = dst.Write(end); err != nil {
return
}
}
return
}
func md5sum(ss ...string) string {
h := md5.New()
for _, s := range ss {

View File

@@ -202,60 +202,6 @@ func TestCopyN(t *testing.T) {
}
}
func TestCopyNWithBuf(t *testing.T) {
testStr := "hello world"
src := bytes.NewBufferString(testStr)
dst := new(bytes.Buffer)
buf := make([]byte, 5)
copyNWithBuf(dst, src, len(testStr), buf, nil, nil)
if dst.String() != "hello world" {
t.Error("copy without pre and end failed, got:", dst.String())
}
src.Reset()
dst.Reset()
src.WriteString(testStr)
copyNWithBuf(dst, src, len(testStr), buf, []byte("by cyf "), nil)
if dst.String() != "by cyf hello world" {
t.Error("copy with pre no end failed, got:", dst.String())
}
src.Reset()
dst.Reset()
src.WriteString(testStr)
copyNWithBuf(dst, src, len(testStr), buf, []byte("by cyf "), []byte(" welcome"))
if dst.String() != "by cyf hello world welcome" {
t.Error("copy with both pre and end failed, got:", dst.String())
}
src.Reset()
dst.Reset()
src.WriteString(testStr)
copyNWithBuf(dst, src, len(testStr), buf, []byte("pre longer then buffer "), []byte(" welcome"))
if dst.String() != "pre longer then buffer hello world welcome" {
t.Error("copy with long pre failed, got:", dst.String())
}
src.Reset()
dst.Reset()
testStr = "34"
src.WriteString(testStr)
copyNWithBuf(dst, src, len(testStr), buf, []byte("12"), []byte(" welcome"))
if dst.String() != "1234 welcome" {
t.Error("copy len(pre)+size<bufLen failed, got:", dst.String())
}
src.Reset()
dst.Reset()
testStr = "2"
src.WriteString(testStr)
copyNWithBuf(dst, src, len(testStr), buf, []byte("1"), []byte("34"))
if dst.String() != "1234" {
t.Error("copy len(pre)+size+len(end)<bufLen failed, got:", dst.String())
}
}
func TestIsFileExists(t *testing.T) {
err := isFileExists("testdata")
if err == nil {