mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-30 13:48:51 +08:00

As the package was growing large, there is a need to clean up. Testing to refactor into a core (basic pdf parsing and primitive objects) and a higher level model package.
45 lines
1009 B
Go
45 lines
1009 B
Go
/*
|
|
* This file is subject to the terms and conditions defined in
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
*/
|
|
|
|
package core
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"os"
|
|
|
|
"github.com/unidoc/unidoc/common"
|
|
)
|
|
|
|
func (this *PdfParser) ReadAtLeast(p []byte, n int) (int, error) {
|
|
remaining := n
|
|
start := 0
|
|
numRounds := 0
|
|
for remaining > 0 {
|
|
nRead, err := this.reader.Read(p[start:])
|
|
if err != nil {
|
|
common.Log.Error("Failed reading (%d;%d) %s", nRead, numRounds, err.Error())
|
|
return start, errors.New("Failed reading")
|
|
}
|
|
numRounds++
|
|
start += nRead
|
|
remaining -= nRead
|
|
}
|
|
return start, nil
|
|
}
|
|
|
|
// Get the current file offset, accounting for buffered position.
|
|
func (this *PdfParser) GetFileOffset() int64 {
|
|
offset, _ := this.rs.Seek(0, os.SEEK_CUR)
|
|
offset -= int64(this.reader.Buffered())
|
|
return offset
|
|
}
|
|
|
|
// Seek the file to an offset position.
|
|
func (this *PdfParser) SetFileOffset(offset int64) {
|
|
this.rs.Seek(offset, os.SEEK_SET)
|
|
this.reader = bufio.NewReader(this.rs)
|
|
}
|