Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a reson to use bufio Reader/Writer when copying actual files?

I am having a simple code that copies between two actual files. (They are on the same disk, but not sure if that is relevant.)

func copy(inPath, outPath string) {
    inFile, err := os.Open(inPath)
    if err != nil {
        return fmt.Errorf("cannot open input file on path %q: %w", inPath, err)
    }
    defer inFile.Close()

    outFile, err := os.Create(outPath)
    if err != nil {
        return fmt.Errorf("cannot create output file on path %q: %w", outPath, err)
    }
    defer outFile()

    if _, err := io.Copy(inFile, outFile); err != nil {
        return fmt.Errorf("cannot copy file %q to %q: %w", inPath, outPath, err)
    }
}

I am now not sure, if I should or should not be using bufio to either reader or writer or both or none.

That is, if I should do something like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

func copy(inPath, outPath string) {
    inFile, err := os.Open(inPath)
    if err != nil {
        return fmt.Errorf("cannot open input file on path %q: %w", inPath, err)
    }
    defer inFile.Close()

    outFile, err := os.Create(outPath)
    if err != nil {
        return fmt.Errorf("cannot create output file on path %q: %w", outPath, err)
    }
    defer outFile()

    if _, err := io.Copy(bufio.NewWriter(outFile), bufio.NewReader(inFile)); err != nil {
        return fmt.Errorf("cannot copy file %q to %q: %w", inPath, outPath, err)
    }
}

The bufio documentation does not tell me at all when to use it and when not.

>Solution :

Do not use bufio in this scenario. When given an *os.File destination, io.Copy copies the file by calling dest.ReadFrom(src). If the source is also an *os.File, ReadFrom calls the OS to do the copy on some systems.

In the case where ReadFrom and other optimizations are not available, there’s still no benefit to using bufio for io.Copy sources and destinations. The io.Copy buffer size is 32k and the bufio default buffer sizes are 4K. Because the bufio types bypass their own buffers when passed a larger buffer, the bufio types don’t do anything other than allocate some extra memory.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading