func ExampleBufferedVectorio() {
	// Create a temp file to use
	f, err := ioutil.TempFile("", "vectorio")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	data1 := []byte("foobarbaz")
	data2 := []byte("foobazbar")

	w, err := vectorio.NewBufferedWritev(f)
	nw, err := w.Write(data2)
	nw, err = w.WriteIovec(syscall.Iovec{&data1[0], 9})
	nw, err = w.Flush()

	if err != nil {
		fmt.Printf("Flush threw error: %s", err)
	}
	if nw == 9*2 {
		fmt.Println("Wrote", nw, "bytes to file!")
	} else {
		fmt.Println("did not write 9 * 2 bytes, wrote ", nw)
	}

	// Output:
	// Wrote 18 bytes to file!
}
示例#2
0
func ExampleVectorioCombined() {
	// Create a temp file for demo purposes
	f, err := ioutil.TempFile("", "vectorio")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	data1 := []byte("foobarbaz_buf\n")
	data2 := []byte("barbazfoo_buf\n")

	// This demonstrates the "buffered" form of the library, similar to bufio.
	// w implements io.Writer.
	w, err := vectorio.NewBufferedWritev(f)
	if err != nil {
		panic(err)
	}

	// The simple method is to just write a byte slice;
	// this is converted to a syscall.Iovec and queued
	// for writing
	w.Write(data2)

	// The user can also gain more control with WriteIovec,
	// although this does not have a significant advantage over Write.
	// This is what Write does on your behalf.
	w.WriteIovec(syscall.Iovec{&data1[0], uint64(len(data1))})

	// Flush must be called after writes are complete, to empty out
	// the buffer of pending Iovec.
	// Returns the total number of bytes written, as reported by the underlying syscall.
	nw1, err := w.Flush()
	if err != nil {
		panic(err)
	}

	// One can also write a slice of byte slices ([][]byte) all at once.
	// Note, this usage does *not* implement io.Writer, but if you have a slice
	// of byte slices to write, this is a way to do that without looping.

	multiple := [][]byte{
		[]byte("foobarbaz_slice\n"),
		[]byte("foobazbar_slice\n"),
	}

	// we return the number of bytes written, as reported by the underlying syscall.
	nw2, err := vectorio.Writev(f, multiple)
	if err != nil {
		panic(err)
	}

	fmt.Println("Wrote", nw1+nw2, "bytes to file")
	// Output:
	// Wrote 60 bytes to file
}
func ExampleBufferedVectorioUnsafe() {
	// Create a temp file to use
	f, _ := ioutil.TempFile("", "vectorio")
	defer f.Close()

	data1 := []byte("foobarbaz")
	data2 := []byte("foobazbar")

	w, _ := vectorio.NewBufferedWritev(f)
	w.Write(data2)
	w.WriteIovec(syscall.Iovec{&data1[0], 9})
	nw, _ := w.Flush()
	fmt.Println("Wrote", nw, "bytes to file!")

	// Output:
	// Wrote 18 bytes to file!
}
示例#4
0
func TestBufferedWritevHuge(t *testing.T) {
	f, err := os.Create("testbufferedwritevhuge")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	data := []byte("foobarba")
	final := []byte("attheend")

	bw, _ := vectorio.NewBufferedWritev(f)

	// write a little more than our buffer size
	for i := 0; i < 1024; i++ {
		bw.Write(data)
	}
	bw.Write(final)

	_, err = bw.Flush()
	if err != nil {
		t.Errorf("Flush threw error: %s", err)
	}

	stats, err := f.Stat()
	if err != nil {
		t.Errorf("Stat threw error: %s", err)
	}
	filesize := stats.Size()
	if filesize != int64(1024*len(data)+len(final)) {
		t.Errorf("Length %d of input does not match %d written bytes", len(data), filesize)
	}

	// Maybe make this validate file contents later??
	f.Seek(0, 0)
	fromdisk, err := ioutil.ReadAll(f)
	if err != nil {
		t.Errorf("can't read file back, %s", err)
	}
	compared := fromdisk[1024*8:]
	if bytes.Compare(compared, final) != 0 {
		t.Errorf("contents of file don't match input, %s != %s or %d != %d", compared, final, len(compared), len(final))
	}
	os.Remove("testbufferedwritevhuge")
}
示例#5
0
func TestBufferedWritev(t *testing.T) {
	f, err := os.Create("testbufferedwritev")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	data := []byte("foobarbaz")
	data_desired := []byte("foobazbar")

	bw, _ := vectorio.NewBufferedWritev(f)

	bw.WriteIovec(syscall.Iovec{&data[0], 3})
	bw.WriteIovec(syscall.Iovec{&data[6], 3})
	bw.Write(data[3:6])

	nw, err := bw.Flush()
	if err != nil {
		t.Errorf("Flush threw error: %s", err)
	}
	if nw != 3*3 {
		t.Errorf("Flush wrote wrong number of bytes, expected 3 * 3 but got %d", nw)
	}

	stats, err := f.Stat()
	if err != nil {
		t.Errorf("Stat threw error: %s", err)
	}
	filesize := stats.Size()
	if filesize != int64(len(data)) {
		t.Errorf("Length %d of input does not match %d written bytes", len(data), filesize)
	}

	f.Seek(0, 0)
	fromdisk, err := ioutil.ReadAll(f)
	if err != nil {
		t.Errorf("can't read file back, %s", err)
	}
	if bytes.Compare(fromdisk, data_desired) != 0 {
		t.Errorf("contents of file don't match input, %s != %s or %d != %d", fromdisk, data_desired, len(fromdisk), len(data_desired))
	}
	os.Remove("testbufferedwritev")
}