Skip to content

Instantly share code, notes, and snippets.

@eikeon
Created September 27, 2013 17:17
Show Gist options
  • Save eikeon/6731904 to your computer and use it in GitHub Desktop.
Save eikeon/6731904 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"github.com/eikeon/gzip"
)
type byteCounter struct {
r *bufio.Reader
offset int64
}
func (b *byteCounter) Read(p []byte) (int, error) {
n, err := b.r.Read(p)
b.offset += int64(n)
return n, err
}
func (b *byteCounter) ReadByte() (byte, error) {
c, err := b.r.ReadByte()
if err == nil {
b.offset++
}
return c, err
}
func foo() error {
f, err := os.Open("test.gz")
if err != nil {
log.Fatal(err)
}
bc := &byteCounter{r: bufio.NewReader(f)}
gz, err := gzip.NewReader(bc)
if err != nil {
log.Fatal(err)
}
gz.StopAtBoundary(true)
var off int64
for {
n, err := io.Copy(ioutil.Discard, gz)
if err != nil {
log.Fatalf("@%d: %d bytes + error: %v", off, n, err)
}
if off == bc.offset {
fmt.Printf("@%d: EOF\n", off)
break
}
fmt.Printf("@%d: %d bytes uncompressed\n", off, n)
off = bc.offset
}
return err
}
func bar(off int64) error {
f, _ := os.Open("test.gz")
_, err := f.Seek(off, 0)
if err != nil {
return err
}
bc := &byteCounter{r: bufio.NewReader(f)}
bc.offset = off
gz, err := gzip.NewReader(bc)
if err != nil {
log.Fatal(err)
}
gz.StopAtBoundary(true)
n, err := io.Copy(ioutil.Discard, gz)
if err != nil {
log.Fatalf("@%d: %d bytes + error: %v", off, n, err)
}
if off == bc.offset {
fmt.Printf("@%d: EOF\n", off)
}
fmt.Printf("@%d,%d: %d bytes uncompressed\n", off, bc.offset, n)
off = bc.offset
return err
}
func main() {
fmt.Printf("foo() -> err: %v\n", foo())
fmt.Println("---")
fmt.Printf("bar(417) -> err: %v\n", bar(417))
fmt.Printf("bar(12846) -> err: %v\n", bar(12846))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment