AS I wrote earlier, I'm trying to avoid reading the entire email part into memory to discover if I should use base64.StdEncoding or base64.RawStdEncoding.
The following seems to work reasonably well:
type B64Translator struct {
br *bufio.Reader
}
func NewB64Translator(r io.Reader) *B64Translator {
return &B64Translator{
br: bufio.NewReader(r),
}
}
// Read reads off the buffered reader expecting base64.StdEncoding bytes
// with (potentially) 1-3 '=' padding characters at the end.
// RawStdEncoding can be used for both StdEncoded and RawStdEncoded data
// if the padding is removed.
func (b *B64Translator) Read(p []byte) (n int, err error) {
h := make([]byte, len(p))
n, err = b.br.Read(h)
if err != nil {
return n, err
}
// to be optimised
c := bytes.Count(h, []byte("="))
copy(p, h[:n-c])
// fmt.Println(string(h), n, string(p), n-c)
return n - c, nil
}
https://go.dev/play/p/H6ii7Vy-8as
One odd thing is that I'm getting extraneous newlines (shown by stars in the output), eg:
--
raw: Bonjour joyeux lion
Qm9uam91ciwgam95ZXV4IGxpb24K
ok: false
decoded: Bonjour, joyeux lion* <-------------------- e.g. here
--
std: "Bonjour, joyeux lion"
IkJvbmpvdXIsIGpveWV1eCBsaW9uIg==
ok: true
decoded: "Bonjour, joyeux lion"
--
Any thoughts on that would be gratefully received.
Rory
> To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/Z4UQYJmuk7Oe6xSG%40campbell-lange.net.