-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioutils.go
135 lines (121 loc) · 2.31 KB
/
ioutils.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2024 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package ioutils
import (
"bytes"
"fmt"
"io"
"go.osspkg.com/errors"
)
func ReadAll(r io.ReadCloser) ([]byte, error) {
b, err := io.ReadAll(r)
err = errors.Wrap(err, r.Close())
if err != nil {
return nil, err
}
return b, nil
}
const buffSize = 128
var (
ErrMaximumSize = errors.New("maximum buffer size reached")
ErrInvalidSize = errors.New("invalid size")
)
func ReadFull(w io.Writer, r io.Reader, maxSize int) error {
if maxSize < 0 {
return ErrInvalidSize
}
// nolint: ineffassign
var (
total = 0
n = 0
buff = make([]byte, buffSize)
err error
)
for {
n, err = r.Read(buff)
if err != nil && !errors.Is(err, io.EOF) {
return err
}
if n < 0 {
return ErrInvalidSize
}
if _, err = w.Write(buff[:n]); err != nil {
return err
}
total += n
if maxSize > 0 && total > maxSize {
return ErrMaximumSize
}
if n < buffSize {
break
}
}
return nil
}
func ReadBytes(v io.Reader, divide string) ([]byte, error) {
var (
n int
err error
b = make([]byte, 0, 512)
db = []byte(divide)
dl = len(db)
)
for {
if len(b) == cap(b) {
b = append(b, 0)[:len(b)]
}
n, err = v.Read(b[len(b):cap(b)])
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
if len(b) < dl {
return b, io.EOF
}
if bytes.Equal(db, b[len(b)-dl:]) {
b = b[:len(b)-dl]
break
}
}
return b, nil
}
func WriteBytes(v io.Writer, b []byte, divide string) error {
var (
db = []byte(divide)
dl = len(db)
)
if len(b) < dl || !bytes.Equal(db, b[len(b)-dl:]) {
b = append(b, db...)
}
if _, err := v.Write(b); err != nil {
return err
}
return nil
}
const copyBufferSize = 512
func Copy(w io.Writer, r io.Reader) (int, error) {
n := 0
buff := make([]byte, copyBufferSize)
for {
m, err1 := r.Read(buff)
if m < 0 {
return 0, fmt.Errorf("reader err: negative read bytes")
}
if err1 != nil && !errors.Is(err1, io.EOF) {
return 0, err1
}
n += m
_, err2 := w.Write(buff[:m])
if err2 != nil {
return 0, fmt.Errorf("writer err: %w", err2)
}
if m < copyBufferSize || errors.Is(err1, io.EOF) {
return n, nil
}
}
}