forked from go-iconv/iconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
103 lines (90 loc) · 1.79 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package iconv
import (
"io"
"syscall"
)
type Writer struct {
inbuf []byte
outbuf []byte
cd Iconv
output io.Writer
n int // inbuf[0:n] is valid
autoSync bool
}
func NewWriter(cd Iconv, output io.Writer, bufSize int, autoSync bool) *Writer {
if bufSize < 16 { bufSize = DefaultBufSize }
outbuf := make([]byte, bufSize)
var inbuf []byte
if !autoSync {
inbuf = make([]byte, bufSize)
}
return &Writer{inbuf, outbuf, cd, output, 0, autoSync}
}
func (w *Writer) Output(w1 io.Writer) {
w.Sync()
w.output = w1
w.n = 0
}
func (w *Writer) AutoSync(b bool) {
w.autoSync = b
if !b && w.inbuf == nil {
w.inbuf = make([]byte, len(w.outbuf))
}
}
func (w *Writer) Sync() error {
if w.n == 0 { return nil }
inleft, err := w.cd.DoWrite(w.output, w.inbuf, w.n, w.outbuf)
if inleft > 0 {
copy(w.inbuf, w.inbuf[w.n-inleft:w.n])
}
w.n = inleft
return err
}
func (w *Writer) Write(b []byte) (n int, err error) {
if w.autoSync {
var inleft int
inleft, err = w.cd.DoWrite(w.output, b, len(b), w.outbuf)
n = len(b) - inleft
return
}
for {
n1 := copy(w.inbuf[w.n:], b)
if n1 == 0 {
if len(b) > 0 { return n, EILSEQ }
break
}
w.n += n1
n += n1
if w.n == len(w.inbuf) {
err = w.Sync()
if err != nil && err != syscall.EINVAL { return }
}
if len(b) == n1 { break }
b = b[n1:]
}
return n, nil
}
func (w *Writer) WriteString(b string) (n int, err error) {
if w.autoSync {
var inleft int
inleft, err = w.cd.DoWrite(w.output, []byte(b), len(b), w.outbuf)
n = len(b) - inleft
return
}
for {
n1 := copy(w.inbuf[w.n:], b)
if n1 == 0 {
if len(b) > 0 { return n, EILSEQ }
break
}
w.n += n1
n += n1
if w.n == len(w.inbuf) {
err = w.Sync()
if err != nil && err != syscall.EINVAL { return }
}
if len(b) == n1 { break }
b = b[n1:]
}
return n, nil
}