Skip to content
This repository was archived by the owner on Jul 7, 2020. It is now read-only.

reader: add Close method #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
reader: add Close method
The Close method closes the ReaderAtCloser that underlies the Reader.
Shugyousha committed Jun 28, 2017
commit 6d61a2839b689e67118f6ab425f9e3cbd451f07a
17 changes: 14 additions & 3 deletions read.go
Original file line number Diff line number Diff line change
@@ -75,9 +75,15 @@ import (
"strconv"
)

// ReaderAtCloser combines the io.ReaderAt and io.Closer interfaces.
type ReaderAtCloser interface {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest naming this type ReadAtCloser. That would be consistent with how io.Reader and io.ReadCloser are combined in io.ReadCloser instead of io.ReaderCloser. There are more examples for this in io.

io.ReaderAt
io.Closer
}

// A Reader is a single PDF file open for reading.
type Reader struct {
f io.ReaderAt
f ReaderAtCloser
end int64
xref []xref
trailer dict
@@ -113,15 +119,15 @@ func Open(file string) (*Reader, error) {
}

// NewReader opens a file for reading, using the data in f with the given total size.
func NewReader(f io.ReaderAt, size int64) (*Reader, error) {
func NewReader(f ReaderAtCloser, size int64) (*Reader, error) {
return NewReaderEncrypted(f, size, nil)
}

// NewReaderEncrypted opens a file for reading, using the data in f with the given total size.
// If the PDF is encrypted, NewReaderEncrypted calls pw repeatedly to obtain passwords
// to try. If pw returns the empty string, NewReaderEncrypted stops trying to decrypt
// the file and returns an error.
func NewReaderEncrypted(f io.ReaderAt, size int64, pw func() string) (*Reader, error) {
func NewReaderEncrypted(f ReaderAtCloser, size int64, pw func() string) (*Reader, error) {
buf := make([]byte, 10)
f.ReadAt(buf, 0)
if !bytes.HasPrefix(buf, []byte("%PDF-1.")) || buf[7] < '0' || buf[7] > '7' || buf[8] != '\r' && buf[8] != '\n' {
@@ -705,6 +711,11 @@ func (v Value) Len() int {
return len(x)
}

// Close closes the underlying ReaderAtCloser of the Reader.
func (r *Reader) Close() error {
return r.f.Close()
}

func (r *Reader) resolve(parent objptr, x interface{}) Value {
if ptr, ok := x.(objptr); ok {
if ptr.id >= uint32(len(r.xref)) {