* @throws PDFException         an invalid stream or file encoding
     * @throws PDFSecurityException if a security provider can not be found
     *                              or there is an error decrypting the file.
     * @throws IOException          if a problem setting up, or parsing the SeekableInput.
     */
    public void setInputStream(InputStream in, String pathOrURL)
            throws PDFException, PDFSecurityException, IOException {
        setDocumentOrigin(pathOrURL);

        if (!isCachingEnabled) {
//System.out.println("Started  downloading PDF to memory : " + (new java.util.Date()));
            // read into memory first
            ConservativeSizingByteArrayOutputStream byteArrayOutputStream =
                    new ConservativeSizingByteArrayOutputStream(100 * 1024, null);

            // write the bytes.
            byte[] buffer = new byte[4096];
            int length;
//                int pdfFileSize = 0;
            // in.read will block until the end of the file is read.
            while ((length = in.read(buffer, 0, buffer.length)) > 0) {
                byteArrayOutputStream.write(buffer, 0, length);
//                    pdfFileSize += length;
            }
            byteArrayOutputStream.flush();
            byteArrayOutputStream.close();
            int size = byteArrayOutputStream.size();
            byteArrayOutputStream.trim();
            byte[] data = byteArrayOutputStream.relinquishByteArray();
//System.out.println("Finished downloading PDF to memory : " + (new java.util.Date()) + "  pdfFileSize: " + pdfFileSize);

            // finally read the cached file
            SeekableByteArrayInputStream byteArrayInputStream =
                    new SeekableByteArrayInputStream(data, 0, size);
            setInputStream(byteArrayInputStream);
        }
        // if caching is allowed cache the url to file
        else {
//System.out.println("Started  downloading PDF to disk : " + (new java.util.Date()));
            // create tmp file and write bytes to it.
            File tempFile = File.createTempFile(
                    "ICEpdfTempFile" + getClass().hashCode(),
                    ".tmp");
            // Delete temp file on exit
            tempFile.deleteOnExit();

            // Write the data to the temp file.
            FileOutputStream fileOutputStream =
                    new FileOutputStream(tempFile.getAbsolutePath(), true);

            // write the bytes.
            byte[] buffer = new byte[4096];
            int length;
//                int pdfFileSize = 0;
            while ((length = in.read(buffer, 0, buffer.length)) > 0) {
                fileOutputStream.write(buffer, 0, length);
//                    pdfFileSize += length;
            }
            fileOutputStream.flush();
            fileOutputStream.close();
//System.out.println("Finished downloading PDF to disk : " + (new java.util.Date()) + "  pdfFileSize: " + pdfFileSize);

            setDocumentCachedFilePath(tempFile.getAbsolutePath());

            // finally read the cached file
            RandomAccessFileInputStream rafis =
                    RandomAccessFileInputStream.build(tempFile);
            setInputStream(rafis);
        }
    }