-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathlimitreadcloser.go
81 lines (73 loc) · 1.78 KB
/
limitreadcloser.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
77
78
79
80
81
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package streams
import (
"errors"
"io"
)
/*!
Adapted from the Go 1.18.3 source code
Copyright 2009 The Go Authors. All rights reserved.
License: BSD (https://github.com/golang/go/blob/go1.18.3/LICENSE)
*/
// ErrStreamTooLarge is returned by LimitReadCloser when the stream is too large.
var ErrStreamTooLarge = errors.New("stream too large")
// LimitReadCloser returns a ReadCloser that reads from r but stops with ErrStreamTooLarge after n bytes.
func LimitReadCloser(r io.ReadCloser, n int64) io.ReadCloser {
return &limitReadCloser{
R: r,
N: n,
}
}
type limitReadCloser struct {
R io.ReadCloser
N int64
closed bool
}
func (l *limitReadCloser) Read(p []byte) (n int, err error) {
if l.N < 0 || l.R == nil {
return 0, ErrStreamTooLarge
}
if len(p) == 0 {
return 0, nil
}
if l.closed {
return 0, io.EOF
}
if int64(len(p)) > (l.N + 1) {
p = p[0:(l.N + 1)]
}
n, err = l.R.Read(p)
l.N -= int64(n)
if l.N < 0 {
// Special case if we just read the "l.N+1" byte
if l.N == -1 {
n--
}
if err == nil {
err = ErrStreamTooLarge
}
if !l.closed {
l.closed = true
l.R.Close()
}
}
return
}
func (l *limitReadCloser) Close() error {
if l.closed {
return nil
}
l.closed = true
return l.R.Close()
}