Skip to content

Commit

Permalink
Update zip library to the latest commit from golang repo
Browse files Browse the repository at this point in the history
  • Loading branch information
bancek committed May 21, 2024
1 parent 5558a17 commit 1ca444a
Show file tree
Hide file tree
Showing 5 changed files with 540 additions and 177 deletions.
2 changes: 1 addition & 1 deletion zip/.git_revision
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f75aafdf56dd90eab75cfeac8cf69358f73ba171
33d725e5758bf1fea62e6c77fc70b57a828a49f5
54 changes: 24 additions & 30 deletions zip/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"compress/flate"
"errors"
"io"
"io/ioutil"
"sync"
)

Expand All @@ -20,7 +19,7 @@ import (
type Compressor func(w io.Writer) (io.WriteCloser, error)

// A Decompressor returns a new decompressing reader, reading from r.
// The ReadCloser's Close method must be used to release associated resources.
// The [io.ReadCloser]'s Close method must be used to release associated resources.
// The Decompressor itself must be safe to invoke from multiple goroutines
// simultaneously, but each returned reader will be used only by
// one goroutine at a time.
Expand Down Expand Up @@ -103,51 +102,46 @@ func (r *pooledFlateReader) Close() error {
}

var (
mu sync.RWMutex // guards compressor and decompressor maps
compressors sync.Map // map[uint16]Compressor
decompressors sync.Map // map[uint16]Decompressor
)

compressors = map[uint16]Compressor{
Store: func(w io.Writer) (io.WriteCloser, error) { return &nopCloser{w}, nil },
Deflate: func(w io.Writer) (io.WriteCloser, error) { return newFlateWriter(w), nil },
}
func init() {
compressors.Store(Store, Compressor(func(w io.Writer) (io.WriteCloser, error) { return &nopCloser{w}, nil }))
compressors.Store(Deflate, Compressor(func(w io.Writer) (io.WriteCloser, error) { return newFlateWriter(w), nil }))

decompressors = map[uint16]Decompressor{
Store: ioutil.NopCloser,
Deflate: newFlateReader,
}
)
decompressors.Store(Store, Decompressor(io.NopCloser))
decompressors.Store(Deflate, Decompressor(newFlateReader))
}

// RegisterDecompressor allows custom decompressors for a specified method ID.
// The common methods Store and Deflate are built in.
// The common methods [Store] and [Deflate] are built in.
func RegisterDecompressor(method uint16, dcomp Decompressor) {
mu.Lock()
defer mu.Unlock()

if _, ok := decompressors[method]; ok {
if _, dup := decompressors.LoadOrStore(method, dcomp); dup {
panic("decompressor already registered")
}
decompressors[method] = dcomp
}

// RegisterCompressor registers custom compressors for a specified method ID.
// The common methods Store and Deflate are built in.
// The common methods [Store] and [Deflate] are built in.
func RegisterCompressor(method uint16, comp Compressor) {
mu.Lock()
defer mu.Unlock()

if _, ok := compressors[method]; ok {
if _, dup := compressors.LoadOrStore(method, comp); dup {
panic("compressor already registered")
}
compressors[method] = comp
}

func compressor(method uint16) Compressor {
mu.RLock()
defer mu.RUnlock()
return compressors[method]
ci, ok := compressors.Load(method)
if !ok {
return nil
}
return ci.(Compressor)
}

func decompressor(method uint16) Decompressor {
mu.RLock()
defer mu.RUnlock()
return decompressors[method]
di, ok := decompressors.Load(method)
if !ok {
return nil
}
return di.(Decompressor)
}
Loading

0 comments on commit 1ca444a

Please sign in to comment.