You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2018/02/09 21:26:18 UTC

svn commit: r1823703 - /pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java

Author: msahyoun
Date: Fri Feb  9 21:26:18 2018
New Revision: 1823703

URL: http://svn.apache.org/viewvc?rev=1823703&view=rev
Log:
PDFBOX-4071: use try-with

Modified:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java?rev=1823703&r1=1823702&r2=1823703&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java Fri Feb  9 21:26:18 2018
@@ -33,7 +33,6 @@ import org.apache.pdfbox.pdmodel.PDDocum
 import org.apache.pdfbox.rendering.PDFRenderer;
 import org.apache.pdfbox.rendering.TestPDFToImage;
 import org.junit.Before;
-import org.junit.Test;
 
 /**
  * Test flatten different forms and compare with rendering.
@@ -221,12 +220,13 @@ public class PDAcroFormFlattenTest {
 		File inputFile = new File(IN_DIR, targetFileName);
         File outputFile = new File(OUT_DIR, targetFileName);
 		
-        PDDocument testPdf = PDDocument.load(inputFile);
-        testPdf.getDocumentCatalog().getAcroForm().flatten();
-        testPdf.setAllSecurityToBeRemoved(true);
-        assertTrue(testPdf.getDocumentCatalog().getAcroForm().getFields().isEmpty());
-        testPdf.save(outputFile);
-        testPdf.close();
+		try (PDDocument testPdf = PDDocument.load(inputFile))
+		{
+			testPdf.getDocumentCatalog().getAcroForm().flatten();
+			testPdf.setAllSecurityToBeRemoved(true);
+			assertTrue(testPdf.getDocumentCatalog().getAcroForm().getFields().isEmpty());
+			testPdf.save(outputFile);
+		}
 
         // compare rendering
         TestPDFToImage testPDFToImage = new TestPDFToImage(TestPDFToImage.class.getName());
@@ -256,19 +256,20 @@ public class PDAcroFormFlattenTest {
 		getFromUrl(sourceUrl, targetFile);
 		
 		File file = new File(IN_DIR,targetFile);
-		PDDocument document = PDDocument.load(file, (String)null);
-        String outputPrefix = IN_DIR.getAbsolutePath() + '/' + file.getName() + "-";
-        int numPages = document.getNumberOfPages();
-		
-        PDFRenderer renderer = new PDFRenderer(document);
-        for (int i = 0; i < numPages; i++)
-        {
-            String fileName = outputPrefix + (i + 1) + ".png";
-            BufferedImage image = renderer.renderImageWithDPI(i, 96); // Windows native DPI
-            ImageIO.write(image, "PNG", new File(fileName));
-        }
-        
-        document.close();
+
+		try (PDDocument document = PDDocument.load(file, (String)null))
+		{
+			String outputPrefix = IN_DIR.getAbsolutePath() + '/' + file.getName() + "-";
+			int numPages = document.getNumberOfPages();
+			
+			PDFRenderer renderer = new PDFRenderer(document);
+			for (int i = 0; i < numPages; i++)
+			{
+				String fileName = outputPrefix + (i + 1) + ".png";
+				BufferedImage image = renderer.renderImageWithDPI(i, 96); // Windows native DPI
+				ImageIO.write(image, "PNG", new File(fileName));
+			}
+		}
 	}
 	
 	/*
@@ -277,18 +278,18 @@ public class PDAcroFormFlattenTest {
 	private static void getFromUrl(String sourceUrl, String targetFile) throws IOException
 	{
 		URL url = new URL(sourceUrl);
-		InputStream is = url.openStream();
-		OutputStream os = new FileOutputStream(new File(IN_DIR,targetFile));
+		
+		try (InputStream is = url.openStream();
+			OutputStream os = new FileOutputStream(new File(IN_DIR,targetFile)))
+		{
 
-		byte[] b = new byte[2048];
-		int length;
+			byte[] b = new byte[2048];
+			int length;
 
-		while ((length = is.read(b)) != -1) {
-			os.write(b, 0, length);
+			while ((length = is.read(b)) != -1) {
+				os.write(b, 0, length);
+			}
 		}
-
-		is.close();
-		os.close();
 	}
 
 	/*