Documentation
¶
Overview ¶
Package ioutil contains extensions and utilities for package io from the standard library.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type LimitError ¶
type LimitError struct {
// Limit is the limit that triggered the error.
Limit uint64
}
LimitError is returned when the Limit is reached.
func (*LimitError) Error ¶
func (err *LimitError) Error() (msg string)
Error implements the error interface for *LimitError.
type LockedBuffer ¶ added in v0.35.12
type LockedBuffer struct {
// contains filtered or unexported fields
}
LockedBuffer is a wrapper around bytes.Buffer that makes it safe for concurrent use.
Example ¶
package main
import (
"fmt"
"slices"
"sync"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/ioutil"
)
func main() {
const goroutineCount = 5
b := ioutil.NewLockedBuffer(goroutineCount)
var wg sync.WaitGroup
for i := range goroutineCount {
wg.Go(func() {
_, err := b.Write([]byte{byte(i)})
errors.Check(err)
})
}
wg.Wait()
readBuffer := make([]byte, goroutineCount)
for i := range goroutineCount {
wg.Go(func() {
_, err := b.Read(readBuffer[i : i+1])
errors.Check(err)
})
}
wg.Wait()
slices.Sort(readBuffer)
fmt.Printf("%x\n", readBuffer)
}
Output: 0001020304
func NewLockedBuffer ¶ added in v0.35.12
func NewLockedBuffer(bufSize uint) (b *LockedBuffer)
NewLockedBuffer creates a new instance of *LockedBuffer. The size of the underlying buffer is set to bufSize.
func (*LockedBuffer) Buffer ¶ added in v0.35.12
func (b *LockedBuffer) Buffer() (buf *bytes.Buffer)
Buffer returns the underlying bytes buffer.
func (*LockedBuffer) Bytes ¶ added in v0.35.12
func (b *LockedBuffer) Bytes() (bytes []byte)
Bytes returns a copy of the buffer contents.
type TruncatedWriter ¶ added in v0.20.2
type TruncatedWriter struct {
// contains filtered or unexported fields
}
TruncatedWriter is an io.Writer that writes up to a certain limit of bytes to its underlying writer and then ignores the rest.
Example ¶
package main
import (
"bytes"
"fmt"
"github.com/AdguardTeam/golibs/ioutil"
)
func main() {
data := []byte("hello")
buf := &bytes.Buffer{}
fmt.Println(ioutil.NewTruncatedWriter(buf, 1).Write(data))
fmt.Println(buf)
buf = &bytes.Buffer{}
fmt.Println(ioutil.NewTruncatedWriter(buf, 2).Write(data))
fmt.Println(buf)
buf = &bytes.Buffer{}
fmt.Println(ioutil.NewTruncatedWriter(buf, 10).Write(data))
fmt.Println(buf)
}
Output: 5 <nil> h 5 <nil> he 5 <nil> hello
func NewTruncatedWriter ¶ added in v0.20.2
func NewTruncatedWriter(w io.Writer, limit uint64) (tw *TruncatedWriter)
NewTruncatedWriter returns a new truncated writer. It wraps w so that it writes up to limit bytes and then ignores the rest.