当前位置: 首页>>代码示例>>Golang>>正文


Golang File.ReadAt方法代码示例

本文整理汇总了Golang中os.File.ReadAt方法的典型用法代码示例。如果您正苦于以下问题:Golang File.ReadAt方法的具体用法?Golang File.ReadAt怎么用?Golang File.ReadAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在os.File的用法示例。


在下文中一共展示了File.ReadAt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: readFromFile

func readFromFile(file *os.File, offset, size int) ([]byte, error) {
	res := make([]byte, size, size)
	if _, err := file.ReadAt(res, int64(offset)); err != nil {
		return nil, err
	}
	return res, nil
}
开发者ID:kjk,项目名称:contentstore,代码行数:7,代码来源:store.go

示例2: Read

// 读取数据,外部需要准备好够存放的desBuf
func (this *BigFile) Read(i BigFileIndex, desBuf []byte) error {
	if i.FileNo >= this.bigfileStat.FileCnt {
		return log.Error("BigFile.Read FileNo[%d] Error", i.FileNo)
	}
	if i.Length > uint32(len(desBuf)) {
		return log.Error("BigFile.Read BigFileIndex.Length[%d] > len(desBuf)[%d]",
			i.Length, uint32(len(desBuf)))
	}

	var f *os.File
	if i.FileNo == this.bigfileStat.FileCnt-1 {
		f = this.readwriteFile
	} else {
		f = this.readOnlyFile[i.FileNo]
	}
	n, err := f.ReadAt(desBuf[:i.Length], int64(i.Offset))
	if err == io.EOF {
		if uint32(n) == i.Length {
			// 刚刚好读完
			return nil
		}
	}
	if uint32(n) != i.Length {
		return log.Error("Read Length Error offset[%d] destBuf len[%d],ReadAt len[%d]",
			i.Offset, i.Length, n)
	}
	if err != nil {
		return log.Error("ReadAt file", err.Error())
	}
	return nil
}
开发者ID:caohao2008,项目名称:goose,代码行数:32,代码来源:bigfile.go

示例3: ReadHashNode

func ReadHashNode(f *os.File, offset int64, dir string) (*HashNode, error) {
	b := make([]byte, hashNodeSize)
	l, err := f.ReadAt(b, offset)

	if err != nil {
		return nil, err
	}
	if l != hashNodeSize {
		return nil, errors.New("didn't read 628 bytes")
	}
	r := bytes.NewReader(b)
	h := &HashNode{Offset: offset}

	binary.Read(r, binary.LittleEndian, &h.Hash)
	if h.Hash == 0 {
		return h, nil
	}
	h.Namespace = nullTerminatedString(b[4 : 128+4])

	namespaceDetailsReader := bytes.NewReader(b[128+4:])
	nd, err := ReadNamespaceDetails(namespaceDetailsReader)
	nd.NamespaceBase = strings.Split(h.Namespace, ".")[0]
	if err != nil {
		return nil, err
	}
	h.NamespaceDetails = nd
	h.NamespaceDetails.Dir = dir
	return h, nil
}
开发者ID:jehiah,项目名称:mongosort,代码行数:29,代码来源:namespace.go

示例4: resourceArmStorageBlobPageSplit

func resourceArmStorageBlobPageSplit(file *os.File) (int64, []resourceArmStorageBlobPage, error) {
	const (
		minPageSize int64 = 4 * 1024
		maxPageSize int64 = 4 * 1024 * 1024
	)

	info, err := file.Stat()
	if err != nil {
		return int64(0), nil, fmt.Errorf("Could not stat file %q: %s", file.Name(), err)
	}

	blobSize := info.Size()
	if info.Size()%minPageSize != 0 {
		blobSize = info.Size() + (minPageSize - (info.Size() % minPageSize))
	}

	emptyPage := make([]byte, minPageSize)

	type byteRange struct {
		offset int64
		length int64
	}

	var nonEmptyRanges []byteRange
	var currentRange byteRange
	for i := int64(0); i < blobSize; i += minPageSize {
		pageBuf := make([]byte, minPageSize)
		_, err = file.ReadAt(pageBuf, i)
		if err != nil && err != io.EOF {
			return int64(0), nil, fmt.Errorf("Could not read chunk at %d: %s", i, err)
		}

		if bytes.Equal(pageBuf, emptyPage) {
			if currentRange.length != 0 {
				nonEmptyRanges = append(nonEmptyRanges, currentRange)
			}
			currentRange = byteRange{
				offset: i + minPageSize,
			}
		} else {
			currentRange.length += minPageSize
			if currentRange.length == maxPageSize || (currentRange.offset+currentRange.length == blobSize) {
				nonEmptyRanges = append(nonEmptyRanges, currentRange)
				currentRange = byteRange{
					offset: i + minPageSize,
				}
			}
		}
	}

	var pages []resourceArmStorageBlobPage
	for _, nonEmptyRange := range nonEmptyRanges {
		pages = append(pages, resourceArmStorageBlobPage{
			offset:  nonEmptyRange.offset,
			section: io.NewSectionReader(file, nonEmptyRange.offset, nonEmptyRange.length),
		})
	}

	return info.Size(), pages, nil
}
开发者ID:Originate,项目名称:terraform,代码行数:60,代码来源:resource_arm_storage_blob.go

示例5: fetchByFetchlist

// fetch the messages in the supplied fetchlist and send them to the channel
func (p *MessagePartition) fetchByFetchlist(fetchList []fetchEntry, messageC chan MessageAndId) error {

	var fileId uint64
	var file *os.File
	var err error
	var lastMsgId uint64
	for _, f := range fetchList {
		if lastMsgId == 0 {
			lastMsgId = f.messageId - 1
		}
		lastMsgId = f.messageId

		// ensure, that we read on the correct file
		if file == nil || fileId != f.fileId {
			file, err = p.checkoutMessagefile(f.fileId)
			if err != nil {
				return err
			}
			defer p.releaseMessagefile(f.fileId, file)
			fileId = f.fileId
		}

		msg := make([]byte, f.size, f.size)
		_, err = file.ReadAt(msg, f.offset)
		if err != nil {
			return err
		}
		messageC <- MessageAndId{f.messageId, msg}
	}
	return nil
}
开发者ID:tkrille,项目名称:guble,代码行数:32,代码来源:message_partition.go

示例6: readMachoGoBuildID

// The Go build ID is stored at the beginning of the Mach-O __text segment.
// The caller has already opened filename, to get f, and read a few kB out, in data.
// Sadly, that's not guaranteed to hold the note, because there is an arbitrary amount
// of other junk placed in the file ahead of the main text.
func readMachoGoBuildID(filename string, f *os.File, data []byte) (buildid string, err error) {
	// If the data we want has already been read, don't worry about Mach-O parsing.
	// This is both an optimization and a hedge against the Mach-O parsing failing
	// in the future due to, for example, the name of the __text section changing.
	if b, err := readRawGoBuildID(filename, data); b != "" && err == nil {
		return b, err
	}

	mf, err := macho.NewFile(f)
	if err != nil {
		return "", &os.PathError{Path: filename, Op: "parse", Err: err}
	}

	sect := mf.Section("__text")
	if sect == nil {
		// Every binary has a __text section. Something is wrong.
		return "", &os.PathError{Path: filename, Op: "parse", Err: fmt.Errorf("cannot find __text section")}
	}

	// It should be in the first few bytes, but read a lot just in case,
	// especially given our past problems on OS X with the build ID moving.
	// There shouldn't be much difference between reading 4kB and 32kB:
	// the hard part is getting to the data, not transferring it.
	n := sect.Size
	if n > uint64(BuildIDReadSize) {
		n = uint64(BuildIDReadSize)
	}
	buf := make([]byte, n)
	if _, err := f.ReadAt(buf, int64(sect.Offset)); err != nil {
		return "", err
	}

	return readRawGoBuildID(filename, buf)
}
开发者ID:sreis,项目名称:go,代码行数:38,代码来源:note.go

示例7: Offset

func Offset(bin *os.File) (int64, error) {
	var n int

	fi, err := bin.Stat()
	if err != nil {
		return 0, err
	}
	buf := make([]byte, len(tag))
	n, err = bin.ReadAt(buf, fi.Size()-int64(len(buf))-8)
	if n < len(buf) {
		return 0, InvalidFileErr
	}

	for i, tagByte := range tag {
		if buf[i] != tagByte {
			return 0, InvalidFileErr
		}
	}

	buf = make([]byte, 8)
	n, err = bin.ReadAt(buf, fi.Size()-int64(len(buf)))
	if n < len(buf) {
		return 0, InvalidFileErr
	}

	dataSize := int64(binary.LittleEndian.Uint64(buf))
	offset := fi.Size() - dataSize - int64(len(tag)) - 8
	return offset, nil
}
开发者ID:pombredanne,项目名称:rand,代码行数:29,代码来源:file.go

示例8: verifyChecksum

// verifyChecksum computes the hash of a file and compares it
// to a checksum. If comparison fails, it returns an error.
func verifyChecksum(fd *os.File, checksum string) (err error) {
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("verifyChecksum() -> %v", e)
		}
	}()
	var h hash.Hash
	h = sha256.New()
	buf := make([]byte, 4096)
	var offset int64 = 0
	for {
		block, err := fd.ReadAt(buf, offset)
		if err != nil && err != io.EOF {
			panic(err)
		}
		if block == 0 {
			break
		}
		h.Write(buf[:block])
		offset += int64(block)
	}
	hexhash := fmt.Sprintf("%x", h.Sum(nil))
	if hexhash != checksum {
		return fmt.Errorf("Checksum validation failed. Got '%s', Expected '%s'.",
			hexhash, checksum)
	}
	return
}
开发者ID:tudalex,项目名称:mig,代码行数:30,代码来源:upgrade.go

示例9: WalkIndexFile

// walks through the index file, calls fn function with each key, offset, size
// stops with the error returned by the fn function
func WalkIndexFile(r *os.File, fn func(key uint64, offset, size uint32) error) error {
	var readerOffset int64
	bytes := make([]byte, 16*RowsToRead)
	count, e := r.ReadAt(bytes, readerOffset)
	glog.V(3).Infoln("file", r.Name(), "readerOffset", readerOffset, "count", count, "e", e)
	readerOffset += int64(count)
	var (
		key          uint64
		offset, size uint32
		i            int
	)

	for count > 0 && e == nil || e == io.EOF {
		for i = 0; i+16 <= count; i += 16 {
			key, offset, size = idxFileEntry(bytes[i : i+16])
			if e = fn(key, offset, size); e != nil {
				return e
			}
		}
		if e == io.EOF {
			return nil
		}
		count, e = r.ReadAt(bytes, readerOffset)
		glog.V(3).Infoln("file", r.Name(), "readerOffset", readerOffset, "count", count, "e", e)
		readerOffset += int64(count)
	}
	return e
}
开发者ID:tnextday,项目名称:seaweedfs,代码行数:30,代码来源:needle_map_memory.go

示例10: ReadAt

func (me data) ReadAt(p []byte, off int64) (n int, err error) {
	for _, fi := range me.info.UpvertedFiles() {
		if off >= fi.Length {
			off -= fi.Length
			continue
		}
		n1 := len(p)
		if int64(n1) > fi.Length-off {
			n1 = int(fi.Length - off)
		}
		var f *os.File
		f, err = os.Open(me.fileInfoName(fi))
		if err != nil {
			return
		}
		n1, err = f.ReadAt(p[:n1], off)
		f.Close()
		if err != nil {
			return
		}
		n += n1
		off = 0
		p = p[n1:]
		if len(p) == 0 {
			break
		}
	}
	return
}
开发者ID:jaswinder-singh,项目名称:torrentsaga,代码行数:29,代码来源:file.go

示例11: openExisting

// ###### Implementation
func openExisting(file *os.File, filename string, config common.Config) (common.WriteAheadLog, error) {
	// Create a buffer for the 8-byte file header.
	// The first 3 bytes are the signature `LOG` followed by an 8-bit version
	// and the boolean flags. Then read the file header into the buffer.
	buf := make([]byte, 8)
	_, err := file.ReadAt(buf, 0)

	// If there was an error reading the file header, close the file and return
	// a nil log and the read error.
	if err != nil {
		file.Close()
		return nil, err
	}

	// If the header was read sucessfully, verify the file signature matches
	// the expected "LOG" signature. If the first 3 bytes do not match `LOG`,
	// return a `nil` log and a `ErrInvalidFileSignature`.
	if !bytes.Equal(buf[0:3], LogFileSignature) {
		return nil, ErrInvalidFileSignature
	}

	// Read the boolean flags from the file header and overwrite the config
	// flags with the ones from the file.
	flags, err := xbinary.LittleEndian.Uint32(buf, 4)
	if err != nil {
		return nil, err
	}
	config.Flags = flags

	// The config version is updated to reflect the actual version of the file.
	// Then return the proper log parser based on the file version.
	config.Version = uint8(buf[3])
	return selectVersion(file, filename, config)
}
开发者ID:blacklabeldata,项目名称:wallaby,代码行数:35,代码来源:wal.go

示例12: getMinMaxValuesWithIndexFromFile

func getMinMaxValuesWithIndexFromFile(file *os.File, startIndex int, lastIndex int, segmentByteSize int) ([]int64, []int64) {
	numberOfSegments := lastIndex - startIndex
	data := make([]byte, segmentByteSize*numberOfSegments)
	mins := make([]int64, numberOfSegments)
	maxs := make([]int64, numberOfSegments)
	n, err := file.ReadAt(data, int64(startIndex*segmentByteSize))
	if err != nil {
		if err == io.EOF {
			return nil, nil
		}
		log.Fatal(err)
	}

	var start int
	var last int
	for i := 0; i < numberOfSegments; i++ {
		start = i * segmentByteSize
		last = (i + 1) * segmentByteSize

		if last > n {
			last = n
		}

		min, max := getMinMaxValue(data[start:last], last-start)
		mins[i] = min
		maxs[i] = max
	}

	return mins, maxs
}
开发者ID:delatech,项目名称:waveform,代码行数:30,代码来源:waveform.go

示例13: readBlock

func readBlock(file *os.File, stat os.FileInfo, doneBlocks *int64, threadID int) {
	block := make([]byte, 4096)
	numBlocks := stat.Size()/4096 + 1
	for {
		blockID := rand.Int63() % numBlocks
		file.ReadAt(block, blockID*4096)
		atomic.AddInt64(doneBlocks, 1)
	}
}
开发者ID:feeblefakie,项目名称:misc,代码行数:9,代码来源:iobench.go

示例14: ReadAt

// ReadAt read into unaligned data buffer via direct I/O
// Use AllocateAligned to avoid extra data fuffer copy
func ReadAt(file *os.File, data []byte, offset int64) (int, error) {
	if alignmentShift(data) == 0 {
		return file.ReadAt(data, offset)
	}
	buf := AllocateAligned(len(data))
	n, err := file.ReadAt(buf, offset)
	copy(data, buf)
	return n, err
}
开发者ID:th3architect,项目名称:longhorn,代码行数:11,代码来源:directfio.go

示例15: copyPacket

func copyPacket(dst *os.File, src *os.File, offset int64, size int) (err os.Error) {
	buf := make([]byte, size)
	_, err = src.ReadAt(buf, offset)
	if err != nil && err != os.EOF {
		return err
	}
	dst.Write(buf)
	return err
}
开发者ID:towerhe,项目名称:pcap_splitter,代码行数:9,代码来源:pcap_splitter.go


注:本文中的os.File.ReadAt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。