bytebuffer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package bytebuffer provides a buffer type that implements io.ReadWriteSeeker. It also provides an interface for creating byte buffers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer implements the io.Reader, io.WriterTo, io.Writer, io.Seeker, and io.ByteScanner interfaces by reading from or writing to a byte slice. The zero value for Buffer operates like a Buffer of an empty slice.

func New

func New(p []byte) *Buffer

New returns a new Buffer reading from and writing to b.

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err error)

Read implements the io.Reader interface.

func (*Buffer) ReadByte

func (b *Buffer) ReadByte() (byte, error)

ReadByte implements the io.ByteReader interface.

func (*Buffer) Reset

func (b *Buffer) Reset(p []byte)

Reset resets the Buffer to be reading from and writing to b.

func (*Buffer) Seek

func (b *Buffer) Seek(offset int64, whence int) (int64, error)

Seek implements the io.Seeker interface.

func (*Buffer) Size

func (b *Buffer) Size() int64

Size returns the length of the underlying byte slice.

func (*Buffer) Truncate

func (b *Buffer) Truncate(size int64) error

Truncate changes the size of the buffer. It does not change the I/O offset.

func (*Buffer) UnreadByte

func (b *Buffer) UnreadByte() error

UnreadByte complements *Buffer.ReadByte in implementing the io.ByteScanner interface.

func (*Buffer) Write

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

Write implements the io.Writer interface. If Write would extend past the underlying byte slice's capacity, then Write allocates a new byte slice large enough to fit the new bytes. Write returns an error if and only if the byte slice length would exceed an int. If the offset is larger than the length of the underlying byte slice, then the intervening bytes are zero-filled.

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements the io.WriterTo interface.

type BufferCreator

type BufferCreator struct {
	// Limit specifies an maximum limit on size of buffers.
	// If Limit is zero, then a reasonable default limit is used.
	// If Limit is negative, then no limit is applied.
	Limit int
}

BufferCreator is a Creator that returns buffers backed by memory.

func (BufferCreator) CreateBuffer

func (c BufferCreator) CreateBuffer(size int64) (ReadWriteSeekCloser, error)

CreateBuffer returns an in-memory buffer of the given size.

type CreateFunc

type CreateFunc func(size int64) (ReadWriteSeekCloser, error)

CreateFunc is a function that implements Creator.

func (CreateFunc) CreateTemp

func (f CreateFunc) CreateTemp(size int64) (ReadWriteSeekCloser, error)

CreateTemp implements Creator by calling f.

type Creator

type Creator interface {
	CreateBuffer(size int64) (ReadWriteSeekCloser, error)
}

A type that implements Creator can create temporary byte buffers. The ReadWriteSeekCloser returned from CreateBuffer must be of the given size and start with its offset at 0. If the size passed to CreateBuffer is less than 1, it indicates that the caller does not know how many bytes will be written to it. A returned ReadWriteSeekCloser values might not permit writes past the ends of its returned buffers. If this is the case, passing a size less than 1 to CreateBuffer should return an error.

type ReadWriteSeekCloser

type ReadWriteSeekCloser interface {
	io.Reader
	io.Writer
	io.Seeker
	io.Closer
}

ReadWriteSeekCloser is an interface that groups the Read, Write, Seek, and Close methods.

type TempFileCreator

type TempFileCreator struct {
	Dir     string
	Pattern string
}

TempFileCreator implements Creator with os.CreateTemp. The fields of TempFileCreator are given as arguments to os.CreateTemp.

func (TempFileCreator) CreateBuffer

func (tfc TempFileCreator) CreateBuffer(size int64) (ReadWriteSeekCloser, error)

CreateTemp creates a new temporary file of the given size. The returned file will be removed when calling Close.

Jump to

Keyboard shortcuts

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