Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(15)

Side by Side Diff: tiff/buffer.go

Issue 5683050: code review 5683050: go.image/tiff: optimize reading uncompressed files from... (Closed)
Patch Set: diff -r ce578805294d https://code.google.com/p/go.image Created 13 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tiff/reader.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package tiff 5 package tiff
6 6
7 import "io" 7 import "io"
8 8
9 // buffer buffers an io.Reader to satisfy io.ReaderAt. 9 // buffer buffers an io.Reader to satisfy io.ReaderAt.
10 type buffer struct { 10 type buffer struct {
11 r io.Reader 11 r io.Reader
12 buf []byte 12 buf []byte
13 } 13 }
14 14
15 func (b *buffer) ReadAt(p []byte, off int64) (int, error) { 15 // fill reads data from b.r until the buffer contains at least end bytes.
16 » o := int(off) 16 func (b *buffer) fill(end int) error {
17 » end := o + len(p)
18 » if int64(end) != off+int64(len(p)) {
19 » » return 0, io.ErrUnexpectedEOF
20 » }
21
22 m := len(b.buf) 17 m := len(b.buf)
23 if end > m { 18 if end > m {
24 if end > cap(b.buf) { 19 if end > cap(b.buf) {
25 newcap := 1024 20 newcap := 1024
26 for newcap < end { 21 for newcap < end {
27 newcap *= 2 22 newcap *= 2
28 } 23 }
29 newbuf := make([]byte, end, newcap) 24 newbuf := make([]byte, end, newcap)
30 copy(newbuf, b.buf) 25 copy(newbuf, b.buf)
31 b.buf = newbuf 26 b.buf = newbuf
32 } else { 27 } else {
33 b.buf = b.buf[:end] 28 b.buf = b.buf[:end]
34 } 29 }
35 if n, err := io.ReadFull(b.r, b.buf[m:end]); err != nil { 30 if n, err := io.ReadFull(b.r, b.buf[m:end]); err != nil {
36 end = m + n 31 end = m + n
37 b.buf = b.buf[:end] 32 b.buf = b.buf[:end]
38 » » » return copy(p, b.buf[o:end]), err 33 » » » return err
39 } 34 }
40 } 35 }
36 return nil
37 }
41 38
42 » return copy(p, b.buf[o:end]), nil 39 func (b *buffer) ReadAt(p []byte, off int64) (int, error) {
40 » o := int(off)
41 » end := o + len(p)
42 » if int64(end) != off+int64(len(p)) {
43 » » return 0, io.ErrUnexpectedEOF
44 » }
45
46 » err := b.fill(end)
47 » return copy(p, b.buf[o:end]), err
48 }
49
50 // Slice returns a slice of the underlying buffer. The slice contains
51 // n bytes starting at offset off.
52 func (b *buffer) Slice(off, n int) ([]byte, error) {
53 » end := off + n
54 » if err := b.fill(end); err != nil {
55 » » return nil, err
56 » }
57 » return b.buf[off:end], nil
43 } 58 }
44 59
45 // newReaderAt converts an io.Reader into an io.ReaderAt. 60 // newReaderAt converts an io.Reader into an io.ReaderAt.
46 func newReaderAt(r io.Reader) io.ReaderAt { 61 func newReaderAt(r io.Reader) io.ReaderAt {
47 if ra, ok := r.(io.ReaderAt); ok { 62 if ra, ok := r.(io.ReaderAt); ok {
48 return ra 63 return ra
49 } 64 }
50 return &buffer{ 65 return &buffer{
51 r: r, 66 r: r,
52 buf: make([]byte, 0, 1024), 67 buf: make([]byte, 0, 1024),
53 } 68 }
54 } 69 }
OLDNEW
« no previous file with comments | « no previous file | tiff/reader.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b