コード例 #1
0
ファイル: bgzf.go プロジェクト: boumenot/packer
// HasEOF checks for the presence of a BGZF magic EOF block.
// The magic block is defined in the SAM specification. A magic block
// is written by a Writer on calling Close. The ReaderAt must provide
// some method for determining valid ReadAt offsets.
func HasEOF(r io.ReaderAt) (bool, error) {
	type sizer interface {
		Size() int64
	}
	type stater interface {
		Stat() (os.FileInfo, error)
	}
	type lenSeeker interface {
		io.Seeker
		Len() int
	}
	var size int64
	switch r := r.(type) {
	case sizer:
		size = r.Size()
	case stater:
		fi, err := r.Stat()
		if err != nil {
			return false, err
		}
		size = fi.Size()
	case lenSeeker:
		var err error
		size, err = r.Seek(0, 1)
		if err != nil {
			return false, err
		}
		size += int64(r.Len())
	default:
		return false, ErrNoEnd
	}

	b := make([]byte, len(magicBlock))
	_, err := r.ReadAt(b, size-int64(len(magicBlock)))
	if err != nil {
		return false, err
	}
	for i, c := range b {
		if c != magicBlock[i] {
			return false, nil
		}
	}
	return true, nil
}