title: COSVisitorException Exception
body: <p>when I was testing my document, sometimes, I Inspect  that sometimes I have that following exception (not every time, sometimes, but I dont understand when it is happens, whay happens. I have onely that Error Log:</p>
  
  <p>exception was at this line</p>
  
  <p><strong>void org.apache.pdfbox.pdmodel.PDDocument.saveIncremental(FileInputStream input, OutputStream output)</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p>when this exception was happening?</p>
  
  <p>the source code:</p>
  
  <pre><code>  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);
              }
  </code></pre>
  
  <p>exception at doc.saveIncremental (<strong>not every time</strong>)  </p>
  
  <p>before sign method, if I have <strong>System.gc(),</strong> everything works well. I don't understand why. before sign method I create visible signaure:</p>
  
  <pre><code>    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);
  </code></pre>
  
  <p>I also found that, in Builder, if I comment one method, this exception is not happens. thats it/ PDJpeg caused this...</p>
  
  <pre><code>@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();
  
  }
  </code></pre>
  
  <p>Or instead of   System.gc(), if I create for(int i=0; i&lt;10000000; i++){} this works too! very, very, very confusing.  Oh.. I don't know  what hppens... thats very <strong>confusing</strong> for me...  </p>