An in depth look at the Write method in the golang bufio package

  • 2020-06-19 10:33:02
  • OfStack

preface

The bufio package implements I/O with buffering. It wraps io.Reader and io.Writer objects and then creates another object (Reader or Writer) that implements the same interface but adds buffering.

Start with the Write(in the os package) method, which has no caching, and writes the data directly to the file.


package main

import (
 "os"
 "fmt"
)

func main() {
 file, err := os.OpenFile("a.txt", os.O_CREATE|os.O_RDWR, 0666)
 if err != nil {
  fmt.Println(err)
 }
 defer file.Close()

 content := []byte("hello world!")
 if _, err = file.Write(content); err != nil {
  fmt.Println(err)
 }
 fmt.Println("write file successful")
}

Next, let's look at an example of an error using the buffered Write method, in which the written data is not visible after the program executes.


package main

import (
  "os"
  "fmt"
  "bufio"
)

func main() {
  file, err := os.OpenFile("a.txt", os.O_CREATE|os.O_RDWR, 0666)
  if err != nil {
    fmt.Println(err)
  }
  defer file.Close()

  content := []byte("hello world!")
  newWriter := bufio.NewWriter(file)
  if _, err = newWriter.Write(content); err != nil {
    fmt.Println(err)
  }
  fmt.Println("write file successful")
}

Why is there no written data in the file? Let's look at the Write method in bufio.


func (b *Writer) Write(p []byte) (nn int, err error){
  for len(p) > b.Available() && b.err == nil {
    var n int
    if b.Buffered() == 0{
      n,b.err =b.wr.Write(p)
    }else {
      n = copy(b.buf[b.n:],p)
      b.n+=n
      b.Flush()
    }
    nn+=n
    p=p[n:]
  }
  if b.err!=nil {
    return nn, b.err
  }
  n:= copy(b.buf[b.n:],p)
  b.n+= n
  nn+=n
  return nn,nil
}

The Write method first determines whether the written data length is larger than the buffer length set. If it is less than, the data copy will be added to the buffer. When the data length is larger than the buffer length, if the data is extremely large, the copy link is skipped and the file is written directly. In other cases, the data is copied to the buffer queue before being written to a file.

So the above error example simply adds the Flush() method to it and writes the cached data to a file.


package main

import (
  "os"
  "fmt"
  "bufio"
)

func main() {
  file, err := os.OpenFile("./a.txt", os.O_CREATE|os.O_RDWR, 0666)
  if err != nil {
    fmt.Println(err)
  }
  defer file.Close()

  content := []byte("hello world!")
  newWriter := bufio.NewWriterSize(file, 1024)
  if _, err = newWriter.Write(content); err != nil {
    fmt.Println(err)
  }
  if err = newWriter.Flush(); err != nil {
    fmt.Println(err)
  }
  fmt.Println("write file successful")
}

conclusion


Related articles: