reader

package
v0.0.0-...-b2ee3fb Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2018 License: MIT Imports: 17 Imported by: 2

Documentation

Overview

Package reader provides the interfaces, embedded structs, and implementing code for normalizing the reading a stream of bytes from compressed files. This package supports the gzip and snappy compression algorithms. No compression is usually identified as "none". This package is used by the go-stream package.

Usage

You can import reader as a package into your own Go project or use the command line interface.

import (
  "github.com/spatialcurrent/go-reader/reader"
)

r, err := reader.OpenFile("data-for-2018.sz", "snappy")
if err != nil {
  panic(err)
}
for {
  b, err := input_reader.ReadBytes([]byte("\n")[0])
  if err != nil {
    if err != io.EOF {
      fmt.Println(errors.Wrap(err, "Error reading bytes from file"))
      os.Exit(1)
    }
  }
  if len(b) > 0 {
    fmt.Println(string(b))
  }
  if err != nil && err == io.EOF {
    break
  }
}

See the github.com/go-reader/cmd/go-reader package for a command line tool for testing DFL expressions.

Projects

go-reader is used by the go-stream project.

Index

Constants

This section is empty.

Variables

View Source
var VERSION = "0.0.2"

Functions

func OpenHTTPFile

func OpenHTTPFile(uri string, alg string, cache bool) (ByteReadCloser, *Metadata, error)

OpenHTTPFile returns a ByteReadCloser for an object for a web address alg may be "bzip2", "gzip", "snappy", or "".

func OpenResource

func OpenResource(uri string, alg string, buffer_size int, cache bool, s3_client *s3.S3) (ByteReadCloser, *Metadata, error)

func OpenS3Object

func OpenS3Object(bucket string, key string, alg string, cache bool, s3_client *s3.S3) (ByteReadCloser, *Metadata, error)

OpenS3Object returns a ByteReadCloser for an object in AWS S3. alg may be "bzip2", "gzip", "snappy", or "".

func SplitUri

func SplitUri(uri string) (string, string)

SplitUri splits a uri string into a scheme and path. If no scheme is specified, then returns "" as the scheme and the full path.

Types

type ByteReadCloser

type ByteReadCloser interface {
	ByteReader
	io.Closer
	ReadAt(i int) (byte, error)
	ReadAll() ([]byte, error)
	ReadRange(start int, end int) ([]byte, error)
}

ByteReader is an interface that extends io.Reader, io.ByteReader, and adds a range function. ByteReader provides functions for reading bytes.

func Bytes

func Bytes(b []byte) (ByteReadCloser, error)

Bytes returns a reader for reading the bytes from an input array, and an error if any.

func Bzip2File

func Bzip2File(path string, cache bool, buffer_size int) (ByteReadCloser, error)

Bzip2File returns a reader for reading bytes from a bzip2-compressed file Wraps the "compress/gzip" package.

func File

func File(path string, cache bool, buffer_size int) (ByteReadCloser, error)

File returns a ByteReader for reading bytes without any transformation from a file, and an error if any.

func GzipBytes

func GzipBytes(b []byte) (ByteReadCloser, error)

GzipBytes returns a reader for reading gzip bytes from an input array. Wraps the "compress/gzip" package.

func GzipFile

func GzipFile(path string, cache bool, buffer_size int) (ByteReadCloser, error)

GzipFile returns a reader for reading bytes from a gzip-compressed file Wraps the "compress/gzip" package.

func OpenBytes

func OpenBytes(b []byte, alg string) (ByteReadCloser, error)

OpenBytes returns a ByteReader for a byte array with a given compression. alg may be "snappy", "gzip", or "none."

func OpenFile

func OpenFile(uri string, alg string, cache bool, buffer_size int) (ByteReadCloser, error)

OpenFile returns a ByteReader for a file with a given compression. alg may be "snappy", "gzip", or "none."

func OpenStdin

func OpenStdin(alg string, cache bool) (ByteReadCloser, error)

OpenStdin returns a ByteReadCloser for a file with a given compression. alg may be "bzip2", "gzip", "snappy", or "".

func SnappyBytes

func SnappyBytes(b []byte) (ByteReadCloser, error)

SnappyBytes returns a reader for an input of snappy-compressed bytes, and an error if any.

func SnappyFile

func SnappyFile(path string, cache bool, buffer_size int) (ByteReadCloser, error)

SnappyFile returns a reader for a snappy-compressed file, and an error if any.

func ZipFile

func ZipFile(path string, cache bool, buffer_size int) (ByteReadCloser, error)

ZipFile returns a ByteReadCloser for reading bytes from a zip-compressed file Wraps the "archive/zip" package.

type ByteReader

type ByteReader interface {
	io.Reader
	io.ByteReader
	ReadBytes(delim byte) ([]byte, error)
}

ByteReader is an interface that extends io.Reader and io.ByteReader. ByteReader provides functions for reading bytes.

type Cache

type Cache struct {
	*Reader
	Cursor   int
	Complete *bool
	Content  *[]byte
}

func NewCache

func NewCache(r *Reader) *Cache

func NewCacheWithContent

func NewCacheWithContent(r *Reader, c *[]byte, i int) *Cache

func (*Cache) Read

func (c *Cache) Read(p []byte) (n int, err error)

Read reads a maximum len(p) bytes from the reader and returns an error, if any.

func (*Cache) ReadAll

func (c *Cache) ReadAll() ([]byte, error)

ReadAll reads all content from the underlying reader and returns the content

func (*Cache) ReadAt

func (c *Cache) ReadAt(i int) (byte, error)

ReadAt returns the byte at the index given by i

func (*Cache) ReadFirst

func (c *Cache) ReadFirst() (byte, error)

ReadFirst returns the first byte stating at the cursor.

func (*Cache) ReadRange

func (c *Cache) ReadRange(start int, end int) ([]byte, error)

ReadRange reads a range of btes from the cache. End points to the index of the last byte to read, so [start:end+1]

type ErrorNotImplemented

type ErrorNotImplemented struct {
	Function string
	Object   string
}

func (*ErrorNotImplemented) Error

func (e *ErrorNotImplemented) Error() string

type Metadata

type Metadata struct {
	ContentType   string
	LastModified  *time.Time
	ContentLength int64
	Header        map[string][]string
}

func NewMetadataFromHeader

func NewMetadataFromHeader(header map[string][]string) *Metadata

func NewMetadataFromS3

func NewMetadataFromS3(output *s3.GetObjectOutput) *Metadata

type Reader

type Reader struct {
	Reader ByteReader // the instance of ByteReader used for reading bytes
	Closer io.Closer  // Used for closing readers with footer metadata, e.g., gzip.  Not always needed, e.g., snappy
	File   *os.File   // underlying file, if any
}

Reader is a struct for normalizing reading of bytes from files with arbitrary compression and for closing underlying resources. Reader implements the ByteReader interface by wrapping around a subordinate ByteReader.

func (*Reader) Close

func (r *Reader) Close() error

Close closes the Closer and the underlying *os.File if not nil.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read reads a maximum len(p) bytes from the reader and returns an error, if any.

func (*Reader) ReadAll

func (r *Reader) ReadAll() ([]byte, error)

ReadAll is not implemented by Reader

func (*Reader) ReadAt

func (r *Reader) ReadAt(i int) (byte, error)

ReadAt is not implemented by Reader

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

ReadByte returns a single byte from the underlying reader.

func (*Reader) ReadBytes

func (r *Reader) ReadBytes(delim byte) ([]byte, error)

Read returns all bytes up to an including the first occurrence of the delimiter "delim" and an error, if any.

func (*Reader) ReadFirst

func (r *Reader) ReadFirst() (byte, error)

ReadFirst is not implemented by Reader

func (*Reader) ReadRange

func (r *Reader) ReadRange(start int, end int) ([]byte, error)

ReadRange is not implemented by Reader

Jump to

Keyboard shortcuts

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