Files
mayfly-go/server/pkg/utils/writerx/counting_writer.go
2024-10-21 22:27:42 +08:00

27 lines
492 B
Go

package writerx
import "io"
type CountingWriteCloser struct {
w io.WriteCloser
n int64 // 已写入的字节数
}
func (c *CountingWriteCloser) Write(p []byte) (int, error) {
n, err := c.w.Write(p)
c.n += int64(n)
return n, err
}
func (c *CountingWriteCloser) Close() error {
return c.w.Close()
}
func (c *CountingWriteCloser) BytesWritten() int64 {
return c.n
}
func NewCountingWriteCloser(writer io.WriteCloser) *CountingWriteCloser {
return &CountingWriteCloser{w: writer}
}