1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# BitReader [](https://pkg.go.dev/github.com/pektezol/bitreader) [](https://goreportcard.com/report/github.com/pektezol/bitreader) [](https://github.com/pektezol/bitreader/blob/main/LICENSE)
The simplest bit reader with big/little-endian support for Golang.
## Installation
```bash
$ go get github.com/pektezol/bitreader
```
## Usage Examples
```go
import "github.com/pektezol/bitreader"
// ioStream: io.Reader Data to read from an io stream
// byteStream: []byte Data to read from a byte slice
// littleEndian: bool Little-endian(true) or big-endian(false) state
reader := bitreader.NewReader(ioStream, le)
reader := bitreader.NewReaderFromBytes(byteStream, le)
// Fork Reader, Copies Current Reader
newReader, err := reader.Fork()
// Read Total Number of Bits Left
bits, err := reader.ReadRemainingBits()
// Read First Bit
state, err := reader.ReadBool()
// Read Bits/Bytes
value, err := reader.ReadBits(64) // up to 64 bits
value, err := reader.ReadBytes(8) // up to 8 bytes
// Read String
text, err := reader.ReadString() // null-terminated
text, err := reader.ReadStringLength(256) // length-specified
// Read Bits/Bytes into Slice
arr, err := reader.ReadBitsToSlice(128)
arr, err := reader.ReadBytesToSlice(64)
// Skip Bits/Bytes
err := reader.SkipBits(8)
err := reader.SkipBytes(4)
// Wrapper functions
state := reader.TryReadBool() // bool
value := reader.TryReadInt1() // uint8
value := reader.TryReadUInt8() // uint8
value := reader.TryReadSInt8() // int8
value := reader.TryReadUInt16() // uint16
value := reader.TryReadSInt16() // int16
value := reader.TryReadUInt32() // uint32
value := reader.TryReadSInt32() // int32
value := reader.TryReadUInt64() // uint64
value := reader.TryReadSInt64() // int64
value := reader.TryReadFloat32() // float32
value := reader.TryReadFloat64() // float64
value := reader.TryReadBits(64) // uint64
value := reader.TryReadBytes(8) // uint64
text := reader.TryReadString() // string
text := reader.TryReadStringLength(64) // string
arr := reader.TryReadBitsToSlice(1024) // []byte
arr := reader.TryReadBytesToSlice(128) // []byte
bits := reader.TryReadRemainingBits() // uint64
```
## Error Handling
All ReadXXX(), SkipXXX() and Fork() functions returns an error message when they don't work as expected. It is advised to always handle errors. \
Wrapper functions, however, only returns the value and panics if an error is encountered, for the sake of ease of use.
## Bug Report / Feature Request
Using [Github Issues](https://github.com/pektezol/BitReader/issues/new/choose), you can report a bug that you encountered and/or request a feature that you would like to be added.
## Documentation
Full documentation can be found in https://pkg.go.dev/github.com/pektezol/bitreader
## License
This project is licensed under [GNU Lesser General Public License version 2.1](https://www.gnu.org/licenses/old-licenses/lgpl-2.1).
|