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
|
|
|
|
2024-10-06 17:05:04 +02:00
|
|
|
// Reader is a type that implements the io.Reader interface for a Msg.
|
|
|
|
//
|
|
|
|
// This struct represents a reader that reads from a byte slice buffer. It keeps track of the
|
|
|
|
// current read position (offset) and any initialization error. The buffer holds the data to be
|
|
|
|
// read from the message.
|
2022-10-20 18:03:57 +02:00
|
|
|
type Reader struct {
|
2024-02-26 00:56:29 +01:00
|
|
|
buffer []byte // contents are the bytes buffer[offset : len(buffer)]
|
|
|
|
offset int // read at &buffer[offset], write at &buffer[len(buffer)]
|
|
|
|
err error // initialization error
|
2022-10-20 15:52:53 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 17:05:04 +02:00
|
|
|
// Error returns an error if the Reader err field is not nil.
|
|
|
|
//
|
|
|
|
// This function checks the Reader's err field and returns it if it is not nil. If no error
|
|
|
|
// occurred during initialization, it returns nil.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - The error stored in the err field, or nil if no error is present.
|
2022-10-20 18:26:51 +02:00
|
|
|
func (r *Reader) Error() error {
|
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
|
2024-10-06 17:05:04 +02:00
|
|
|
// Read reads the content of the Msg buffer into the provided payload to satisfy the io.Reader interface.
|
|
|
|
//
|
|
|
|
// This function reads data from the Reader's buffer into the provided byte slice (payload).
|
|
|
|
// It checks for errors or an empty buffer and resets the Reader if necessary. If no data is available,
|
|
|
|
// it returns io.EOF. Otherwise, it copies the content from the buffer into the payload and updates
|
|
|
|
// the read offset.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - payload: A byte slice where the data will be copied.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - n: The number of bytes copied into the payload.
|
|
|
|
// - err: An error if any issues occurred during the read operation or io.EOF if the buffer is empty.
|
2024-02-26 00:56:29 +01:00
|
|
|
func (r *Reader) Read(payload []byte) (n int, err error) {
|
2022-10-20 18:03:57 +02:00
|
|
|
if r.err != nil {
|
|
|
|
return 0, r.err
|
|
|
|
}
|
2024-02-26 00:56:29 +01:00
|
|
|
if r.empty() || r.buffer == nil {
|
2022-10-20 15:52:53 +02:00
|
|
|
r.Reset()
|
2024-02-26 00:56:29 +01:00
|
|
|
if len(payload) == 0 {
|
2022-10-20 15:52:53 +02:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2024-02-26 00:56:29 +01:00
|
|
|
n = copy(payload, r.buffer[r.offset:])
|
|
|
|
r.offset += n
|
2022-10-20 18:03:57 +02:00
|
|
|
return n, err
|
2022-10-20 15:52:53 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 17:05:04 +02:00
|
|
|
// Reset resets the Reader buffer to be empty, but it retains the underlying storage for future use.
|
|
|
|
//
|
|
|
|
// This function clears the Reader's buffer by setting its length to 0 and resets the read offset
|
|
|
|
// to the beginning. The underlying storage is retained, allowing future writes to reuse the buffer.
|
2022-10-20 18:03:57 +02:00
|
|
|
func (r *Reader) Reset() {
|
2024-02-26 00:56:29 +01:00
|
|
|
r.buffer = r.buffer[:0]
|
|
|
|
r.offset = 0
|
2022-10-20 15:52:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// empty reports whether the unread portion of the Reader buffer is empty.
|
2024-10-06 17:05:04 +02:00
|
|
|
//
|
|
|
|
// This function checks if the unread portion of the Reader's buffer is empty by comparing
|
|
|
|
// the length of the buffer to the current read offset.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - true if the unread portion is empty, false otherwise.
|
2024-02-26 00:56:29 +01:00
|
|
|
func (r *Reader) empty() bool { return len(r.buffer) <= r.offset }
|