Documentation ¶
Overview ¶
Package util implements small helper function that should be included in the stdlib in our opinion.
Index ¶
- Variables
- func Clamp(x, lo, hi int) int
- func Closer(c io.Closer)
- func DeriveKey(pwd, salt []byte, keyLen int) []byte
- func LimitWriter(w io.Writer, sz int64) io.Writer
- func Max(a, b int) int
- func Max64(a, b int64) int64
- func Min(a, b int) int
- func Min64(a, b int64) int64
- func NewTimeoutReader(r io.Reader, d time.Duration) io.Reader
- func NewTimeoutWriter(w io.Writer, d time.Duration) io.Writer
- func NopWriteCloser(w io.Writer) io.WriteCloser
- func OmitBytes(data []byte, lim int) string
- func PeekHeader(r io.Reader, size int64) ([]byte, io.Reader, error)
- func PrefixReader(data []byte, r io.Reader) io.Reader
- func SyncedReadWriter(w io.ReadWriter) io.ReadWriter
- func Tar(root, archiveName string, w io.Writer) error
- func Touch(path string) error
- func UClamp(x, lo, hi uint) uint
- func UMax(a, b uint) uint
- func UMin(a, b uint) uint
- func Untar(r io.Reader, root string) error
- type Empty
- type Errors
- type SizeAccumulator
- type SyncBuffer
- type TimeoutReadWriter
- func (rw *TimeoutReadWriter) Read(p []byte) (n int, err error)
- func (rw *TimeoutReadWriter) SetDeadline(d time.Time) error
- func (rw *TimeoutReadWriter) SetReadDeadline(d time.Time) error
- func (rw *TimeoutReadWriter) SetReadTimeout(d time.Duration) error
- func (rw *TimeoutReadWriter) SetTimeout(d time.Duration) error
- func (rw *TimeoutReadWriter) SetWriteDeadline(d time.Time) error
- func (rw *TimeoutReadWriter) SetWriteTimeout(d time.Duration) error
- func (rw *TimeoutReadWriter) Write(p []byte) (n int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTimeout = errors.New("I/O Timeout: Operation timed out")
ErrTimeout will be returned by Read/Write in case of a timeout.
Functions ¶
func Closer ¶
Closer closes c. If that fails, it will log the error. The intended usage is for convinient defer calls only! It gives only little knowledge about where the error is, but it's slightly better than a bare defer xyz.Close()
func DeriveKey ¶
DeriveKey derives a key from password and salt being keyLen bytes long. It uses an established password derivation function.
func LimitWriter ¶
LimitWriter is like io.LimitReader but for an io.Writer
func NewTimeoutReader ¶
NewTimeoutReader wraps `r` and returns a io.Reader that times out after `d` elapsed with ErrTimeout if `r` didn't succeed in that time.
func NewTimeoutWriter ¶
NewTimeoutWriter wraps `w` and returns a io.Writer that times out after `d` elapsed with ErrTimeout if `w` didn't succeed in that time.
func NopWriteCloser ¶
func NopWriteCloser(w io.Writer) io.WriteCloser
NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer w.
func OmitBytes ¶
OmitBytes converts a byte slice into a string representation that omits data in the middle if necessary. It is useful for testing and printing user information. `lim` is the number of bytes
Example:
OmitBytes([]byte{1,2,3,4}, 2) -> [1 ... 2] OmitBytes([]byte{1,2,3,4}, 4) -> [1, 2, 3, 4]
func PeekHeader ¶ added in v0.3.0
PeekHeader returns a new reader that will yield the very same data as `r`. It reads `size` bytes from `r` and returns it. The underlying implementation uses PrefixReader to prefix the stream with the header again.
func PrefixReader ¶
PrefixReader returns an io.Reader that outputs `data` before the rest of `r`.
func SyncedReadWriter ¶
func SyncedReadWriter(w io.ReadWriter) io.ReadWriter
SyncedReadWriter returns a io.ReadWriter that protects each call to Read() and Write() with a sync.Mutex.
Types ¶
type SizeAccumulator ¶
type SizeAccumulator struct {
// contains filtered or unexported fields
}
SizeAccumulator is a io.Writer that simply counts the amount of bytes that has been written to it. It's useful to count the received bytes from a reader in conjunction with a io.TeeReader
Example usage without error handling:
s := &SizeAccumulator{} teeR := io.TeeReader(r, s) io.Copy(os.Stdout, teeR) fmt.Printf("Wrote %d bytes to stdout\n", s.Size())
Example ¶
s := &SizeAccumulator{} teeR := io.TeeReader(bytes.NewReader([]byte("Hello, ")), s) io.Copy(os.Stdout, teeR) fmt.Printf("wrote %d bytes to stdout\n", s.Size())
Output: Hello, wrote 7 bytes to stdout
func (*SizeAccumulator) Reset ¶
func (s *SizeAccumulator) Reset()
Reset resets the size counter to 0.
func (*SizeAccumulator) Size ¶
func (s *SizeAccumulator) Size() uint64
Size returns the cumulated written bytes. It can be safely called from any go routine.
type SyncBuffer ¶
SyncBuffer is a bytes.Buffer that protects each call to Read() and Write() with a sync.RWMutex, i.e. parallel access to Read() is possible, but blocks when doing a Write().
type TimeoutReadWriter ¶
TimeoutReadWriter is io.ReadWriter capable of returning ErrTimeout if there was no result in a certain timeout period.
func NewTimeoutReadWriter ¶
func NewTimeoutReadWriter(rw io.ReadWriter, d time.Duration) *TimeoutReadWriter
NewTimeoutReadWriter wraps `rw` and returns a io.ReadWriter that times out after `d` elapsed with ErrTimeout if `rw` didn't succeed in that time.
func (*TimeoutReadWriter) SetDeadline ¶
func (rw *TimeoutReadWriter) SetDeadline(d time.Time) error
SetDeadline sets an absolute time in the future where an I/O operation should be canceled.
func (*TimeoutReadWriter) SetReadDeadline ¶
func (rw *TimeoutReadWriter) SetReadDeadline(d time.Time) error
SetReadDeadline sets an absolute time in the future where a read option should be canceled.
func (*TimeoutReadWriter) SetReadTimeout ¶
func (rw *TimeoutReadWriter) SetReadTimeout(d time.Duration) error
SetReadTimeout sets an own timeout for reading.
func (*TimeoutReadWriter) SetTimeout ¶
func (rw *TimeoutReadWriter) SetTimeout(d time.Duration) error
SetTimeout sets both the read and write timeout to `d`.
func (*TimeoutReadWriter) SetWriteDeadline ¶
func (rw *TimeoutReadWriter) SetWriteDeadline(d time.Time) error
SetWriteDeadline sets an absolute time in the future where a write option should be canceled.
func (*TimeoutReadWriter) SetWriteTimeout ¶
func (rw *TimeoutReadWriter) SetWriteTimeout(d time.Duration) error
SetWriteTimeout sets an own timeout for writing.
Directories ¶
Path | Synopsis |
---|---|
Package conductor is a small helper to execute work heavy operations in the backgrounds that deliver partial results ("result streaming").
|
Package conductor is a small helper to execute work heavy operations in the backgrounds that deliver partial results ("result streaming"). |
Package log implements utility methods for logging in a colorful manner.
|
Package log implements utility methods for logging in a colorful manner. |
Package registry contains shared utils between server and client regarding the registry opening and modification.
|
Package registry contains shared utils between server and client regarding the registry opening and modification. |
Package trie implements a general purpose Path-*Node.
|
Package trie implements a general purpose Path-*Node. |