本文整理汇总了Golang中io.NewSectionReader函数的典型用法代码示例。如果您正苦于以下问题:Golang NewSectionReader函数的具体用法?Golang NewSectionReader怎么用?Golang NewSectionReader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewSectionReader函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getPack
func getPack(t *testing.T) *PackReader {
// Take a random packfile in our own repository.
packs, err := filepath.Glob("../.git/objects/pack/pack-*.pack")
if err != nil || len(packs) == 0 {
t.Fatalf("globbing failed: %v", err, packs)
}
pname := packs[0]
pack, err := os.Open(pname)
if err != nil {
t.Fatalf("count not open %s: %s", pname, err)
}
idx, err := os.Open(pname[:len(pname)-5] + ".idx")
if err != nil {
t.Fatal(err)
}
packstat, err := pack.Stat()
if err != nil {
t.Fatal("stat pack", err)
}
idxstat, err := idx.Stat()
if err != nil {
t.Fatal("stat idx", err)
}
t.Logf("opening pack %s (%d bytes)", pname, packstat.Size())
pk, err := NewPackReader(
io.NewSectionReader(pack, 0, packstat.Size()),
io.NewSectionReader(idx, 0, idxstat.Size()))
if err != nil {
t.Fatal(err)
}
return pk
}
示例2: Open
// Open returns a ReadCloser that provides access to the File's contents.
func (f *File) Open() (rc io.ReadCloser, err os.Error) {
off := int64(f.headerOffset)
size := int64(f.CompressedSize)
if f.bodyOffset == 0 {
r := io.NewSectionReader(f.zipr, off, f.zipsize-off)
if err = readFileHeader(f, r); err != nil {
return
}
if f.bodyOffset, err = r.Seek(0, os.SEEK_CUR); err != nil {
return
}
if size == 0 {
size = int64(f.CompressedSize)
}
}
if f.hasDataDescriptor() && size == 0 {
// permit SectionReader to see the rest of the file
size = f.zipsize - (off + f.bodyOffset)
}
r := io.NewSectionReader(f.zipr, off+f.bodyOffset, size)
switch f.Method {
case Store: // (no compression)
rc = ioutil.NopCloser(r)
case Deflate:
rc = flate.NewReader(r)
default:
err = UnsupportedMethod
}
if rc != nil {
rc = &checksumReader{rc, crc32.NewIEEE(), f, r}
}
return
}
示例3: TestNewRequestContentLength
func TestNewRequestContentLength(t *testing.T) {
readByte := func(r io.Reader) io.Reader {
var b [1]byte
r.Read(b[:])
return r
}
tests := []struct {
r io.Reader
want int64
}{
{bytes.NewReader([]byte("123")), 3},
{bytes.NewBuffer([]byte("1234")), 4},
{strings.NewReader("12345"), 5},
{strings.NewReader(""), 0},
{NoBody, 0},
// Not detected. During Go 1.8 we tried to make these set to -1, but
// due to Issue 18117, we keep these returning 0, even though they're
// unknown.
{struct{ io.Reader }{strings.NewReader("xyz")}, 0},
{io.NewSectionReader(strings.NewReader("x"), 0, 6), 0},
{readByte(io.NewSectionReader(strings.NewReader("xy"), 0, 6)), 0},
}
for i, tt := range tests {
req, err := NewRequest("POST", "http://localhost/", tt.r)
if err != nil {
t.Fatal(err)
}
if req.ContentLength != tt.want {
t.Errorf("test[%d]: ContentLength(%T) = %d; want %d", i, tt.r, req.ContentLength, tt.want)
}
}
}
示例4: TestNewRequestContentLength
func TestNewRequestContentLength(t *testing.T) {
readByte := func(r io.Reader) io.Reader {
var b [1]byte
r.Read(b[:])
return r
}
tests := []struct {
r io.Reader
want int64
}{
{bytes.NewReader([]byte("123")), 3},
{bytes.NewBuffer([]byte("1234")), 4},
{strings.NewReader("12345"), 5},
// Not detected:
{struct{ io.Reader }{strings.NewReader("xyz")}, 0},
{io.NewSectionReader(strings.NewReader("x"), 0, 6), 0},
{readByte(io.NewSectionReader(strings.NewReader("xy"), 0, 6)), 0},
}
for _, tt := range tests {
req, err := NewRequest("POST", "http://localhost/", tt.r)
if err != nil {
t.Fatal(err)
}
if req.ContentLength != tt.want {
t.Errorf("ContentLength(%T) = %d; want %d", tt.r, req.ContentLength, tt.want)
}
}
}
示例5: parseSFNT
func parseSFNT(r io.ReaderAt, headerOffset int64, table map[int64]Table) (SFNT, error) {
header := new(SfntHeader)
headerSize := int64(binary.Size(header))
sr := io.NewSectionReader(r, headerOffset, headerSize)
if err := binary.Read(sr, binary.BigEndian, header); err != nil {
return nil, err
}
numTables := header.NumTables
offsetTable := make([]OffsetEntry, numTables)
sr = io.NewSectionReader(r, headerOffset+headerSize, int64(binary.Size(offsetTable)))
if err := binary.Read(sr, binary.BigEndian, offsetTable); err != nil {
return nil, err
}
tableMap := make(SFNT)
for _, entry := range offsetTable {
tag := entry.Tag.String()
offset := int64(entry.Offset)
size := int64(entry.Length)
if v, ok := table[offset]; ok {
tableMap[tag] = v
} else {
v = &DefaultTable{entry.Tag, io.NewSectionReader(r, offset, size)}
table[offset] = v
tableMap[tag] = v
}
}
for _, p := range DefaultParser {
for i, v := range tableMap {
tableMap[i] = p.Parse(tableMap, v)
}
}
return tableMap, nil
}
示例6: Open
// Open returns a ReadCloser that provides access to the File's contents.
// Multiple files may be read concurrently.
func (f *File) Open() (rc io.ReadCloser, err error) {
bodyOffset, err := f.findBodyOffset()
if err != nil {
return
}
size := int64(f.CompressedSize64)
r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, size)
dcomp := f.zip.decompressor(f.Method)
if dcomp == nil {
err = ErrAlgorithm
return
}
rc = dcomp(r)
var desr io.Reader
if f.hasDataDescriptor() {
desr = io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset+size, dataDescriptorLen)
}
rc = &checksumReader{
rc: rc,
hash: crc32.NewIEEE(),
f: f,
desr: desr,
}
return
}
示例7: ExampleBytes
func ExampleBytes() {
buf := newWriter(make([]byte, 0, 10))
io.Copy(os.Stdout, buf)
io.Copy(os.Stdout, io.NewSectionReader(*&buf, 0, 100))
io.WriteString(buf, "Hello ")
r := io.NewSectionReader(*&buf, 0, int64(buf.Len()))
io.CopyN(os.Stdout, r, 5)
io.CopyN(os.Stdout, buf, 5)
io.WriteString(buf, "World")
r = io.NewSectionReader(*&buf, 0, int64(buf.Len()))
io.CopyN(os.Stdout, r, 6)
io.WriteString(buf, "abcdefg")
io.Copy(os.Stdout, buf)
io.Copy(os.Stdout, buf)
io.WriteString(buf, "Hello World")
r = io.NewSectionReader(*&buf, 0, int64(buf.Len()))
io.CopyN(os.Stdout, r, 5)
io.CopyN(os.Stdout, buf, 4)
io.WriteString(buf, "abcdefg")
io.Copy(os.Stdout, buf)
io.Copy(os.Stdout, buf)
//Output:
// HelloHello World WorldabcdefgHelloHello Worldabcdefg
}
示例8: Open
// Open returns a ReadCloser that provides access to the File's contents.
// Multiple files may be read concurrently.
func (f *File) Open() (rc io.ReadCloser, err error) {
bodyOffset, err := f.findBodyOffset()
if err != nil {
return
}
size := int64(f.CompressedSize64)
r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, size)
switch f.Method {
case Store: // (no compression)
rc = ioutil.NopCloser(r)
case Deflate:
rc, err = zlib.NewReader(r)
if err != nil {
return
}
default:
err = ErrAlgorithm
return
}
var desr io.Reader
if f.hasDataDescriptor() {
desr = io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset+size, dataDescriptorLen)
}
rc = &checksumReader{rc, crc32.NewIEEE(), f, desr, nil}
return
}
示例9: ExamplePackReader_Extract
func ExamplePackReader_Extract() {
base := ".git/objects/pack/pack-bb4afc76654154e3a9f198723ba89873ecb14293"
fpack, err := os.Open(base + ".pack")
if err != nil {
log.Fatal(err)
}
fidx, err := os.Open(base + ".idx")
if err != nil {
log.Fatal(err)
}
defer fpack.Close()
defer fidx.Close()
packsize, err1 := fpack.Seek(0, os.SEEK_END)
idxsize, err2 := fidx.Seek(0, os.SEEK_END)
if err1 != nil || err2 != nil {
log.Fatal(err1, err2)
}
pk, err := NewPackReader(
io.NewSectionReader(fpack, 0, packsize),
io.NewSectionReader(fidx, 0, idxsize))
if err != nil {
log.Fatal(err)
}
var hash Hash
hex.Decode(hash[:], []byte("2e16bbf779131a90346eab585e9e5c4736d3aeac"))
obj, err := pk.Extract(hash)
if err != nil {
log.Fatal(err)
}
log.Printf("%+v", obj)
}
示例10: Decode
// Decode reads a TIFF image from r and returns it as an image.Image.
// The type of Image returned depends on the contents of the TIFF.
func Decode(r io.Reader) (img image.Image, err os.Error) {
d, err := newDecoder(r)
if err != nil {
return
}
// Check if we have the right number of strips, offsets and counts.
rps := int(d.firstVal(tRowsPerStrip))
numStrips := (d.config.Height + rps - 1) / rps
if rps == 0 || len(d.features[tStripOffsets]) < numStrips || len(d.features[tStripByteCounts]) < numStrips {
return nil, FormatError("inconsistent header")
}
switch d.mode {
case mGray, mGrayInvert:
img = image.NewGray(d.config.Width, d.config.Height)
case mPaletted:
img = image.NewPaletted(d.config.Width, d.config.Height, d.palette)
case mNRGBA:
img = image.NewNRGBA(d.config.Width, d.config.Height)
case mRGB, mRGBA:
img = image.NewRGBA(d.config.Width, d.config.Height)
}
var p []byte
for i := 0; i < numStrips; i++ {
ymin := i * rps
// The last strip may be shorter.
if i == numStrips-1 && d.config.Height%rps != 0 {
rps = d.config.Height % rps
}
offset := int64(d.features[tStripOffsets][i])
n := int64(d.features[tStripByteCounts][i])
switch d.firstVal(tCompression) {
case cNone:
// TODO(bsiegert): Avoid copy if r is a tiff.buffer.
p = make([]byte, 0, n)
_, err = d.r.ReadAt(p, offset)
case cLZW:
r := lzw.NewReader(io.NewSectionReader(d.r, offset, n), lzw.MSB, 8)
p, err = ioutil.ReadAll(r)
r.Close()
case cDeflate, cDeflateOld:
r, err := zlib.NewReader(io.NewSectionReader(d.r, offset, n))
if err != nil {
return nil, err
}
p, err = ioutil.ReadAll(r)
r.Close()
default:
err = UnsupportedError("compression")
}
if err != nil {
return
}
err = d.decode(img, p, ymin, ymin+rps)
}
return
}
示例11: TestFile
func TestFile(t *testing.T) {
if *path == "" {
t.Logf("skipping since no input file specified")
return
}
r, err := OpenFile(*path)
if err != nil {
t.Fatal(err)
}
info, err := r.Info()
if err != nil {
t.Error(err)
}
sects := info.Sections
info.Sections = nil
t.Logf("%+v", info)
for _, sec := range sects {
t.Logf("%s: %+v", secNames[sec.Type], sec)
}
// Test messages.
for _, sec := range sects {
if sec.Type == SecMessages {
for id, off := range sec.Folders {
r := io.NewSectionReader(r.File, off, sec.Offset+16+sec.Length-off)
title, msgs, err := parseMessageFolder(r)
if err != nil {
t.Error(err)
}
t.Logf("Folder %d %q", id, title)
t.Logf("%d messages", len(msgs))
if len(msgs) > 0 {
t.Logf("First message: %s", msgs[0])
t.Logf("Last message: %s", msgs[len(msgs)-1])
}
}
}
if sec.Type == SecMMS {
for id, off := range sec.Folders {
r := io.NewSectionReader(r.File, off, sec.Offset+16+sec.Length-off)
title, msgs, err := parseMMSFolder(r)
if err != nil {
t.Error(err)
}
t.Logf("Folder %d %q", id, title)
t.Logf("%d messages", len(msgs))
if len(msgs) > 0 {
first, last := msgs[0], msgs[len(msgs)-1]
t.Logf("First message: %q...%q", first[:40], first[len(first)-40:])
t.Logf("Last message: %q...%q", last[:40], last[len(last)-40:])
}
}
}
}
}
示例12: Read
func (s *subset) Read() (err error) {
a := io.NewSectionReader(s.fh[0], s.offsets[0], chunkSize)
b := io.NewSectionReader(s.fh[1], s.offsets[1], chunkSize)
s.read[0], err = a.Read(s.b[0])
if err != nil && err != io.EOF {
return err
}
s.read[1], err = b.Read(s.b[1])
if err != nil && err != io.EOF {
return err
}
return
}
示例13: Part
func (i *Idx) Part(part string, idxFilePath string, idxLength int64) (pos int64, length int64, err error) {
// this function is for returning a single pos and length for a given range
// used for non-subset indices where the records are contiguous for the data file
f, err := os.Open(idxFilePath)
if err != nil {
err = errors.New(e.IndexNoFile)
return
}
defer f.Close()
if strings.Contains(part, "-") {
startend := strings.Split(part, "-")
start, startEr := strconv.ParseInt(startend[0], 10, 64)
end, endEr := strconv.ParseInt(startend[1], 10, 64)
if startEr != nil || endEr != nil || start <= 0 || start > int64(idxLength) || end <= 0 || end > int64(idxLength) {
err = errors.New(e.InvalidIndexRange)
return
}
// read start offset and length from index file
sr := io.NewSectionReader(f, (start-1)*16, 16)
srec := make([]int64, 2)
binary.Read(sr, binary.LittleEndian, &srec[0])
binary.Read(sr, binary.LittleEndian, &srec[1])
// read end offset and length from index file
sr = io.NewSectionReader(f, (end-1)*16, 16)
erec := make([]int64, 2)
binary.Read(sr, binary.LittleEndian, &erec[0])
binary.Read(sr, binary.LittleEndian, &erec[1])
pos = srec[0]
length = (erec[0] - srec[0]) + erec[1]
} else {
p, er := strconv.ParseInt(part, 10, 64)
if er != nil || p <= 0 || p > int64(idxLength) {
err = errors.New(e.IndexOutBounds)
return
}
// read offset and length from index file
sr := io.NewSectionReader(f, (p-1)*16, 16)
rec := make([]int64, 2)
binary.Read(sr, binary.LittleEndian, &rec[0])
binary.Read(sr, binary.LittleEndian, &rec[1])
pos = rec[0]
length = rec[1]
}
return
}
示例14: main
func main() {
reader, _ := os.Open("test.txt")
defer reader.Close()
sectionReader := io.NewSectionReader(reader, 5, 10)
fmt.Println(reflect.TypeOf(sectionReader))
var n, total int
var err error
p := make([]byte, 15)
//Read
for {
n, err = sectionReader.Read(p)
if err == io.EOF {
fmt.Println("Find EOF so end total", total)
break
}
total = total + n
fmt.Println("Read value:", string(p[0:n]))
fmt.Println("Read count:", n)
}
//ReadAt
p = make([]byte, 15)
n, _ = sectionReader.ReadAt(p, 4)
fmt.Println("Read value:", string(p[0:n]))
fmt.Println("Read count:", n)
//Seek
sectionReader1 := io.NewSectionReader(reader, 2, 20)
sectionReader1.Seek(2, 0)
p = make([]byte, 10)
n, _ = sectionReader1.Read(p)
fmt.Println("First read value:", string(p[0:n]))
fmt.Println("First read count:", n)
a, _ := sectionReader.Seek(2, 1)
fmt.Println("off - base is", a)
n, _ = sectionReader.Read(p)
fmt.Println("Second Read value:", string(p[0:n]))
fmt.Println("Second Read count:", n)
sectionReader.Seek(8, 2)
n, _ = sectionReader.Read(p)
fmt.Println("Third read value:", string(p[0:n]))
fmt.Println("Third read count:", n)
//size
sectionReader2 := io.NewSectionReader(reader, 5, 20)
fmt.Println("Can read count:", sectionReader2.Size())
p = make([]byte, 10)
n, _ = sectionReader2.Read(p)
fmt.Println("Read count:", n)
fmt.Println("Can read count:", sectionReader2.Size())
}
示例15: Open
// Open returns a new ReadSeeker reading the ELF section.
// Even if the section is stored compressed in the ELF file,
// the ReadSeeker reads uncompressed data.
func (s *Section) Open() io.ReadSeeker {
if s.Flags&SHF_COMPRESSED == 0 {
return io.NewSectionReader(s.sr, 0, 1<<63-1)
}
if s.compressionType == COMPRESS_ZLIB {
return &readSeekerFromReader{
reset: func() (io.Reader, error) {
fr := io.NewSectionReader(s.sr, s.compressionOffset, int64(s.FileSize)-s.compressionOffset)
return zlib.NewReader(fr)
},
size: int64(s.Size),
}
}
err := &FormatError{int64(s.Offset), "unknown compression type", s.compressionType}
return errorReader{err}
}