Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id ACE8D200BE9 for ; Mon, 26 Dec 2016 13:52:34 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id AB8BB160B3B; Mon, 26 Dec 2016 12:52:34 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 01CF9160B2A for ; Mon, 26 Dec 2016 13:52:33 +0100 (CET) Received: (qmail 6475 invoked by uid 500); 26 Dec 2016 12:52:33 -0000 Mailing-List: contact users-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@pdfbox.apache.org Delivered-To: mailing list users@pdfbox.apache.org Received: (qmail 6464 invoked by uid 99); 26 Dec 2016 12:52:32 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 26 Dec 2016 12:52:32 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 6EA891A02EE for ; Mon, 26 Dec 2016 12:52:32 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.5 X-Spam-Level: X-Spam-Status: No, score=0.5 tagged_above=-999 required=6.31 tests=[HTML_MESSAGE=2, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_SORBS_SPAM=0.5, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id CVhhcRlKBsum for ; Mon, 26 Dec 2016 12:52:29 +0000 (UTC) Received: from mailout01.t-online.de (mailout01.t-online.de [194.25.134.80]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id 2E96F5F252 for ; Mon, 26 Dec 2016 12:52:29 +0000 (UTC) Received: from fwd15.aul.t-online.de (fwd15.aul.t-online.de [172.20.27.63]) by mailout01.t-online.de (Postfix) with SMTP id 4070F423BF77 for ; Mon, 26 Dec 2016 13:52:14 +0100 (CET) Received: from [192.168.2.105] (XphkegZXQhiurvjQEEaIf6+INjlwy8FT02C-HcTegaxh8XBnLYGq1N2UXR5duw0Zst@[217.231.139.34]) by fwd15.t-online.de with (TLSv1.2:ECDHE-RSA-AES256-SHA encrypted) esmtp id 1cLUl9-3XsWdE0; Mon, 26 Dec 2016 13:52:11 +0100 Subject: Re: PDDocument.load, password and InputStream To: users@pdfbox.apache.org References: From: Tilman Hausherr Message-ID: <42cf059e-6e8b-9682-61a1-6bf4e93393f1@t-online.de> Date: Mon, 26 Dec 2016 13:52:30 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------8A4C3BDD80A542BA9C47CB07" X-ID: XphkegZXQhiurvjQEEaIf6+INjlwy8FT02C-HcTegaxh8XBnLYGq1N2UXR5duw0Zst X-TOI-MSGID: 2fd86035-032b-461f-96f9-09557c64599e archived-at: Mon, 26 Dec 2016 12:52:34 -0000 --------------8A4C3BDD80A542BA9C47CB07 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Am 26.12.2016 um 12:23 schrieb Maxime GUERREIRO: > Hello everyone, > > I am using PDFBox 2.0.4 and I couldn't find the best way to do it. > I hope you can help me :-) > > I have a simple application that accepts PDF files (among other formats), > and I currently pass it along (as argument to methods) as a File. In order > to cleanup my code, I wanted to pass it as an InputStream instead... but I ... which is not a good idea as it will make everything slower. Because PDFBox makes an internal copy to allow random access. > miss one feature in PDFBox 2.0 detecting if the file is password protected. > I think it was possible using the 1.8 version, as we instantiated the doc and > *then* we'd give it the password. > > Sample code: > ``` > try (PDDocument ignore = PDDocument.load(inFile)) { > return null; // not password protected > } catch (InvalidPasswordException ignore) { > } > ``` That code has a memory leak as "ignore" isn't closed > > The issue is that when I try to reuse inFile, it tells me it has been closed. I doubt that. Unless "inFile" is an input stream and not a file, as the name would suggest. > When wrapping it in a CloseShieldInputStream, the whole file is read... leading > to an unwanted memory consumption and/or a temporary file. > > This was not an issue with File-s because I could re-recreate an InputStream > when I needed to. > > > ~> Is there a way to check if a file is password-protected *and* if the given > password is the right one, without PDFBox reading it as a whole? |document.isEncrypted() == true| then it is encrypted and you gave the correct password (which can be the empty password!!!). If it is encrypted and you gave the wrong password then you'll get an InvalidPasswordException Tilman > > Thanks, > Maxime > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org > For additional commands, e-mail: users-help@pdfbox.apache.org > --------------8A4C3BDD80A542BA9C47CB07--