forked from johannesboyne/gofakes3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
57 lines (49 loc) · 1.16 KB
/
util.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
package gofakes3
import (
"io"
"strconv"
)
func parseClampedInt(in string, defaultValue, min, max int64) (int64, error) {
var v int64
if in == "" {
v = defaultValue
} else {
var err error
v, err = strconv.ParseInt(in, 10, 0)
if err != nil {
return defaultValue, ErrInvalidArgument
}
}
if v < min {
v = min
} else if v > max {
v = max
}
return v, nil
}
// ReadAll is a fakeS3-centric replacement for io.ReadAll(), for use when
// the size of the result is known ahead of time. It is considerably faster to
// preallocate the entire slice than to allow growslice to be triggered
// repeatedly, especially with larger buffers.
//
// It also reports S3-specific errors in certain conditions, like
// ErrIncompleteBody.
func ReadAll(r io.Reader, size int64) (b []byte, err error) {
var n int
b = make([]byte, size)
n, err = io.ReadFull(r, b)
if err == io.ErrUnexpectedEOF {
return nil, ErrIncompleteBody
} else if err != nil {
return nil, err
}
if n != int(size) {
return nil, ErrIncompleteBody
}
if extra, err := io.ReadAll(r); err != nil {
return nil, err
} else if len(extra) > 0 {
return nil, ErrIncompleteBody
}
return b, nil
}