iokit

package
v0.296.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Example (ByteSize)
package main

import (
	"bytes"
	"io"

	"go.llib.dev/frameless/pkg/iokit"
)

func main() {

	var bs = make([]byte, iokit.Megabyte)
	buf := &bytes.Buffer{}

	n, err := buf.Write(bs)
	if err != nil {
		panic(err.Error())
	}

	if n < iokit.Kilobyte {
		//
	}

	io.LimitReader(buf, 128*iokit.Kibibyte)
}
Output:

Index

Examples

Constants

View Source
const (
	Byte = 1 << (10 * iota) // ignore first value by assigning to blank identifier
	Kibibyte
	Mebibyte
	Gibibyte
	Tebibyte
	Pebibyte
)
View Source
const (
	Kilobyte = Kibibyte
	Megabyte = Mebibyte
	Gigabyte = Gibibyte
	Terabyte = Tebibyte
)
View Source
const ErrReadLimitReached errorkit.Error = "request-entity-too-large"
View Source
const ErrSeekNegativePosition errorkit.Error = "iokit: negative position"

Variables

This section is empty.

Functions

func FormatByteSize added in v0.222.0

func FormatByteSize[ByteSize byteSize](n ByteSize) string

FormatByteSize will format byte size interpreted as the unit of digital information. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit of memory in many computer architectures

| Value | IEC | Memory | |-------|--------------|--------------| | 1 | B byte | B byte | | 1024 | KiB kibibyte | KB kilobyte | | 10242 | MiB mebibyte | MB megabyte | | 10243 | GiB gibibyte | GB gigabyte | | 10244 | TiB tebibyte | TB terabyte | | 10245 | PiB pebibyte | – | | 10246 | EiB exbibyte | – | | 10247 | ZiB zebibyte | – | | 10248 | YiB yobibyte | – |

func MoveByte added in v0.258.0

func MoveByte(in ByteReader, out ByteWriter) (byte, error)

func MoveRune added in v0.258.0

func MoveRune(in RuneReader, out RuneWriter) (rune, int, error)

func PeekByte added in v0.258.0

func PeekByte(in bytePeeker) (byte, error)

func PeekRune added in v0.258.0

func PeekRune(in runePeeker) (rune, int, error)

func ReadAllWithLimit added in v0.202.0

func ReadAllWithLimit(body io.Reader, readLimit ByteSize) (_ []byte, returnErr error)

Types

type Buffer

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

Buffer is an in-memory io.ReadWriteSeeker implementation

func NewBuffer

func NewBuffer[T []byte | string](data T) *Buffer

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

func (*Buffer) Close

func (b *Buffer) Close() error

func (*Buffer) Read

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

func (*Buffer) Seek

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

Seek seeks in the buffer of this Buffer instance

func (*Buffer) String

func (b *Buffer) String() string

func (*Buffer) Write

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

Write writes to the buffer of this Buffer instance

type ByteReader added in v0.258.0

type ByteReader interface {
	ReadByte() (byte, error)
}

type ByteSize added in v0.222.0

type ByteSize = int

type ByteUnreader added in v0.258.0

type ByteUnreader interface {
	UnreadByte() error
}

type ByteWriter added in v0.258.0

type ByteWriter interface {
	WriteByte(c byte) error
}

type KeepAliveReader added in v0.218.2

type KeepAliveReader struct {
	Source io.Reader // | io.ReadCloser
	// IdleTimeout specifies how long the KeepAliveReader will wait without any read operations
	// before it reads from the Source to prevent an I/O timeout.
	IdleTimeout time.Duration
	// contains filtered or unexported fields
}

KeepAliveReader is a decorator designed to help prevent read timeouts. When working with external readers in a streaming fashion, such as an `http.Request#Body`, regular reads are crucial to avoid IO closures due to read timeouts. For example, If processing a single stream element takes longer than the read timeout on the source server, the server might close the input stream, causing issues in stream processing. NewKeepAliveReader addresses this by regularly reading a byte from the stream when there are no Read calls and buffering it for the next Read call.

Example
package main

import (
	"bytes"
	"io"
	"time"

	"go.llib.dev/frameless/pkg/iokit"
)

func main() {
	r := bytes.NewReader([]byte("reader"))

	kar := iokit.NewKeepAliveReader(r, 20*time.Second)
	defer kar.Close()
	var _ io.ReadCloser = kar

	_, _ = kar.Read(make([]byte, 10))
}
Output:

func NewKeepAliveReader added in v0.218.2

func NewKeepAliveReader(r io.Reader, d time.Duration) *KeepAliveReader

func (*KeepAliveReader) Close added in v0.218.2

func (r *KeepAliveReader) Close() error

func (*KeepAliveReader) Init added in v0.218.2

func (r *KeepAliveReader) Init()

func (*KeepAliveReader) Read added in v0.218.2

func (r *KeepAliveReader) Read(p []byte) (int, error)

type RuneReader added in v0.258.0

type RuneReader interface {
	ReadRune() (r rune, size int, err error)
}

type RuneUnreader added in v0.258.0

type RuneUnreader interface {
	UnreadRune() error
}

type RuneWriter added in v0.258.0

type RuneWriter interface {
	WriteRune(r rune) (n int, err error)
}

type StubReader added in v0.202.0

type StubReader struct {
	Data     []byte
	ReadErr  error
	CloseErr error
	// contains filtered or unexported fields
}

func (*StubReader) Close added in v0.202.0

func (r *StubReader) Close() error

func (*StubReader) IsClosed added in v0.202.0

func (r *StubReader) IsClosed() bool

func (*StubReader) LastReadAt added in v0.218.2

func (r *StubReader) LastReadAt() time.Time

func (*StubReader) Read added in v0.202.0

func (r *StubReader) Read(p []byte) (int, error)

type SyncReadWriter

type SyncReadWriter struct {
	// ReadWriter is the protected io.ReadWriter
	ReadWriter io.ReadWriter
	// Locker is an optional sync.Locker value if you need to share locking between different multiple SyncReader.
	//
	// Default: sync.Mutex
	Locker sync.Locker
}

func (*SyncReadWriter) Read

func (rw *SyncReadWriter) Read(p []byte) (n int, err error)

func (*SyncReadWriter) Write

func (rw *SyncReadWriter) Write(p []byte) (n int, err error)

type SyncReader

type SyncReader struct {
	// Reader is the protected io.Reader
	Reader io.Reader
	// Locker is an optional sync.Locker value if you need to share locking between different multiple SyncReader.
	//
	// Default: sync.Mutex
	Locker sync.Locker
}

func (*SyncReader) Read

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

type SyncWriter

type SyncWriter struct {
	// Writer is the protected io.Writer
	Writer io.Writer
	// Locker is an optional sync.Locker value if you need to share locking between different multiple SyncWriter.
	//
	// Default: sync.Mutex
	Locker sync.Locker
}

func (*SyncWriter) Write

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

Jump to

Keyboard shortcuts

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