Documentation ¶
Overview ¶
Package readerutil implements io.Reader utility functions.
Index ¶
- Constants
- Variables
- func IsUTF16(r io.ReadSeeker, order binary.ByteOrder) (ok bool, err error)
- func IsUTF8(r io.ReadSeeker) (ok bool, err error)
- func NewByteReader(r io.Reader) io.ByteReader
- func NewFilter(r io.Reader, charset string) io.Reader
- func NewSpaceFilter(r io.Reader) io.Reader
- func ReadByte(r io.Reader) (c byte, err error)
- func Size(r io.Seeker) (n int64, err error)
- type BinaryPeeker
- type LineReader
- type Peeker
Examples ¶
Constants ¶
const (
// Whitespace characters include space, tab, carriage return and newline.
Whitespace = " \t\r\n"
)
Variables ¶
var (
ErrUnknownEncoding = errors.New("readerutil.NewLineReader: unknown encoding.")
)
Error values.
Functions ¶
func IsUTF16 ¶
IsUTF16 decodes a chunk of data as UTF-16 with the specified byte order and returns true if a valid BOM byte sequence was located or no decoding errors occured and at least 75% of the decoded runes are graphic or space and within the ASCII range.
func IsUTF8 ¶
func IsUTF8(r io.ReadSeeker) (ok bool, err error)
IsUTF8 decodes a chunk of data as UTF-8 and returns true if no decoding errors occured and at least 75% of the decoded runes are graphic or space and within the ASCII range.
func NewByteReader ¶
func NewByteReader(r io.Reader) io.ByteReader
NewByteReader returns a new io.ByteReader based on the provided io.Reader.
func NewFilter ¶
NewFilter returns an io.Reader which discards characters from charset when reading from r.
Example ¶
r := strings.NewReader("foo,bar;baz") f := NewFilter(r, ",;") io.Copy(os.Stdout, f)
Output: foobarbaz
func NewSpaceFilter ¶
NewSpaceFilter returns an io.Reader which discards whitespace characters when reading from r.
Example ¶
r := strings.NewReader("foo bar\nbaz") f := NewSpaceFilter(r) io.Copy(os.Stdout, f)
Output: foobarbaz
Types ¶
type BinaryPeeker ¶
type BinaryPeeker interface { io.ReadSeeker Peek(data interface{}) (err error) }
A BinaryPeeker is an io.ReadSeeker that can also peek ahead.
Peek reads structured binary data without advancing the reader. Data must be a pointer to a fixed-size value or a slice of fixed-size values. Bytes read from r are decoded using the receiver's byte order and written to successive fields of the data. When reading into structs, the field data for fields with blank (_) field names is skipped; i.e., blank field names may be used for padding.
func NewBinaryPeeker ¶
func NewBinaryPeeker(r io.ReadSeeker, order binary.ByteOrder) BinaryPeeker
NewBinaryPeeker returns a new BinaryPeeker based on the provided io.ReadSeeker.
type LineReader ¶
LineReader implements the ReadLine method.
ReadLine returns a single line, not including the end-of-line bytes. ReadLine either returns a valid line (can be empty) or it returns an error, never both.
func NewLineReader ¶
func NewLineReader(r io.ReadSeeker) (lr LineReader, err error)
NewLineReader returns a LineReader that reads from r. If able to determine the encoding it decodes each line to UTF-8.
type Peeker ¶
type Peeker interface { io.ReadSeeker Peek(n int) (buf []byte, err error) }
A Peeker is an io.ReadSeeker that can also peek ahead.
Peek returns the next n bytes without advancing the reader. The error is EOF only if no bytes were read. If an EOF happens after reading some but not all the bytes, ReadFull returns ErrUnexpectedEOF.
func NewPeeker ¶
func NewPeeker(r io.ReadSeeker) Peeker
NewPeeker returns a new Peeker based on the provided io.ReadSeeker.