You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-library/vendor/github.com/gogf/gf/v2/internal/utils/utils_io.go

49 lines
1.2 KiB

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package utils
import (
"io"
)
// ReadCloser implements the io.ReadCloser interface
// which is used for reading request body content multiple times.
//
// Note that it cannot be closed.
type ReadCloser struct {
index int // Current read position.
content []byte // Content.
repeatable bool // Mark the content can be repeatable read.
}
// NewReadCloser creates and returns a RepeatReadCloser object.
func NewReadCloser(content []byte, repeatable bool) io.ReadCloser {
return &ReadCloser{
content: content,
repeatable: repeatable,
}
}
// Read implements the io.ReadCloser interface.
func (b *ReadCloser) Read(p []byte) (n int, err error) {
// Make it repeatable reading.
if b.index >= len(b.content) && b.repeatable {
b.index = 0
}
n = copy(p, b.content[b.index:])
b.index += n
if b.index >= len(b.content) {
return n, io.EOF
}
return n, nil
}
// Close implements the io.ReadCloser interface.
func (b *ReadCloser) Close() error {
return nil
}