Skip to content

Commit e27ff86

Browse files
committed
PDFBOX-5660: close input, as suggested by Valery Bokov; refactor; closes #313
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1929489 13f79535-47bb-0310-9956-ffa450edef68
1 parent 76d830a commit e27ff86

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ static void init() throws IOException, GeneralSecurityException
169169

170170
// load the keystore
171171
keyStore = KeyStore.getInstance("PKCS12");
172-
keyStore.load(new FileInputStream(KEYSTORE_PATH), PASSWORD.toCharArray());
172+
try (InputStream is = new FileInputStream(KEYSTORE_PATH))
173+
{
174+
keyStore.load(is, PASSWORD.toCharArray());
175+
}
173176

174177
new File("target/test-output").mkdirs();
175178

@@ -192,13 +195,12 @@ void testTimeDifference() throws IOException, URISyntaxException
192195
Date localTime = new Date();
193196

194197
// https://stackoverflow.com/questions/4442192/
195-
NTPUDPClient timeClient = new NTPUDPClient();
196198
InetAddress inetAddress = InetAddress.getByName("pool.ntp.org");
197-
timeClient.setDefaultTimeout(Duration.ofMillis(5000));
198199
TimeInfo timeInfo;
199200
long returnTime;
200-
try
201+
try (NTPUDPClient timeClient = new NTPUDPClient())
201202
{
203+
timeClient.setDefaultTimeout(Duration.ofMillis(5000));
202204
timeInfo = timeClient.getTime(inetAddress);
203205
returnTime = timeInfo.getReturnTime();
204206
}
@@ -593,13 +595,14 @@ private void checkSignature(File origFile, File signedFile, boolean checkTimeSta
593595
assertArrayEquals(signedFileContent1, signedFileContent2);
594596

595597
// verify that all getContents() methods returns the same content
596-
try (FileInputStream fis = new FileInputStream(signedFile))
598+
byte[] contents2 = sig.getContents(totalFileContent);
599+
assertArrayEquals(contents, contents2);
600+
try (InputStream is = new FileInputStream(signedFile))
597601
{
598-
byte[] contents2 = sig.getContents(fis.readAllBytes());
599-
assertArrayEquals(contents, contents2);
602+
byte[] contents3 = sig.getContents(is);
603+
assertArrayEquals(contents, contents3);
600604
}
601-
byte[] contents3 = sig.getContents(new FileInputStream(signedFile));
602-
assertArrayEquals(contents, contents3);
605+
603606

604607
// inspiration:
605608
// http://stackoverflow.com/a/26702631/535646
@@ -729,11 +732,11 @@ void testSaveIncrementalAfterSign(boolean externallySign)
729732

730733
checkSignature(new File(SIMPLE_FORM_FILENAME), new File(OUT_DIR, fileNameSigned), false);
731734

732-
try (PDDocument doc = Loader.loadPDF(new File(OUT_DIR, fileNameSigned)))
735+
try (PDDocument doc = Loader.loadPDF(new File(OUT_DIR, fileNameSigned));
736+
FileOutputStream fileOutputStream = new FileOutputStream(new File(OUT_DIR, fileNameResaved1)))
733737
{
734738
oldImage = new PDFRenderer(doc).renderImage(0);
735-
736-
FileOutputStream fileOutputStream = new FileOutputStream(new File(OUT_DIR, fileNameResaved1));
739+
737740
PDField field = doc.getDocumentCatalog().getAcroForm().getField("SampleField");
738741
field.setValue("New Value 1");
739742

@@ -1126,10 +1129,7 @@ private void checkLTV(File outFile)
11261129

11271130
private byte[] signEncrypted(SecureRandom secureRandom, Date signingTime) throws IOException, GeneralSecurityException
11281131
{
1129-
KeyStore keystore = KeyStore.getInstance("PKCS12");
1130-
keystore.load(new FileInputStream(KEYSTORE_PATH), PASSWORD.toCharArray());
1131-
1132-
CreateSignature signing = new CreateSignature(keystore, PASSWORD.toCharArray());
1132+
CreateSignature signing = new CreateSignature(keyStore, PASSWORD.toCharArray());
11331133
signing.setExternalSigning(true);
11341134

11351135
File inFile = new File(IN_DIR + "sign_me_protected.pdf");

0 commit comments

Comments
 (0)