forked from Terry-Mao/goim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufio.go
47 lines (40 loc) · 859 Bytes
/
bufio.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
package main
import (
"bufio"
"io"
"sync"
)
func NewBufioReaderSize(pool *sync.Pool, r io.Reader, size int) *bufio.Reader {
if v := pool.Get(); v != nil {
br := v.(*bufio.Reader)
br.Reset(r)
return br
}
return bufio.NewReaderSize(r, size)
}
func PutBufioReader(pool *sync.Pool, br *bufio.Reader) {
br.Reset(nil)
pool.Put(br)
}
func NewBufioWriterSize(pool *sync.Pool, w io.Writer, size int) *bufio.Writer {
if v := pool.Get(); v != nil {
bw := v.(*bufio.Writer)
bw.Reset(w)
return bw
}
return bufio.NewWriterSize(w, size)
}
func PutBufioWriter(pool *sync.Pool, bw *bufio.Writer) {
bw.Reset(nil)
pool.Put(bw)
}
func NewByteArraySize(pool *sync.Pool, size int) []byte {
if v := pool.Get(); v != nil {
ba := v.([]byte)
return ba
}
return make([]byte, size)
}
func PutByteArray(pool *sync.Pool, ba []byte) {
pool.Put(ba)
}