1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
[![GoDoc] (https://godoc.org/github.com/brentp/xopen?status.png)](https://godoc.org/github.com/brentp/xopen)
[](https://travis-ci.org/brentp/xopen)
[](https://coveralls.io/r/brentp/xopen?branch=master)
# xopen
--
import "github.com/brentp/xopen"
xopen makes it easy to get buffered (possibly gzipped) readers and writers. and
close all of the associated files. Ropen opens a file for reading. Wopen opens a
file for writing. Both will use gzip when appropriate and will use buffered IO.
## Usage
Here's how to get a buffered reader:
```go
// gzipped
rdr, err := xopen.Ropen("some.gz")
// normal
rdr, err := xopen.Ropen("some.txt")
// stdin (possibly gzipped)
rdr, err := xopen.Ropen("-")
// https://
rdr, err := xopen.Ropen("http://example.com/some-file.txt")
// Cmd
rdr, err := xopen.Ropen("|ls -lh somefile.gz")
// User directory:
rdr, err := xopen.Ropen("~/brentp/somefile")
```
Get a buffered writer with `xopen.Wopen`.
#### func CheckBytes
```go
func CheckBytes(b *bufio.Reader, buf []byte) (bool, error)
```
CheckBytes peeks at a buffered stream and checks if the first read bytes match.
#### func IsGzip
```go
func IsGzip(b *bufio.Reader) (bool, error)
```
IsGzip returns true buffered Reader has the gzip magic.
#### func IsStdin
```go
func IsStdin() bool
```
IsStdin checks if we are getting data from stdin.
#### func XReader
```go
func XReader(f string) (io.Reader, error)
```
XReader returns a reader from a url string or a file.
#### type Reader
```go
type Reader struct {
*bufio.Reader
}
```
Reader is returned by Ropen
#### func Buf
```go
func Buf(r io.Reader) *Reader
```
Return a buffered reader from an io.Reader If f == "-", then it will attempt to
read from os.Stdin. If the file is gzipped, it will be read as such.
#### func Ropen
```go
func Ropen(f string) (*Reader, error)
```
Ropen opens a buffered reader.
#### func (*Reader) Close
```go
func (r *Reader) Close() error
```
Close the associated files.
#### type Writer
```go
type Writer struct {
*bufio.Writer
}
```
Writer is returned by Wopen
#### func Wopen
```go
func Wopen(f string) (*Writer, error)
```
Wopen opens a buffered reader. If f == "-", then stdout will be used. If f
endswith ".gz", then the output will be gzipped.
#### func (*Writer) Close
```go
func (w *Writer) Close() error
```
Close the associated files.
#### func (*Writer) Flush
```go
func (w *Writer) Flush()
```
Flush the writer.
|