iostream

package
v2.0.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package iostream contains various io.Reader and io.Writer implementations.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ScanWriterMaxBufferSize = 1024 * 1024

ScanWriterMaxBufferSize is the maximum size of the ScanWriter buffer. If the buffer is full before the delimiter is encountered, the buffer contents are flushed like the delimiter was encountered.

Functions

func NewScanWriter

func NewScanWriter(fn CallbackFn) io.WriteCloser

NewScanWriter returns a new ScanWriter.

Types

type ByteCounter

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

ByteCounter is a simple io.Writer that counts the number of bytes written to it, to be used in conjunction with io.MultiWriter / io.TeeReader.

func (*ByteCounter) Count

func (bc *ByteCounter) Count() int64

Count returns the number of bytes written to the ByteCounter.

func (*ByteCounter) Write

func (bc *ByteCounter) Write(p []byte) (int, error)

Write implements io.Writer.

type CallbackFn

type CallbackFn func(string)

CallbackFn is a function that takes a string as an argument and returns nothing.

type ScanWriter

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

ScanWriter is an io.WriteCloser wrapper for bufio.Scanner. Instead of calling scanner.Scan() like in bufio.Scanner, you write to it and it calls the given callback function with the contents of the internal buffer every time it encounters the given delimiter.

You must call Close() to flush the remaining buffer contents to the scanner.

Example
package main

import (
	"fmt"
	"io"
	"strings"

	"github.com/k0sproject/rig/v2/iostream"
)

func main() {
	contentReader := strings.NewReader("Hello, World!\nHow are you today?\nNice to meet you.\n")
	i := 1
	sw := iostream.NewScanWriter(func(row string) {
		fmt.Println(i, row)
		i++
	})
	defer sw.Close()

	_, _ = io.Copy(sw, contentReader)
}
Output:
1 Hello, World!
2 How are you today?
3 Nice to meet you.

func (*ScanWriter) Close

func (w *ScanWriter) Close() error

Close closes the writer and the underlying pipe. It returns the last error encountered by the scanner.

func (*ScanWriter) CloseWithError

func (w *ScanWriter) CloseWithError(reason error) error

CloseWithError closes the underlying pipe with an error.

func (*ScanWriter) Err

func (w *ScanWriter) Err() error

Err returns the last error encountered by the scanner.

func (*ScanWriter) Split

func (w *ScanWriter) Split(split bufio.SplitFunc)

Split sets the split function for the scanner. see bufio.Scanner https://pkg.go.dev/bufio#Scanner

func (*ScanWriter) Text

func (w *ScanWriter) Text() string

Text returns the most recent token generated by a call to Scan as a newly allocated string holding its bytes.

func (*ScanWriter) Write

func (w *ScanWriter) Write(p []byte) (int, error)

Write writes the given bytes to the scanner.

Jump to

Keyboard shortcuts

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