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