Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/srndv2/src/srnd/line.go

35 lines
505 B
Go
Raw Normal View History

package srnd
import (
"bytes"
"io"
)
type LineWriter struct {
2017-04-04 20:01:02 +05:00
w io.Writer
Left int64
}
2017-04-04 20:01:02 +05:00
func NewLineWriter(w io.Writer, limit int64) *LineWriter {
return &LineWriter{
2017-04-04 20:01:02 +05:00
w: w,
Left: limit,
}
}
func (l *LineWriter) Write(data []byte) (n int, err error) {
2017-04-04 20:01:02 +05:00
if l.Left <= 0 {
err = ErrOversizedMessage
return
}
2017-04-04 20:04:14 +05:00
wr := len(data)
data = bytes.Replace(data, []byte{13, 10}, []byte{10}, -1)
2017-04-04 20:01:02 +05:00
n, err = l.w.Write(data)
l.Left -= int64(n)
2017-04-04 20:04:14 +05:00
if err != nil {
return n, err
}
n = wr
return
}