buffers

package module
v0.0.0-...-44ebd92 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: MIT Imports: 6 Imported by: 0

README

go-buffers

Package buffers provides tools for working with byte array, and byte slice buffers, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/sourcecode.social/reiver/go-buffers

GoDoc

Example

import "sourcecode.social/reiver/go-buffers"

// ...

var buffer [1024]byte

var p []byte = buffer[:]

writer := buffers.NewWriter(p)

n, err := writer.Write(data)

switch casted := err.(type) {
case buffers.TooShort:
	//@TODO
default:
	return err
}

Documentation

Overview

Package buffers provides tools for working with byte array, and byte slice buffers, for the Go programming language.

Example

Here is an example of buffers.Writer being used to provide a io.Writer interface to an byte array, and byte slice:

import "sourcecode.social/reiver/go-buffers"

// ...

var buffer [1024]byte

var p []byte = buffer[:]

writer := buffers.NewWriter(p)

n, err := writer.Write(data)

switch casted := err.(type) {
case buffers.TooShort:
	//@TODO
default:
	return err
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	BufferOverflow = erorr.Error("buffer overflow")
)

Functions

This section is empty.

Types

type Buffer

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

A Buffer is used to store a series of bytes.

A Buffer can be used as is, without any type of initialization (if not doesn't want the Buffer to have a limit):

var buffer buffers.Buffer

But if one wants the Buffer to have a limit, then it should be initialized like the following:

var buffer buffers.Buffer = buffers.LimitedBuffer(4194304) // 4 MB

func LimitedBuffer

func LimitedBuffer(max int) Buffer

LimitedBuffer returns a Buffer with a maximum size.

var buffer buffers.Buffer = buffers.LimitedBuffer(4194304) // 4 MB

func (*Buffer) Len

func (receiver *Buffer) Len() int

Len returns how many bytes have been accumulated in Buffer.

receiver.Len() == len(receiver.String())

func (*Buffer) ReadFrom

func (receiver *Buffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom reads from the io.Reader. Copying bytes until it gets an io.EOF.

func (*Buffer) String

func (receiver *Buffer) String() string

String returns the bytes that have been accumulated.

func (*Buffer) Write

func (receiver *Buffer) Write(p []byte) (n int, err error)

func (*Buffer) WriteByte

func (receiver *Buffer) WriteByte(c byte) error

WriteByte appends another byte to the Buffer.

func (*Buffer) WriteRune

func (receiver *Buffer) WriteRune(r rune) (int, error)

WriteRune appends the UTF-8 encoded rune to the Buffer.

func (*Buffer) WriteString

func (receiver *Buffer) WriteString(s string) (int, error)

WriteRune appends the string to the Buffer.

type TooShort

type TooShort interface {
	error
	TooShort() (expectedAtLeast uint64, actual uint64)
}

type Writer

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

Writer provides an io.Writer interface to a byte array, and byte slice.

This is something that should probably exist in the Go built-in "bytes" library, but doesn't.

Example
var buffer [256]byte

var p []byte = buffer[:]

writer := buffers.NewWriter(p)

data := []byte("Hello world!")

n, err := writer.Write(data)
if nil != err {
	fmt.Printf("ERROR: Problem writing: %s\n", err)
	return
}

fmt.Printf("Wrote %d bytes to the buffer.\n", n)
fmt.Printf("Those %d bytes in the buffer have a value of “%s”.\n", n, buffer)
Output:

func NewWriter

func NewWriter(dst []byte) *Writer

func (*Writer) Write

func (receiver *Writer) Write(src []byte) (n int, err error)

Jump to

Keyboard shortcuts

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