-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixedsizewriter.go
54 lines (46 loc) · 1.17 KB
/
fixedsizewriter.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
package splitmerge
import "io"
type fixedBlockSizeWriter struct {
dst io.WriteCloser
block []byte
offset int
blockSize int
}
var _ io.WriteCloser = &fixedBlockSizeWriter{}
func newFixedBlockSizeWriter(dst io.WriteCloser, blockSize int) io.WriteCloser {
return &fixedBlockSizeWriter{
dst: dst,
block: make([]byte, blockSize),
blockSize: blockSize,
}
}
func (fbsw *fixedBlockSizeWriter) Write(data []byte) (int, error) {
dataOffset := 0
for {
bytes := copy(fbsw.block[fbsw.offset:], data[dataOffset:])
fbsw.offset += bytes
dataOffset += bytes
if fbsw.offset == len(fbsw.block) {
wlen, err := fbsw.dst.Write(fbsw.block)
if err != nil {
fbsw.offset = 0
return dataOffset - fbsw.blockSize + wlen, err
}
// Note: writer.Write() should never retain slice written to it
// so, it is safe to reuse this buffer again.
fbsw.offset = 0
}
if dataOffset == len(data) {
return len(data), nil
}
}
}
func (fbsw *fixedBlockSizeWriter) Close() error {
if fbsw.offset < len(fbsw.block) && fbsw.offset > 0 {
_, err := fbsw.dst.Write(fbsw.block[:fbsw.offset])
if err != nil {
return err
}
}
return fbsw.dst.Close()
}