https://github.com/knadh/chunkedreader
chunkedreader is a light weight wrapper for Go's `bufio` that enables reading of byte streams in fixed size chunks
https://github.com/knadh/chunkedreader
Last synced: 10 months ago
JSON representation
chunkedreader is a light weight wrapper for Go's `bufio` that enables reading of byte streams in fixed size chunks
- Host: GitHub
- URL: https://github.com/knadh/chunkedreader
- Owner: knadh
- License: mit
- Created: 2015-03-19T07:07:26.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-03-19T09:11:47.000Z (about 11 years ago)
- Last Synced: 2024-10-28T19:59:34.077Z (over 1 year ago)
- Language: Go
- Size: 117 KB
- Stars: 4
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go chunkedreader
Kailash Nadh, March 2015
MIT License
## What?
chunkedreader is a light weight wrapper for Go's `bufio` that enables reading of byte streams in fixed size chunks.
It makes use of `bufio.Scanner`'s custom split function capability, which otherwise is meant for splitting
based on delimiters, to split based on fixed lengths.
This could especially be useful for reading continuous fixed length messages from a TCP stream, emulating "packets", for instance.
## Installation (go 1.1+)
`go get github.com/knadh/chunkedreader`
## Example
```go
package main
import (
"github.com/knadh/chunkedreader"
)
func main() {
// Open file for reading.
f, _ := os.Open("test.txt")
// Initialize the reader to read chunks of 4 bytes.
ch := chunkedreader.New(f, 4)
for ch.Read() {
// Length of b will always be 4, or less than 4 if there
// are no more bytes available to read.
b := ch.Bytes()
}
}
```