2023-01-15 16:14:19 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
|
2022-10-20 15:53:59 +02:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-10-20 15:52:53 +02:00
|
|
|
package mail
|
|
|
|
|
2022-10-20 18:03:57 +02:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
2022-10-20 15:52:53 +02:00
|
|
|
|
2022-10-20 18:03:57 +02:00
|
|
|
// Reader is a type that implements the io.Reader interface for a Msg
|
|
|
|
type Reader struct {
|
2022-10-20 15:52:53 +02:00
|
|
|
buf []byte // contents are the bytes buf[off : len(buf)]
|
|
|
|
off int // read at &buf[off], write at &buf[len(buf)]
|
2023-05-31 09:34:45 +02:00
|
|
|
err error // initialization error
|
2022-10-20 15:52:53 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 18:26:51 +02:00
|
|
|
// Error returns an error if the Reader err field is not nil
|
|
|
|
func (r *Reader) Error() error {
|
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
|
2022-10-20 15:52:53 +02:00
|
|
|
// Read reads the length of p of the Msg buffer to satisfy the io.Reader interface
|
2022-10-20 18:03:57 +02:00
|
|
|
func (r *Reader) Read(p []byte) (n int, err error) {
|
|
|
|
if r.err != nil {
|
|
|
|
return 0, r.err
|
|
|
|
}
|
2022-10-20 15:52:53 +02:00
|
|
|
if r.empty() {
|
|
|
|
r.Reset()
|
|
|
|
if len(p) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n = copy(p, r.buf[r.off:])
|
|
|
|
r.off += n
|
2022-10-20 18:03:57 +02:00
|
|
|
return n, err
|
2022-10-20 15:52:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets the Reader buffer to be empty, but it retains the underlying storage
|
|
|
|
// for use by future writes.
|
2022-10-20 18:03:57 +02:00
|
|
|
func (r *Reader) Reset() {
|
2022-10-20 15:52:53 +02:00
|
|
|
r.buf = r.buf[:0]
|
|
|
|
r.off = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty reports whether the unread portion of the Reader buffer is empty.
|
2022-10-20 18:03:57 +02:00
|
|
|
func (r *Reader) empty() bool { return len(r.buf) <= r.off }
|