sizeio

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2020 License: MIT Imports: 2 Imported by: 1

README

go-sizeio

PkgGoDev Test Status codecov

Extends readers with an additional method Size returning the size of the reader content. See the documentation for more information.

Installation

Add this package to go.mod and go.sub in your Go project:

go get github.com/prantlf/go-sizeio

Usage

Use convenience methods to create readers with the extra Size method:

import "github.com/prantlf/go-sizeio"
// get a file reader/closer with the size available right away
readerWithSize := sizeio.OpenFile("...")
// get a general reader with the size known ahead
readerWithSize := sizeio.SizeReader(otherReader, 456)
// test any reader whether it supports the Size method
_, ok := someReader.(sizeio.WithSize)
// get the size of the reader content
size := readerWithSize.Size() // returns 456 as in64

See the documentation for the full interface.

Documentation

Overview

Package sizeio extends interfaces Reader and ReadCloser with the interface WithSize, which provides the size of the reader content.

Strings and bytes support the Size method out-of-tbe-box:

readerWithSize := strings.NewReader("...")
readerWithSize := bytes.NewReader([]byte{...})

Files can be opened or converted using the following convenience methods:

readerWithSize := sizeio.OpenFile("...")
readerWithSize := sizeio.SizeFile(file)

Readers can be converted using the following convenience methods:

readerWithSize := sizeio.SizeReader(reader, 456)
readerWithSize := sizeio.SizeReadCloser(readClose, 789)

Otherwise you can use a structure with the following method, which is essentially the WithSize interface:

Size() int64

And finally, test a reader and get the size of the reader content:

_, ok := reader.(sizeio.WithSize) // returns true is Size is provided
size := readerWithSize.Size()     // returns 789 as in64
Example
// Open a file for reading and obtain its size right away.
reader, err := sizeio.OpenFile("demo/test.txt")
if err != nil {
	log.Fatal(err)
}
defer reader.Close()

// Test any reader whether it supports the Size method
_, ok := reader.(sizeio.WithSize)

// Get the size of the content delivered by the reader.
size := reader.Size()

fmt.Printf("Supported: %v\n", ok)
fmt.Printf("Size:      %d\n", size)
fmt.Printf("Content:   %s\n", demo.StringifyReader(reader))
Output:

Supported: true
Size:      17
Content:   text file content

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ReadCloserWithSize

type ReadCloserWithSize interface {
	io.ReadCloser
	WithSize
}

ReadCloserWithSize encapsulates two interfaces - ReadCloser and WithSize. It is returned by SizeReadCloser.

func OpenFile

func OpenFile(filePath string) (ReadCloserWithSize, error)

OpenFile opens the named file for reading only. It obtains the file size right away to support the WithSize interface.

Do not forget to close the reader, once you do not need it, or defer the closure to perform it automatically in case of a failure.

Example
// Open a file for reading and obtain its size right away.
reader, err := sizeio.OpenFile("demo/test.txt")
if err != nil {
	log.Fatal(err)
}
defer reader.Close()

fmt.Printf("Size: %d\n", reader.Size())
Output:

Size: 17

func SizeFile added in v1.1.0

func SizeFile(file *os.File) (ReadCloserWithSize, error)

SizeFile gets the file size from an opened file and adds to the resulting ReaderCloser within the interface WithSize.

The input File will be wrapped in the output ReaderCloser. Do not forget to close it, once you do not need it, or defer the closure to perform it automatically in case of a failure.

Example
file, err := os.Open("demo/test.txt")
if err != nil {
	log.Fatal(err)
}

// Add the file size to the file reader.
reader, err := sizeio.SizeFile(file)
if err != nil {
	file.Close()
	log.Fatal(err)
}
defer reader.Close()

fmt.Printf("Size: %d\n", reader.Size())
Output:

Size: 17

func SizeReadCloser

func SizeReadCloser(reader io.ReadCloser, size int64) ReadCloserWithSize

SizeReadCloser adds the interface WithSize to a ReadCloser instance.

Example
input := ioutil.NopCloser(strings.NewReader("content"))

// Add size to a reader/closer.
reader := sizeio.SizeReadCloser(input, 7)
defer reader.Close()

fmt.Printf("Size: %d\n", reader.Size())
Output:

Size: 7

type ReaderWithSize

type ReaderWithSize interface {
	io.Reader
	WithSize
}

ReaderWithSize encapsulates two interfaces - Reader and WithSize. It is returned by SizeReader.

func SizeReader

func SizeReader(reader io.Reader, size int64) ReaderWithSize

SizeReader adds the interface WithSize to a Reader instance.

Example
input := strings.NewReader("content")

// Add size to a reader.
reader := sizeio.SizeReader(input, 7)

fmt.Printf("Size: %d\n", reader.Size())
Output:

Size: 7

type WithSize

type WithSize interface {
	// Size returns the size of the content delivered by a reader.
	Size() int64
}

WithSize provides the size of the reader content. Used by ReaderWithSize and ReadCloserWithSize.

Example
input := strings.NewReader("content")

// Add size to a reader.
reader := sizeio.SizeReader(input, 7)

_, ok := reader.(sizeio.WithSize)
fmt.Printf("Supported: %v\n", ok)
Output:

Supported: true

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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