Récupération d'un fichier avec BUFFER. DOnc plus optimisé

This commit is contained in:
Benjamin 2022-03-10 14:35:41 +01:00
parent 6fa5f6c1c4
commit b198f51c1e

View File

@ -3,7 +3,6 @@ package utils
import (
"bufio"
"fmt"
"io"
"os"
)
@ -14,12 +13,19 @@ func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool {
return false
}
println(1)
defer file.Close()
_, err := io.CopyN(file, reader, int64(fileSize))
println(2)
currentSize := 0
buffer := make([]byte, 1024)
for currentSize < fileSize {
length, err := reader.Read(buffer)
if err != nil {
return false
}
currentSize += length
file.WriteAt(buffer, int64(currentSize))
}
return true
}