"bufio"
"errors"
"io"
+ "sync"
+ "sync/atomic"
)
// ErrFormat indicates that decoding encountered an unknown format.
}
// Formats is the list of registered formats.
-var formats []format
+var (
+ formatsMu sync.Mutex
+ atomicFormats atomic.Value
+)
// RegisterFormat registers an image format for use by Decode.
// Name is the name of the format, like "jpeg" or "png".
// Decode is the function that decodes the encoded image.
// DecodeConfig is the function that decodes just its configuration.
func RegisterFormat(name, magic string, decode func(io.Reader) (Image, error), decodeConfig func(io.Reader) (Config, error)) {
- formats = append(formats, format{name, magic, decode, decodeConfig})
+ formatsMu.Lock()
+ formats, _ := atomicFormats.Load().([]format)
+ atomicFormats.Store(append(formats, format{name, magic, decode, decodeConfig}))
+ formatsMu.Unlock()
}
// A reader is an io.Reader that can also peek ahead.
// Sniff determines the format of r's data.
func sniff(r reader) format {
+ formats, _ := atomicFormats.Load().([]format)
for _, f := range formats {
b, err := r.Peek(len(f.magic))
if err == nil && match(f.magic, b) {