本文整理汇总了Golang中io.ReadWriteSeeker.Read方法的典型用法代码示例。如果您正苦于以下问题:Golang ReadWriteSeeker.Read方法的具体用法?Golang ReadWriteSeeker.Read怎么用?Golang ReadWriteSeeker.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.ReadWriteSeeker
的用法示例。
在下文中一共展示了ReadWriteSeeker.Read方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ReadRawRecord
// ReadRawRecord reads a raw record from the provided file.
//
// On error, err will no non-nil. Expected error values are io.EOF
// when the end of the file has been reached or io.ErrUnexpectedEOF if
// a complete record was unable to be read.
//
// In the case of io.ErrUnexpectedEOF the file offset will be reset
// back to where it was upon entering this function so it is ready to
// be read from again if it is expected more data will be written to
// the file.
func ReadRawRecord(file io.ReadWriteSeeker) (*RawRecord, error) {
var header RawHeader
/* Get the current offset so we can seek back to it. */
offset, _ := file.Seek(0, 1)
/* Now read in the header. */
err := binary.Read(file, binary.BigEndian, &header)
if err != nil {
file.Seek(offset, 0)
return nil, err
}
/* Create a buffer to hold the raw record data and read the
/* record data into it */
data := make([]byte, header.Len)
n, err := file.Read(data)
if err != nil {
file.Seek(offset, 0)
return nil, err
}
if uint32(n) != header.Len {
file.Seek(offset, 0)
return nil, io.ErrUnexpectedEOF
}
return &RawRecord{header.Type, data}, nil
}
示例2: Compress
func Compress(infile, outfile io.ReadWriteSeeker, passwd string, cb Callback) (err error) {
waveHdr := WaveHeader{}
var dataSize uint32
if dataSize, err = waveHdr.Read(infile); err != nil {
err = errRead
return
} else if dataSize >= 0x7FFFFFFF {
err = fmt.Errorf("incorrect data size info in wav file: %x", dataSize)
return
}
if (waveHdr.chunkId != riffSign) ||
(waveHdr.format != waveSign) ||
(waveHdr.numChannels == 0) ||
(waveHdr.numChannels > maxNCH) ||
(waveHdr.bitsPerSample == 0) ||
(waveHdr.bitsPerSample > maxBPS) {
err = errFormat
return
}
encoder := NewEncoder(outfile)
smpSize := uint32(waveHdr.numChannels * ((waveHdr.bitsPerSample + 7) / 8))
info := Info{
nch: uint32(waveHdr.numChannels),
bps: uint32(waveHdr.bitsPerSample),
sps: waveHdr.sampleRate,
format: formatSimple,
samples: dataSize / smpSize,
}
if len(passwd) > 0 {
encoder.SetPassword(passwd)
info.format = formatEncrypted
}
bufSize := pcmBufferLength * smpSize
buffer := make([]byte, bufSize)
if err = encoder.SetInfo(&info, 0); err != nil {
return
}
var readLen int
for dataSize > 0 {
if bufSize >= dataSize {
bufSize = dataSize
}
if readLen, err = infile.Read(buffer[:bufSize]); err != nil || readLen != int(bufSize) {
err = errRead
return
}
encoder.ProcessStream(buffer[:bufSize], cb)
dataSize -= bufSize
}
encoder.Close()
return
}
示例3: AppendAdder
func AppendAdder(genome io.ReadWriteSeeker, gene genome.Gene, geneMap genome.GeneMap) (pos int64, err os.Error) {
//go to the end of the file
pos, err = genome.Seek(0, 2)
length, err := genome.Write(gene)
if err != nil {
panic(err.String())
}
genome.Seek(pos, 0)
test := make([]byte, length)
genome.Read(test)
if !bytes.Equal(gene.GetData(), test) {
panic("apperder broken")
}
return
}