当我测试我的文档时,有时,我检查有时我有以下异常(不是每次,有时,但我不明白它什么时候发生,为什么会发生。我只有那个错误日志:
例外是在这条线上
void org.apache.pdfbox.pdmodel.PDDocument.saveIncremental(FileInputStream 输入,OutputStream 输出)
org.apache.pdfbox.exceptions.COSVisitorException: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1354)
at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:217)
at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:206)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:525)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:435)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1122)
at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:552)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1501)
at org.apache.pdfbox.pdmodel.PDDocument.saveIncremental(PDDocument.java:1369)
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at org.apache.pdfbox.io.RandomAccessBuffer.seek(RandomAccessBuffer.java:84)
at org.apache.pdfbox.io.RandomAccessFileInputStream.read(RandomAccessFileInputStream.java:96)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1337)
... 12 more
什么时候发生这个异常?
源代码:
public void sign(File inputDocument, File outputDocument, DigitalSigningToken token,
DigitalSignSettings settings) {
byte[] buffer = new byte[8 * 1024];
FileInputStream fis = new FileInputStream(inputDocument);
FileOutputStream fos = new FileOutputStream(outputDocument);
int c;
while ((c = fis.read(buffer)) != -1) {
fos.write(buffer, 0, c);
}
fis.close();
fis = new FileInputStream(outputDocument);
PDDocument doc = PDDocument.load(inputDocument);
create signature dictionary
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
signature.setSubFilter(PDSignature.SUBFILTER_ETSI_CADES_DETACHED);
signature.setName(settings.getSignerName());
signature.setLocation(settings.getSignerLocation());
signature.setReason(settings.getSignatureReason());
SignatureOptions options = new SignatureOptions();
options.setPage(settings.getPage());
options.setPreferedSignatureSize(settings.getPreferredSize());
options.setVisualSignature(settings.getSignatureImage());
// TokenSignature implements org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface
doc.addSignature(signature, new TokenSignatureInterface(token, profile), options);
doc.saveIncremental(fis, fos);
}
doc.saveIncremental 的异常(不是每次)
在签名方法之前,如果我有System.gc(),一切正常。我不明白为什么。在签名方法之前,我创建了可见的签名:
DigitalSignSettings settings = new DigitalSignSettings();
settings.setSignerName("");
settings.setSignerLocation("");
settings.setSignatureReason("");
settings.setPreferredSize(0);
settings.setPage(1);
settings.setVisualSignEnabled(true);
FileInputStream fin = new FileInputStream("c:\\sig.jpg");
PDFBuilder builder = new VisibleSignaturePDFBuilder();
// builder pattern, which creates pdf vith visible signature
PDFCraator creator = new PDFCraator(builder);
VisibleSignatureConfig properties = new VisibleSignatureConfig(originalDocument.getAbsolutePath(), fin, 1);
ByteArrayInputStream template = creator.buildPDF(properties);
System.gc();
settings.setSignatureImage(template);
PDFSigner signer = new PDFSigner();
signer.signDocument(originalDocument, outputDocument, token, settings);
我还发现,在 Builder 中,如果我注释一种方法,则不会发生此异常。就是这样/ PDJpeg 导致了这个......
@Override
public void createSignatureImage(PDDocument template, InputStream inputStream) throws IOException {
PDJpeg img = new PDJpeg(template, inputStream);
pdfStructure.setJpedImage(img);
logger.info("Visible Signature Image has been created");
inputStream.close();
}
或者代替 System.gc(),如果我创建 for(int i=0; i<10000000; i++){} 这也有效!非常非常非常混乱。哦..我不知道发生了什么...这对我来说很困惑...