forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
null.go
76 lines (62 loc) · 1.72 KB
/
null.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
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Discard implementation copied from the Go project:
// https://golang.org/src/io/ioutil/ioutil.go.
// Copyright 2009 The Go Authors. All rights reserved.
// Package null extends ioutil.Discard and adds an io.WriteCloser
// and WriteNameCloser for use within u-root.
package null
import (
"io"
"sync"
)
type devNull int
// devNull implements ReaderFrom as an optimization so io.Copy to
// ioutil.Discard can avoid doing unnecessary work.
var _ io.ReaderFrom = devNull(0)
func (devNull) Write(p []byte) (int, error) {
return len(p), nil
}
func (devNull) Name() string {
return "null"
}
func (devNull) WriteString(s string) (int, error) {
return len(s), nil
}
var blackHolePool = sync.Pool{
New: func() interface{} {
b := make([]byte, 8192)
return &b
},
}
func (devNull) ReadFrom(r io.Reader) (n int64, err error) {
bufp := blackHolePool.Get().(*[]byte)
readSize := 0
for {
readSize, err = r.Read(*bufp)
n += int64(readSize)
if err != nil {
blackHolePool.Put(bufp)
if err == io.EOF {
return n, nil
}
return
}
}
}
func (devNull) Close() error {
return nil
}
// WriteNameCloser is the interface that groups Write, Close, and Name methods.
type WriteNameCloser interface {
io.Writer
io.Closer
Name() string
}
// WriteNameClose is an WriteNameCloser on which all Write and Close calls succeed
// without doing anything, and the Name call returns "null".
var WriteNameClose WriteNameCloser = devNull(0)
// WriteClose is an WriteCloser on which all Write and Close calls succeed
// without doing anything.
var WriteClose io.WriteCloser = devNull(0)