ioutil

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: Unlicense Imports: 2 Imported by: 1

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

func LimitReader

func LimitReader(r io.Reader, n uint64) (limited io.Reader)

LimitReader returns an io.Reader that reads up to n bytes. Once that limit is reached, [ErrLimit] is returned from limited.Read. limited.Read is not safe for concurrent use. n must be non-negative.

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 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 uint) (tw *TruncatedWriter)

NewTruncatedWriter returns a new truncated writer. It wraps the given writer w the way it writes up to the given limit and then ignores the rest.

func (*TruncatedWriter) Write added in v0.20.2

func (w *TruncatedWriter) Write(b []byte) (n int, err error)

Write implements the io.Writer interface for *TruncatedWriter. n is always len(b).

Jump to

Keyboard shortcuts

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