當前位置: 首頁>>代碼示例>>Golang>>正文


Golang io.ReaderAt類代碼示例

本文整理匯總了Golang中io.ReaderAt的典型用法代碼示例。如果您正苦於以下問題:Golang ReaderAt類的具體用法?Golang ReaderAt怎麽用?Golang ReaderAt使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了ReaderAt類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: readDirectoryEnd

func readDirectoryEnd(r io.ReaderAt, size int64) (dir *directoryEnd, err error) {
	// look for directoryEndSignature in the last 1k, then in the last 65k
	var b []byte
	for i, bLen := range []int64{1024, 65 * 1024} {
		if bLen > size {
			bLen = size
		}
		b = make([]byte, int(bLen))
		if _, err := r.ReadAt(b, size-bLen); err != nil && err != io.EOF {
			return nil, err
		}
		if p := findSignatureInBlock(b); p >= 0 {
			b = b[p:]
			break
		}
		if i == 1 || bLen == size {
			return nil, FormatError
		}
	}

	// read header into struct
	c := binary.LittleEndian
	d := new(directoryEnd)
	d.diskNbr = c.Uint16(b[4:6])
	d.dirDiskNbr = c.Uint16(b[6:8])
	d.dirRecordsThisDisk = c.Uint16(b[8:10])
	d.directoryRecords = c.Uint16(b[10:12])
	d.directorySize = c.Uint32(b[12:16])
	d.directoryOffset = c.Uint32(b[16:20])
	d.commentLen = c.Uint16(b[20:22])
	d.comment = string(b[22 : 22+int(d.commentLen)])
	return d, nil
}
開發者ID:aubonbeurre,項目名稱:gcc,代碼行數:33,代碼來源:reader.go

示例2: IsBZip2

// IsBZip2 checks to see if the received reader's contents are in bzip2 format
// by checking the magic numbers.
func IsBZip2(r io.ReaderAt) (bool, error) {
	h := make([]byte, 3)
	// Read the first 3 bytes
	_, err := r.ReadAt(h, 0)
	if err != nil {
		return false, err
	}
	var hb [3]byte
	// check for bzip2
	hbuf := bytes.NewReader(h)
	err = binary.Read(hbuf, binary.LittleEndian, &hb)
	if err != nil {
		return false, fmt.Errorf("error while checking if input matched bzip2's magic number: %s", err)
	}
	var cb [3]byte
	cbuf := bytes.NewBuffer(magicnumBZip2)
	err = binary.Read(cbuf, binary.BigEndian, &cb)
	if err != nil {
		return false, fmt.Errorf("error while converting bzip2 magic number for comparison: %s", err)
	}
	if hb == cb {
		return true, nil
	}
	return false, nil
}
開發者ID:mohae,項目名稱:magicnum,代碼行數:27,代碼來源:compress.go

示例3: IsGZip

// IsGZip checks to see if the received reader's contents are in gzip format
// by checking the magic numbers.
func IsGZip(r io.ReaderAt) (bool, error) {
	h := make([]byte, 2)
	// Read the first 2 bytes
	_, err := r.ReadAt(h, 0)
	if err != nil {
		return false, err
	}
	var h16 uint16
	// check for gzip
	hbuf := bytes.NewReader(h)
	err = binary.Read(hbuf, binary.BigEndian, &h16)
	if err != nil {
		return false, fmt.Errorf("error while checking if input matched bzip2's magic number: %s", err)
	}
	var c16 uint16
	cbuf := bytes.NewBuffer(magicnumGZip)
	err = binary.Read(cbuf, binary.BigEndian, &c16)
	if err != nil {
		return false, fmt.Errorf("error while converting bzip2 magic number for comparison: %s", err)
	}
	if h16 == c16 {
		return true, nil
	}
	return false, nil
}
開發者ID:mohae,項目名稱:magicnum,代碼行數:27,代碼來源:compress.go

示例4: readHeader

// readHeader reads the header at the end of rd. size is the length of the
// whole data accessible in rd.
func readHeader(rd io.ReaderAt, size int64) ([]byte, error) {
	hl, err := readHeaderLength(rd, size)
	if err != nil {
		return nil, err
	}

	if int64(hl) > size-int64(binary.Size(hl)) {
		return nil, errors.New("header is larger than file")
	}

	if int64(hl) > maxHeaderSize {
		return nil, errors.New("header is larger than maxHeaderSize")
	}

	buf := make([]byte, int(hl))
	n, err := rd.ReadAt(buf, size-int64(hl)-int64(binary.Size(hl)))
	if err != nil {
		return nil, errors.Wrap(err, "ReadAt")
	}

	if n != len(buf) {
		return nil, errors.New("not enough bytes read")
	}

	return buf, nil
}
開發者ID:ckemper67,項目名稱:restic,代碼行數:28,代碼來源:pack.go

示例5: readDirectoryEnd

func readDirectoryEnd(r io.ReaderAt, size int64) (dir *directoryEnd, err os.Error) {
	// look for directoryEndSignature in the last 1k, then in the last 65k
	var b []byte
	for i, bLen := range []int64{1024, 65 * 1024} {
		if bLen > size {
			bLen = size
		}
		b = make([]byte, int(bLen))
		if _, err := r.ReadAt(b, size-bLen); err != nil && err != os.EOF {
			return nil, err
		}
		if p := findSignatureInBlock(b); p >= 0 {
			b = b[p:]
			break
		}
		if i == 1 || bLen == size {
			return nil, FormatError
		}
	}

	// read header into struct
	defer recoverError(&err)
	br := bytes.NewBuffer(b[4:]) // skip over signature
	d := new(directoryEnd)
	read(br, &d.diskNbr)
	read(br, &d.dirDiskNbr)
	read(br, &d.dirRecordsThisDisk)
	read(br, &d.directoryRecords)
	read(br, &d.directorySize)
	read(br, &d.directoryOffset)
	read(br, &d.commentLen)
	d.comment = string(readByteSlice(br, d.commentLen))
	return d, nil
}
開發者ID:machinaut,項目名稱:go,代碼行數:34,代碼來源:reader.go

示例6: Patch

func Patch(delta []byte, baseFile io.ReaderAt) (newFile io.Reader, checksum []byte, err error) {
	ops, checksum, err := readDelta(delta)
	if err != nil {
		log.Println("Couldn't patch:", err)
		return
	}

	var wr bytes.Buffer

	for _, op := range ops {
		switch op.code {
		case ADD:
			b := make([]byte, op.length)
			baseFile.ReadAt(b, int64(op.index))
			wr.Write(b)
			break
		case DATA:
			wr.Write(op.data)
			break
		}
	}

	newFile = &wr
	return
}
開發者ID:rakoo,項目名稱:rproxy,代碼行數:25,代碼來源:patch.go

示例7: readFooter

func readFooter(r io.ReaderAt, size uint64) (mi, ii *bInfo, err error) {
	if size < uint64(footerSize) {
		err = errors.ErrInvalid("file is too short to be an sstable")
		return
	}

	buf := make([]byte, footerSize)
	n, err := r.ReadAt(buf, int64(size)-footerSize)
	if err != nil {
		return
	}

	if bytes.Compare(buf[handlesSize:], magicBytes) != 0 {
		err = errors.ErrInvalid("not an sstable (bad magic number)")
		return
	}

	mi = new(bInfo)
	n, err = mi.decodeFrom(buf)
	if err != nil {
		return
	}

	ii = new(bInfo)
	n, err = ii.decodeFrom(buf[n:])
	if err != nil {
		return
	}

	return
}
開發者ID:yufeng108,項目名稱:goleveldb,代碼行數:31,代碼來源:footer.go

示例8: ReaderAtSize

// Determine the size of a ReaderAt using a binary search. Given that file
// offsets are no larger than int64, there is an upper limit of 64 iterations
// before the EOF is found.
func ReaderAtSize(rd io.ReaderAt) (pos int64, err error) {
	defer errs.Recover(&err)

	// Function to check if the given position is at EOF
	buf := make([]byte, 2)
	checkEOF := func(pos int64) int {
		if pos > 0 {
			cnt, err := rd.ReadAt(buf[:2], pos-1)
			errs.Panic(errs.Ignore(err, io.EOF))
			return 1 - cnt // RetVal[Cnt] = {0: +1, 1: 0, 2: -1}
		} else { // Special case where position is zero
			cnt, err := rd.ReadAt(buf[:1], pos-0)
			errs.Panic(errs.Ignore(err, io.EOF))
			return 0 - cnt // RetVal[Cnt] = {0: 0, 1: -1}
		}
	}

	// Obtain the size via binary search O(log n) => 64 iterations
	posMin, posMax := int64(0), int64(math.MaxInt64)
	for posMax >= posMin {
		pos = (posMax + posMin) / 2
		switch checkEOF(pos) {
		case -1: // Below EOF
			posMin = pos + 1
		case 0: // At EOF
			return pos, nil
		case +1: // Above EOF
			posMax = pos - 1
		}
	}
	panic(errs.New("EOF is in a transient state"))
}
開發者ID:dsnet,項目名稱:golib,代碼行數:35,代碼來源:util.go

示例9: Hash

func Hash(r io.ReaderAt, size int64) (string, error) {
	var hash uint64

	if size < chunkSize*2 {
		return "", errors.New("File is too small")
	}

	// Read head and tail blocks.
	buf := make([]byte, chunkSize*2)
	if _, err := r.ReadAt(buf[:chunkSize], 0); err != nil {
		return "", err
	}
	if _, err := r.ReadAt(buf[chunkSize:], size-chunkSize); err != nil {
		return "", err
	}

	// Convert to uint64, and sum.
	nums := make([]uint64, (chunkSize*2)/8)
	reader := bytes.NewReader(buf)
	if err := binary.Read(reader, binary.LittleEndian, &nums); err != nil {
		return "", err
	}
	for _, num := range nums {
		hash += num
	}

	return fmt.Sprintf("%016x", hash+uint64(size)), nil
}
開發者ID:xfanity,項目名稱:pulsar,代碼行數:28,代碼來源:hash.go

示例10: compXmlStringAt

// compXmlStringAt -- Return the string stored in StringTable format at
// offset strOff.  This offset points to the 16 bit string length, which
// is followed by that number of 16 bit (Unicode) chars.
func compXmlStringAt(arr io.ReaderAt, meta stringsMeta, strOff uint32) string {
	if strOff == 0xffffffff {
		return ""
	}
	length := make([]byte, 2)
	off := meta.StringDataOffset + meta.DataOffset[strOff]
	arr.ReadAt(length, int64(off))
	strLen := int(length[1]<<8 + length[0])

	chars := make([]byte, int64(strLen))
	ii := 0
	for i := 0; i < strLen; i++ {
		c := make([]byte, 1)
		arr.ReadAt(c, int64(int(off)+2+ii))

		if c[0] == 0 {
			i--
		} else {
			chars[i] = c[0]
		}
		ii++
	}

	return string(chars)
} // end of compXmlStringAt
開發者ID:mehulsbhatt,項目名稱:android-3,代碼行數:28,代碼來源:xml.go

示例11: decrypt_part_of_file

func decrypt_part_of_file(input_file, key_file io.ReaderAt, output_filename string, chunk_len, chunk_num, from int64) {
	data := make([]byte, chunk_len)
	key := make([]byte, chunk_len)
	xored_data := make([]byte, chunk_len)
	var err error
	output_file, err := os.Create(output_filename)
	check(err)
	defer output_file.Close()

	for i := int64(0); i < chunk_num; i++ {
		n, err := input_file.ReadAt(data, from+i*chunk_len)
		if err != nil && err != io.EOF {
			panic(err)
		}

		_, err = key_file.ReadAt(key[:n], from+i*chunk_len)
		if err != nil && err != io.EOF {
			panic(err)
		}

		xor(data[:n], key[:n], xored_data[:n])
		_, err = output_file.WriteAt(xored_data[:n], from+i*chunk_len)
		check(err)
	}
}
開發者ID:ninedraft,項目名稱:xorefy,代碼行數:25,代碼來源:xor.go

示例12: ReadAt

func (cr ChunkedReader) ReadAt(p []byte, off int64) (n int, err error) {
	n = 0
	for len(p) > 0 {
		var startOff int64
		var r io.ReaderAt

		startOff, r, err = cr(off)
		if err != nil {
			return
		}

		var m int
		m, err = r.ReadAt(p, off-startOff)

		n += m
		off += int64(m)
		p = p[m:]

		if err == nil {
			if len(p) > 0 {
				panic("ReaderAt returned a non-full read without errors")
			}
			return
		}

		if err == io.EOF {
			err = nil
		}
		if err != nil {
			return
		}
	}
	return
}
開發者ID:robryk,項目名稱:goddar,代碼行數:34,代碼來源:chunk_reader.go

示例13: IsTar

// IsTar checks to see if the received reader's contents are in the tar format
// by checking the magic numbers. This evaluates using both tar1 and tar2 magic
// numbers.
func IsTar(r io.ReaderAt) (bool, error) {
	h := make([]byte, 8)
	// Read the first 8 bytes at offset 257
	_, err := r.ReadAt(h, 257)
	if err != nil {
		return false, err
	}
	var h64 uint64
	// check for Zip
	hbuf := bytes.NewReader(h)
	err = binary.Read(hbuf, binary.BigEndian, &h64)
	if err != nil {
		return false, fmt.Errorf("error while checking if input matched tar's magic number: %s", err)
	}
	var c64 uint64
	cbuf := bytes.NewBuffer(magicnumTar1)
	err = binary.Read(cbuf, binary.BigEndian, &c64)
	if err != nil {
		return false, fmt.Errorf("error while converting the tar magic number for comparison: %s", err)
	}
	if h64 == c64 {
		return true, nil
	}
	cbuf = bytes.NewBuffer(magicnumTar2)
	err = binary.Read(cbuf, binary.BigEndian, &c64)
	if err != nil {
		return false, fmt.Errorf("error while converting the empty tar magic number for comparison: %s", err)
	}
	if h64 == c64 {
		return true, nil
	}
	return false, nil
}
開發者ID:mohae,項目名稱:magicnum,代碼行數:36,代碼來源:compress.go

示例14: IsLZ4

// IsLZ4 checks to see if the received reader's contents are in LZ4 foramt by
// checking the magic numbers.
func IsLZ4(r io.ReaderAt) (bool, error) {
	h := make([]byte, 4)
	// Read the first 4 bytes
	_, err := r.ReadAt(h, 0)
	if err != nil {
		return false, err
	}
	var h32 uint32
	// check for lz4
	hbuf := bytes.NewReader(h)
	err = binary.Read(hbuf, binary.LittleEndian, &h32)
	if err != nil {
		return false, fmt.Errorf("error while checking if input matched LZ4's magic number: %s", err)
	}
	var c32 uint32
	cbuf := bytes.NewBuffer(magicnumLZ4)
	err = binary.Read(cbuf, binary.BigEndian, &c32)
	if err != nil {
		return false, fmt.Errorf("error while converting LZ4 magic number for comparison: %s", err)
	}
	if h32 == c32 {
		return true, nil
	}
	return false, nil
}
開發者ID:mohae,項目名稱:magicnum,代碼行數:27,代碼來源:compress.go

示例15: readNumRecs

func readNumRecs(r io.ReaderAt) (int64, error) {
	var buf [4]byte
	_, err := r.ReadAt(buf[:], _NumRecsOffset)
	if err != nil {
		return 0, err
	}
	return int64(buf[0])<<24 + int64(buf[1])<<16 + int64(buf[2])<<8 + int64(buf[3]), nil
}
開發者ID:lvdlvd,項目名稱:go-cdf,代碼行數:8,代碼來源:numrecs.go


注:本文中的io.ReaderAt類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。