# bufat

[![CI](https://github.com/MJKWoolnough/bufat/actions/workflows/go-checks.yml/badge.svg)](https://github.com/MJKWoolnough/byteio/actions)
[![Go Reference](https://pkg.go.dev/badge/vimagination.zapto.org/bufat.svg)](https://pkg.go.dev/vimagination.zapto.org/byteio)

--
    import "vimagination.zapto.org/bufat"

Package bufat provides a buffered io.ReaderAt implementation.

## Highlights

 - Buffer reads from a io.ReaderAt
 - Can customise buffer and chunk size, and choose between LRU and MRU caches.
 - Options to limit max read position and to offset read position.

## Usage

```go
package main

import (
	"fmt"
	"strings"

	"vimagination.zapto.org/bufat"
)

type rat struct {
	*strings.Reader
	Reads int
}

func (r *rat) ReadAt(p []byte, pos int64) (int, error) {
	r.Reads++

	return r.Reader.ReadAt(p, pos)
}

func main() {
	r := &rat{Reader: strings.NewReader("ABCDEFGHIJKLMNOPQRSTUVWZYZ")}
	b := bufat.Buffer(
		r,
		bufat.ChunkSize(4),
	)

	var buf [10]byte

	b.ReadAt(buf[:], 1)
	fmt.Printf("%d: %s\n", r.Reads, buf)

	b.ReadAt(buf[:], 1)
	fmt.Printf("%d: %s\n", r.Reads, buf)

	b.ReadAt(buf[:], 5)
	fmt.Printf("%d: %s\n", r.Reads, buf)

	// Output:
	// 3: BCDEFGHIJK
	// 3: BCDEFGHIJK
	// 4: FGHIJKLMNO
}
```

## Documentation

Full API docs can be found at:

https://pkg.go.dev/vimagination.zapto.org/bufat
