Skip to content

Commit 72ec6e8

Browse files
committed
Add example about ReadFrom + bufio interaction
Fixes #125
1 parent f6a9258 commit 72ec6e8

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

example_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package sftp_test
22

33
import (
4+
"bufio"
45
"fmt"
6+
"io"
57
"log"
68
"os"
79
"os/exec"
@@ -107,13 +109,13 @@ func ExampleClient_Mkdir_parents() {
107109
sshFxFailure := uint32(4)
108110
mkdirParents := func(client *sftp.Client, dir string) (err error) {
109111
var parents string
110-
112+
111113
if path.IsAbs(dir) {
112114
// Otherwise, an absolute path given below would be turned in to a relative one
113115
// by splitting on "/"
114116
parents = "/"
115117
}
116-
118+
117119
for _, name := range strings.Split(dir, "/") {
118120
if name == "" {
119121
// Paths with double-/ in them should just move along
@@ -145,3 +147,18 @@ func ExampleClient_Mkdir_parents() {
145147
log.Fatal(err)
146148
}
147149
}
150+
151+
func ExampleFile_ReadFrom_bufio() {
152+
// Using Bufio to buffer writes going to an sftp.File won't buffer as it
153+
// skips buffering if the underlying writer support ReadFrom. The
154+
// workaround is to wrap your writer in a struct that only implements
155+
// io.Writer.
156+
//
157+
// For background see github.com/pkg/sftp/issues/125
158+
159+
var data_source io.Reader
160+
var f *sftp.File
161+
type writerOnly struct{ io.Writer }
162+
bw := bufio.NewWriter(writerOnly{f}) // no ReadFrom()
163+
bw.ReadFrom(data_source)
164+
}

0 commit comments

Comments
 (0)