58 lines
1.0 KiB
Go
58 lines
1.0 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package transport
|
|
|
|
import (
|
|
_ "errors"
|
|
_ "fmt"
|
|
_ "net/url"
|
|
_ "net/http"
|
|
_ "strings"
|
|
_ "path/filepath"
|
|
"io"
|
|
_ "os"
|
|
_ "context"
|
|
)
|
|
|
|
type ReadFilter func([]byte, int, error) (int, error)
|
|
|
|
var ReadBufferSize int = 32 * 1024
|
|
|
|
type readerFrom struct {
|
|
io.Writer
|
|
filter ReadFilter
|
|
}
|
|
|
|
func (w readerFrom) ReadFrom(r io.Reader) (n int64, err error) {
|
|
readBuffer := make([]byte, ReadBufferSize)
|
|
for {
|
|
var readBytes int
|
|
if readBytes, err = r.Read(readBuffer); readBytes > 0 {
|
|
if w.filter != nil {
|
|
readBytes, err = w.filter(readBuffer, readBytes, err)
|
|
}
|
|
writeBytes, writeErr := w.Write(readBuffer[0:readBytes])
|
|
if writeBytes > 0 {
|
|
n += int64(writeBytes)
|
|
}
|
|
if readBytes != writeBytes {
|
|
writeErr = io.ErrShortWrite
|
|
}
|
|
if writeErr != nil {
|
|
break
|
|
}
|
|
}
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
err = nil
|
|
}
|
|
break
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func NewReaderFrom(w io.Writer, filter ReadFilter) io.ReaderFrom {
|
|
return readerFrom{ Writer: w, filter: filter}
|
|
}
|