|
|
|
@ -4,6 +4,11 @@ import (
|
|
|
|
|
"io"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
minBufferSizeKilo = 2
|
|
|
|
|
maxBufferSizeKilo = 128
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ReadFrom(reader io.Reader, sizeInKilo int) ([]byte, error) {
|
|
|
|
|
buffer := make([]byte, sizeInKilo<<10)
|
|
|
|
|
nBytes, err := reader.Read(buffer)
|
|
|
|
@ -13,9 +18,23 @@ func ReadFrom(reader io.Reader, sizeInKilo int) ([]byte, error) {
|
|
|
|
|
return buffer[:nBytes], err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func roundUp(size int) int {
|
|
|
|
|
if size <= minBufferSizeKilo {
|
|
|
|
|
return minBufferSizeKilo
|
|
|
|
|
}
|
|
|
|
|
if size >= maxBufferSizeKilo {
|
|
|
|
|
return maxBufferSizeKilo
|
|
|
|
|
}
|
|
|
|
|
size--
|
|
|
|
|
size |= size >> 1
|
|
|
|
|
size |= size >> 2
|
|
|
|
|
size |= size >> 4
|
|
|
|
|
return size + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReaderToChan dumps all content from a given reader to a chan by constantly reading it until EOF.
|
|
|
|
|
func ReaderToChan(stream chan<- []byte, reader io.Reader) error {
|
|
|
|
|
bufferSizeKilo := 4
|
|
|
|
|
bufferSizeKilo := 2
|
|
|
|
|
for {
|
|
|
|
|
data, err := ReadFrom(reader, bufferSizeKilo)
|
|
|
|
|
if len(data) > 0 {
|
|
|
|
@ -24,6 +43,15 @@ func ReaderToChan(stream chan<- []byte, reader io.Reader) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if bufferSizeKilo == maxBufferSizeKilo {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
dataLenKilo := len(data) >> 10
|
|
|
|
|
if dataLenKilo == bufferSizeKilo {
|
|
|
|
|
bufferSizeKilo <<= 1
|
|
|
|
|
} else {
|
|
|
|
|
bufferSizeKilo = roundUp(dataLenKilo)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|