1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
// Package ioutil implements some I/O utility functions.
|
6 |
|
|
package ioutil
|
7 |
|
|
|
8 |
|
|
import (
|
9 |
|
|
"bytes"
|
10 |
|
|
"io"
|
11 |
|
|
"os"
|
12 |
|
|
"sort"
|
13 |
|
|
)
|
14 |
|
|
|
15 |
|
|
// readAll reads from r until an error or EOF and returns the data it read
|
16 |
|
|
// from the internal buffer allocated with a specified capacity.
|
17 |
|
|
func readAll(r io.Reader, capacity int64) (b []byte, err error) {
|
18 |
|
|
buf := bytes.NewBuffer(make([]byte, 0, capacity))
|
19 |
|
|
// If the buffer overflows, we will get bytes.ErrTooLarge.
|
20 |
|
|
// Return that as an error. Any other panic remains.
|
21 |
|
|
defer func() {
|
22 |
|
|
e := recover()
|
23 |
|
|
if e == nil {
|
24 |
|
|
return
|
25 |
|
|
}
|
26 |
|
|
if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge {
|
27 |
|
|
err = panicErr
|
28 |
|
|
} else {
|
29 |
|
|
panic(e)
|
30 |
|
|
}
|
31 |
|
|
}()
|
32 |
|
|
_, err = buf.ReadFrom(r)
|
33 |
|
|
return buf.Bytes(), err
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
// ReadAll reads from r until an error or EOF and returns the data it read.
|
37 |
|
|
// A successful call returns err == nil, not err == EOF. Because ReadAll is
|
38 |
|
|
// defined to read from src until EOF, it does not treat an EOF from Read
|
39 |
|
|
// as an error to be reported.
|
40 |
|
|
func ReadAll(r io.Reader) ([]byte, error) {
|
41 |
|
|
return readAll(r, bytes.MinRead)
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
// ReadFile reads the file named by filename and returns the contents.
|
45 |
|
|
// A successful call returns err == nil, not err == EOF. Because ReadFile
|
46 |
|
|
// reads the whole file, it does not treat an EOF from Read as an error
|
47 |
|
|
// to be reported.
|
48 |
|
|
func ReadFile(filename string) ([]byte, error) {
|
49 |
|
|
f, err := os.Open(filename)
|
50 |
|
|
if err != nil {
|
51 |
|
|
return nil, err
|
52 |
|
|
}
|
53 |
|
|
defer f.Close()
|
54 |
|
|
// It's a good but not certain bet that FileInfo will tell us exactly how much to
|
55 |
|
|
// read, so let's try it but be prepared for the answer to be wrong.
|
56 |
|
|
fi, err := f.Stat()
|
57 |
|
|
var n int64
|
58 |
|
|
if size := fi.Size(); err == nil && size < 2e9 { // Don't preallocate a huge buffer, just in case.
|
59 |
|
|
n = size
|
60 |
|
|
}
|
61 |
|
|
// As initial capacity for readAll, use n + a little extra in case Size is zero,
|
62 |
|
|
// and to avoid another allocation after Read has filled the buffer. The readAll
|
63 |
|
|
// call will read into its allocated internal buffer cheaply. If the size was
|
64 |
|
|
// wrong, we'll either waste some space off the end or reallocate as needed, but
|
65 |
|
|
// in the overwhelmingly common case we'll get it just right.
|
66 |
|
|
return readAll(f, n+bytes.MinRead)
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
// WriteFile writes data to a file named by filename.
|
70 |
|
|
// If the file does not exist, WriteFile creates it with permissions perm;
|
71 |
|
|
// otherwise WriteFile truncates it before writing.
|
72 |
|
|
func WriteFile(filename string, data []byte, perm os.FileMode) error {
|
73 |
|
|
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
|
74 |
|
|
if err != nil {
|
75 |
|
|
return err
|
76 |
|
|
}
|
77 |
|
|
n, err := f.Write(data)
|
78 |
|
|
f.Close()
|
79 |
|
|
if err == nil && n < len(data) {
|
80 |
|
|
err = io.ErrShortWrite
|
81 |
|
|
}
|
82 |
|
|
return err
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
// byName implements sort.Interface.
|
86 |
|
|
type byName []os.FileInfo
|
87 |
|
|
|
88 |
|
|
func (f byName) Len() int { return len(f) }
|
89 |
|
|
func (f byName) Less(i, j int) bool { return f[i].Name() < f[j].Name() }
|
90 |
|
|
func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
|
91 |
|
|
|
92 |
|
|
// ReadDir reads the directory named by dirname and returns
|
93 |
|
|
// a list of sorted directory entries.
|
94 |
|
|
func ReadDir(dirname string) ([]os.FileInfo, error) {
|
95 |
|
|
f, err := os.Open(dirname)
|
96 |
|
|
if err != nil {
|
97 |
|
|
return nil, err
|
98 |
|
|
}
|
99 |
|
|
list, err := f.Readdir(-1)
|
100 |
|
|
f.Close()
|
101 |
|
|
if err != nil {
|
102 |
|
|
return nil, err
|
103 |
|
|
}
|
104 |
|
|
sort.Sort(byName(list))
|
105 |
|
|
return list, nil
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
type nopCloser struct {
|
109 |
|
|
io.Reader
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
func (nopCloser) Close() error { return nil }
|
113 |
|
|
|
114 |
|
|
// NopCloser returns a ReadCloser with a no-op Close method wrapping
|
115 |
|
|
// the provided Reader r.
|
116 |
|
|
func NopCloser(r io.Reader) io.ReadCloser {
|
117 |
|
|
return nopCloser{r}
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
type devNull int
|
121 |
|
|
|
122 |
|
|
// devNull implements ReaderFrom as an optimization so io.Copy to
|
123 |
|
|
// ioutil.Discard can avoid doing unnecessary work.
|
124 |
|
|
var _ io.ReaderFrom = devNull(0)
|
125 |
|
|
|
126 |
|
|
func (devNull) Write(p []byte) (int, error) {
|
127 |
|
|
return len(p), nil
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
var blackHole = make([]byte, 8192)
|
131 |
|
|
|
132 |
|
|
func (devNull) ReadFrom(r io.Reader) (n int64, err error) {
|
133 |
|
|
readSize := 0
|
134 |
|
|
for {
|
135 |
|
|
readSize, err = r.Read(blackHole)
|
136 |
|
|
n += int64(readSize)
|
137 |
|
|
if err != nil {
|
138 |
|
|
if err == io.EOF {
|
139 |
|
|
return n, nil
|
140 |
|
|
}
|
141 |
|
|
return
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
panic("unreachable")
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
// Discard is an io.Writer on which all Write calls succeed
|
148 |
|
|
// without doing anything.
|
149 |
|
|
var Discard io.Writer = devNull(0)
|