bitlib

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 3 Imported by: 4

README

Bitlib

Coverage Go Report Card

Utilities for reading and writing binary data that for some reason I keep re-writing over and over.

Reader Implements:

  • io.Reader
  • io.ByteReader

Writer Implements:

  • io.Writer
  • io.ByteWriter
  • io.StringWriter

Example

package bitlib_test

import (
	"bytes"
	"encoding/binary"
	"log"
	"testing"

	"github.com/EliCDavis/bitlib"
	"github.com/stretchr/testify/assert"
)

func TestReadWrite(t *testing.T) {
	buf := bytes.Buffer{}
	var f64Val float64 = 1.23456789
	var f32Val float32 = 1.23456789
	var i64Val int64 = -12334567890000
	var i32Val int32 = -123345678
	var i16Val int16 = -12334
	var u64Val uint64 = 12334567890000
	var u32Val uint32 = 123345678
	var u16Val uint16 = 12334
	var bVal byte = 14
	var bArr []byte = []byte{1, 2, 3, 4, 5}
	var f64Arr []float64 = []float64{1, 2, 3, 4, 5}
	var f32Arr []float32 = []float32{1, 2, 3, 4, 5}

	// Write
	writer := bitlib.NewWriter(&buf, binary.LittleEndian)
	writer.Float64(f64Val)
	writer.Float32(f32Val)
	writer.Int64(i64Val)
	writer.Int32(i32Val)
	writer.Int16(i16Val)
	writer.UInt64(u64Val)
	writer.UInt32(u32Val)
	writer.UInt16(u16Val)
	writer.VarInt(i64Val)
	writer.UVarInt(u64Val)
	writer.Byte(bVal)
	writer.ByteArray(bArr)
	writer.Float64Array(f64Arr)
	writer.Float32Array(f32Arr)

	// Read
	reader := bitlib.NewReader(bytes.NewBuffer(buf.Bytes()), binary.LittleEndian)
	log.Println(reader.Float64())
	log.Println(reader.Float32())
	log.Println(reader.Int64())
	log.Println(reader.Int32())
	log.Println(reader.Int16())
	log.Println(reader.UInt64())
	log.Println(reader.UInt32())
	log.Println(reader.UInt16())
	log.Println(reader.VarInt())
	log.Println(reader.UVarInt())
	log.Println(reader.Byte())
	log.Println(reader.ByteArray(len(bArr)))
	log.Println(reader.Float64Array(len(f64Arr)))
	log.Println(reader.Float32Array(len(f32Arr)))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Read added in v1.1.0

func Read[T any](reader *Reader) (T, error)

func ReadArray added in v1.1.0

func ReadArray[T any](reader *Reader, length int) ([]T, error)

func Write added in v1.1.0

func Write[T any](writer *Writer, data T) error

func WriteArray added in v1.1.0

func WriteArray[T any](writer *Writer, data []T) error

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func NewReader

func NewReader(in io.Reader, byteOrder binary.ByteOrder) *Reader

func (*Reader) Any added in v1.1.0

func (r *Reader) Any(a any) error

Reads data and sets the value a passed into this function. Function must recieve a pointer to the value to be set.

func (*Reader) Byte

func (r *Reader) Byte() byte

func (*Reader) ByteArray

func (r *Reader) ByteArray(len int) []byte

func (Reader) Error

func (r Reader) Error() error

func (*Reader) Float32

func (r *Reader) Float32() float32

func (*Reader) Float32Array

func (r *Reader) Float32Array(len int) []float32

func (*Reader) Float64

func (r *Reader) Float64() float64

func (*Reader) Float64Array

func (r *Reader) Float64Array(len int) []float64

func (*Reader) Int16

func (r *Reader) Int16() int16

func (*Reader) Int32

func (r *Reader) Int32() int32

func (*Reader) Int32Array

func (r *Reader) Int32Array(len int) []int32

func (*Reader) Int64

func (r *Reader) Int64() int64

func (*Reader) Read added in v1.1.0

func (r *Reader) Read(p []byte) (n int, err error)

Implementing the io.Reader interface

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

Implementing the io.ByteReader interface

func (*Reader) String added in v1.1.0

func (r *Reader) String(len int) string

func (*Reader) UInt16

func (r *Reader) UInt16() uint16

func (*Reader) UInt32

func (r *Reader) UInt32() uint32

func (*Reader) UInt64

func (r *Reader) UInt64() uint64

func (*Reader) UVarInt

func (r *Reader) UVarInt() (i uint64)

func (*Reader) Uint32Array

func (r *Reader) Uint32Array(len int) []uint32

func (*Reader) VarInt

func (r *Reader) VarInt() (i int64)

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(in io.Writer, byteOrder binary.ByteOrder) *Writer

func (*Writer) Any added in v1.1.0

func (w *Writer) Any(a any) error

func (*Writer) Byte

func (w *Writer) Byte(i byte) error

func (*Writer) ByteArray

func (w *Writer) ByteArray(b []byte) error

func (Writer) Error

func (w Writer) Error() error

func (*Writer) Float32

func (w *Writer) Float32(f float32) error

func (*Writer) Float32Array

func (w *Writer) Float32Array(fArr []float32) error

func (*Writer) Float64

func (w *Writer) Float64(f float64) error

func (*Writer) Float64Array

func (w *Writer) Float64Array(fArr []float64) error

func (*Writer) Int16

func (w *Writer) Int16(i int16) error

func (*Writer) Int32

func (w *Writer) Int32(i int32) error

func (*Writer) Int32Array

func (w *Writer) Int32Array(iArr []int32) error

func (*Writer) Int64

func (w *Writer) Int64(i int64) error

func (*Writer) UInt16

func (w *Writer) UInt16(i uint16) error

func (*Writer) UInt32

func (w *Writer) UInt32(i uint32) error

func (*Writer) UInt64

func (w *Writer) UInt64(i uint64) error

func (*Writer) UVarInt

func (w *Writer) UVarInt(i uint64) error

func (*Writer) Uint32Array

func (w *Writer) Uint32Array(iArr []uint32) error

func (*Writer) VarInt

func (w *Writer) VarInt(i int64) error

func (*Writer) Write added in v1.1.0

func (w *Writer) Write(p []byte) (n int, err error)

Implementing the io.Writer interface

func (*Writer) WriteByte added in v1.1.0

func (w *Writer) WriteByte(c byte) error

Implementing io.ByteWriter

func (*Writer) WriteString added in v1.1.0

func (w *Writer) WriteString(s string) (int, error)

Implementing io.StringWriter

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL