2016-07-09 14:09:27 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
2016-07-29 17:23:39 +00:00
|
|
|
* file 'LICENSE.md', which is part of this source code package.
|
2016-07-09 14:09:27 +00:00
|
|
|
*/
|
|
|
|
|
2016-09-08 17:53:45 +00:00
|
|
|
package core
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"os"
|
2016-07-17 19:59:17 +00:00
|
|
|
|
|
|
|
"github.com/unidoc/unidoc/common"
|
2016-07-09 14:09:27 +00:00
|
|
|
)
|
|
|
|
|
2017-08-02 12:56:32 +00:00
|
|
|
// ReadAtLeast reads at least n bytes into slice p.
|
|
|
|
// Returns the number of bytes read (should always be == n), and an error on failure.
|
2017-08-03 15:13:40 +00:00
|
|
|
// TODO (v3): Unexport.
|
2017-08-03 15:33:51 +00:00
|
|
|
func (parser *PdfParser) ReadAtLeast(p []byte, n int) (int, error) {
|
2016-07-09 14:09:27 +00:00
|
|
|
remaining := n
|
|
|
|
start := 0
|
|
|
|
numRounds := 0
|
|
|
|
for remaining > 0 {
|
2017-08-03 15:33:51 +00:00
|
|
|
nRead, err := parser.reader.Read(p[start:])
|
2016-07-09 14:09:27 +00:00
|
|
|
if err != nil {
|
2016-10-31 21:48:25 +00:00
|
|
|
common.Log.Debug("ERROR Failed reading (%d;%d) %s", nRead, numRounds, err.Error())
|
2016-07-09 14:09:27 +00:00
|
|
|
return start, errors.New("Failed reading")
|
|
|
|
}
|
|
|
|
numRounds++
|
|
|
|
start += nRead
|
|
|
|
remaining -= nRead
|
|
|
|
}
|
|
|
|
return start, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current file offset, accounting for buffered position.
|
2017-08-03 15:13:40 +00:00
|
|
|
// TODO (v3): Unexport.
|
2017-08-03 15:33:51 +00:00
|
|
|
func (parser *PdfParser) GetFileOffset() int64 {
|
|
|
|
offset, _ := parser.rs.Seek(0, os.SEEK_CUR)
|
|
|
|
offset -= int64(parser.reader.Buffered())
|
2016-07-09 14:09:27 +00:00
|
|
|
return offset
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek the file to an offset position.
|
2017-08-03 15:13:40 +00:00
|
|
|
// TODO (v3): Unexport.
|
2017-08-03 15:33:51 +00:00
|
|
|
func (parser *PdfParser) SetFileOffset(offset int64) {
|
|
|
|
parser.rs.Seek(offset, os.SEEK_SET)
|
|
|
|
parser.reader = bufio.NewReader(parser.rs)
|
2016-07-09 14:09:27 +00:00
|
|
|
}
|