use try with resources in examples

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1816187 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2017-11-23 19:28:36 +00:00
parent 625c511503
commit 61cad2865e
6 changed files with 99 additions and 177 deletions

View File

@ -45,20 +45,19 @@ import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
/**
* <p>This class copies a POI file system to a new file and compares the copy
* with the original.</p>
*
* <p>
* <p>Property set streams are copied logically, i.e. the application
* establishes a {@link org.apache.poi.hpsf.PropertySet} of an original property
* set, creates a {@link org.apache.poi.hpsf.PropertySet} and writes the
* {@link org.apache.poi.hpsf.PropertySet} to the destination POI file
* system. - Streams which are no property set streams are copied bit by
* bit.</p>
*
* <p>
* <p>The comparison of the POI file systems is done logically. That means that
* the two disk files containing the POI file systems do not need to be
* exactly identical. However, both POI file systems must contain the same
@ -67,38 +66,36 @@ import org.apache.poi.util.TempFile;
* with the same attributes, and the sections must contain the same properties.
* Details like the ordering of the properties do not matter.</p>
*/
public class CopyCompare
{
public class CopyCompare {
/**
* <p>Runs the example program. The application expects one or two
* arguments:</p>
*
* <p>
* <ol>
*
* <p>
* <li><p>The first argument is the disk file name of the POI filesystem to
* copy.</p></li>
*
* <p>
* <li><p>The second argument is optional. If it is given, it is the name of
* a disk file the copy of the POI filesystem will be written to. If it is
* not given, the copy will be written to a temporary file which will be
* deleted at the end of the program.</p></li>
*
* <p>
* </ol>
*
* @param args Command-line arguments.
* @exception MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @exception NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @exception IOException if any I/O exception occurs.
* @exception UnsupportedEncodingException if a character encoding is not
* supported.
* @throws MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @throws NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @throws IOException if any I/O exception occurs.
* @throws UnsupportedEncodingException if a character encoding is not
* supported.
*/
public static void main(final String[] args)
throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException
{
throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException {
String originalFileName = null;
String copyFileName = null;
@ -113,7 +110,7 @@ public class CopyCompare
copyFileName = args[1];
} else {
System.err.println("Usage: " + CopyCompare.class.getName() +
"originPOIFS [copyPOIFS]");
"originPOIFS [copyPOIFS]");
System.exit(1);
}
@ -132,51 +129,43 @@ public class CopyCompare
/* Read all documents from the original POI file system and compare them
* with the equivalent document from the copy. */
POIFSFileSystem opfs = null, cpfs = null;
try {
opfs = new POIFSFileSystem(new File(originalFileName));
cpfs = new POIFSFileSystem(new File(copyFileName));
try (POIFSFileSystem opfs = new POIFSFileSystem(new File(originalFileName));
POIFSFileSystem cpfs = new POIFSFileSystem(new File(copyFileName))) {
final DirectoryEntry oRoot = opfs.getRoot();
final DirectoryEntry cRoot = cpfs.getRoot();
final StringBuffer messages = new StringBuffer();
if (equal(oRoot, cRoot, messages)) {
System.out.println("Equal");
} else {
System.out.println("Not equal: " + messages);
System.out.println("Not equal: " + messages);
}
} finally {
IOUtils.closeQuietly(cpfs);
IOUtils.closeQuietly(opfs);
}
}
/**
* <p>Compares two {@link DirectoryEntry} instances of a POI file system.
* The directories must contain the same streams with the same names and
* contents.</p>
*
* @param d1 The first directory.
* @param d2 The second directory.
* @param d1 The first directory.
* @param d2 The second directory.
* @param msg The method may append human-readable comparison messages to
* this string buffer.
* this string buffer.
* @return <code>true</code> if the directories are equal, else
* <code>false</code>.
* @exception MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @exception NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @exception IOException if any I/O exception occurs.
* @throws MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @throws NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @throws IOException if any I/O exception occurs.
*/
private static boolean equal(final DirectoryEntry d1,
final DirectoryEntry d2,
final StringBuffer msg)
throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException
{
throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException {
boolean equal = true;
/* Iterate over d1 and compare each entry with its counterpart in d2. */
for (final Entry e1 : d1) {
@ -215,30 +204,28 @@ public class CopyCompare
}
/**
* <p>Compares two {@link DocumentEntry} instances of a POI file system.
* Documents that are not property set streams must be bitwise identical.
* Property set streams must be logically equal.</p>
*
* @param d1 The first document.
* @param d2 The second document.
* @param d1 The first document.
* @param d2 The second document.
* @param msg The method may append human-readable comparison messages to
* this string buffer.
* this string buffer.
* @return <code>true</code> if the documents are equal, else
* <code>false</code>.
* @exception MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @exception NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @exception IOException if any I/O exception occurs.
* @throws MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @throws NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @throws IOException if any I/O exception occurs.
*/
private static boolean equal(final DocumentEntry d1, final DocumentEntry d2,
final StringBuffer msg)
throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException
{
throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException {
try (DocumentInputStream dis1 = new DocumentInputStream(d1); DocumentInputStream dis2 = new DocumentInputStream(d2)) {
if (PropertySet.isPropertySetStream(dis1) &&
PropertySet.isPropertySetStream(dis2)) {
@ -264,7 +251,6 @@ public class CopyCompare
}
/**
* <p>This class does all the work. Its method {@link
* #processPOIFSReaderEvent(POIFSReaderEvent)} is called for each file in
@ -284,9 +270,9 @@ public class CopyCompare
* <p>The constructor of a {@link CopyFile} instance creates the target
* POIFS. It also stores the name of the file the POIFS will be written
* to once it is complete.</p>
*
*
* @param dstName The name of the disk file the destination POIFS is to
* be written to.
* be written to.
*/
public CopyFile(final String dstName) {
this.dstName = dstName;
@ -345,41 +331,39 @@ public class CopyCompare
}
/**
* <p>Writes a {@link PropertySet} to a POI filesystem.</p>
*
* @param poiFs The POI filesystem to write to.
* @param path The file's path in the POI filesystem.
* @param name The file's name in the POI filesystem.
* @param ps The property set to write.
* @param path The file's path in the POI filesystem.
* @param name The file's name in the POI filesystem.
* @param ps The property set to write.
*/
public void copy(final POIFSFileSystem poiFs,
final POIFSDocumentPath path,
final String name,
final PropertySet ps)
throws WritingNotSupportedException, IOException {
throws WritingNotSupportedException, IOException {
final DirectoryEntry de = getPath(poiFs, path);
final PropertySet mps = new PropertySet(ps);
de.createDocument(name, mps.toInputStream());
}
/**
* <p>Copies the bytes from a {@link DocumentInputStream} to a new
* stream in a POI filesystem.</p>
*
* @param poiFs The POI filesystem to write to.
* @param path The source document's path.
* @param name The source document's name.
* @param poiFs The POI filesystem to write to.
* @param path The source document's path.
* @param name The source document's name.
* @param stream The stream containing the source document.
*/
public void copy(final POIFSFileSystem poiFs,
final POIFSDocumentPath path,
final String name,
final DocumentInputStream stream)
throws IOException {
throws IOException {
// create the directories to the document
final DirectoryEntry de = getPath(poiFs, path);
// check the parameters after the directories have been created
@ -395,7 +379,7 @@ public class CopyCompare
stream.close();
out.close();
final InputStream in =
new ByteArrayInputStream(out.toByteArray());
new ByteArrayInputStream(out.toByteArray());
de.createDocument(name, in);
}
@ -410,12 +394,12 @@ public class CopyCompare
}
/** Contains the directory paths that have already been created in the
/**
* Contains the directory paths that have already been created in the
* output POI filesystem and maps them to their corresponding
* {@link org.apache.poi.poifs.filesystem.DirectoryNode}s. */
private final Map<String,DirectoryEntry> paths = new HashMap<>();
* {@link org.apache.poi.poifs.filesystem.DirectoryNode}s.
*/
private final Map<String, DirectoryEntry> paths = new HashMap<>();
/**
@ -424,7 +408,7 @@ public class CopyCompare
* a POI filesystem its directory must be created first. This method
* creates all directories between the POI filesystem root and the
* directory the document should belong to which do not yet exist.</p>
*
* <p>
* <p>Unfortunately POI does not offer a simple method to interrogate
* the POIFS whether a certain child node (file or directory) exists in
* a directory. However, since we always start with an empty POIFS which
@ -435,9 +419,9 @@ public class CopyCompare
* to the corresponding {@link DirectoryEntry} instances.</p>
*
* @param poiFs The POI filesystem the directory hierarchy is created
* in, if needed.
* @param path The document's path. This method creates those directory
* components of this hierarchy which do not yet exist.
* in, if needed.
* @param path The document's path. This method creates those directory
* components of this hierarchy which do not yet exist.
* @return The directory entry of the document path's parent. The caller
* should use this {@link DirectoryEntry} to create documents in it.
*/
@ -464,7 +448,7 @@ public class CopyCompare
de = getPath(poiFs, path.getParent());
/* Now create the target directory: */
de = de.createDirectory(path.getComponent
(path.length() - 1));
(path.length() - 1));
}
paths.put(s, de);
return de;

View File

@ -250,18 +250,14 @@ public class ToHtml {
ensureOut();
// First, copy the base css
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("excelStyle.css")));
try (BufferedReader in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("excelStyle.css")))){
String line;
while ((line = in.readLine()) != null) {
out.format("%s%n", line);
}
} catch (IOException e) {
throw new IllegalStateException("Reading standard css", e);
} finally {
IOUtils.closeQuietly(in);
}
// now add css for each used style

View File

@ -26,7 +26,6 @@ import org.apache.poi.crypt.examples.EncryptionUtils;
import org.apache.poi.examples.util.TempFileUtils;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
@ -47,38 +46,24 @@ public class LoadPasswordProtectedXlsxStreaming {
TempFileUtils.checkTempFiles();
String filename = args[0];
String password = args[1];
FileInputStream fis = new FileInputStream(filename);
try {
InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password);
try {
printSheetCount(unencryptedStream);
} finally {
IOUtils.closeQuietly(unencryptedStream);
}
} finally {
IOUtils.closeQuietly(fis);
try (FileInputStream fis = new FileInputStream(filename);
InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password)) {
printSheetCount(unencryptedStream);
}
TempFileUtils.checkTempFiles();
}
public static void printSheetCount(final InputStream inputStream) throws Exception {
AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
try {
OPCPackage pkg = OPCPackage.open(source);
try {
XSSFReader reader = new XSSFReader(pkg);
SheetIterator iter = (SheetIterator)reader.getSheetsData();
int count = 0;
while(iter.hasNext()) {
iter.next();
count++;
}
System.out.println("sheet count: " + count);
} finally {
IOUtils.closeQuietly(pkg);
try (AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
OPCPackage pkg = OPCPackage.open(source)) {
XSSFReader reader = new XSSFReader(pkg);
SheetIterator iter = (SheetIterator)reader.getSheetsData();
int count = 0;
while(iter.hasNext()) {
iter.next();
count++;
}
} finally {
IOUtils.closeQuietly(source);
System.out.println("sheet count: " + count);
}
}
}

View File

@ -84,22 +84,16 @@ public class SavePasswordProtectedXlsx {
public static void save(final InputStream inputStream, final String filename, final String pwd)
throws InvalidFormatException, IOException, GeneralSecurityException {
POIFSFileSystem fs = null;
FileOutputStream fos = null;
OPCPackage opc = null;
try {
fs = new POIFSFileSystem();
try (POIFSFileSystem fs = new POIFSFileSystem();
OPCPackage opc = OPCPackage.open(inputStream);
FileOutputStream fos = new FileOutputStream(filename)) {
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
Encryptor enc = Encryptor.getInstance(info);
enc.confirmPassword(pwd);
opc = OPCPackage.open(inputStream);
fos = new FileOutputStream(filename);
opc.save(enc.getDataStream(fs));
fs.writeFilesystem(fos);
} finally {
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(opc);
IOUtils.closeQuietly(fs);
IOUtils.closeQuietly(inputStream);
}
}

View File

@ -26,7 +26,6 @@ import org.apache.poi.crypt.examples.EncryptionUtils;
import org.apache.poi.examples.util.TempFileUtils;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
@ -45,36 +44,18 @@ public class LoadPasswordProtectedXlsx {
TempFileUtils.checkTempFiles();
String filename = args[0];
String password = args[1];
FileInputStream fis = new FileInputStream(filename);
try {
InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password);
try {
printSheetCount(unencryptedStream);
} finally {
IOUtils.closeQuietly(unencryptedStream);
}
} finally {
IOUtils.closeQuietly(fis);
try (FileInputStream fis = new FileInputStream(filename);
InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password)) {
printSheetCount(unencryptedStream);
}
TempFileUtils.checkTempFiles();
}
public static void printSheetCount(final InputStream inputStream) throws Exception {
AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
try {
OPCPackage pkg = OPCPackage.open(source);
try {
XSSFWorkbook workbook = new XSSFWorkbook(pkg);
try {
System.out.println("sheet count: " + workbook.getNumberOfSheets());
} finally {
IOUtils.closeQuietly(workbook);
}
} finally {
IOUtils.closeQuietly(pkg);
}
} finally {
IOUtils.closeQuietly(source);
try (AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
OPCPackage pkg = OPCPackage.open(source);
XSSFWorkbook workbook = new XSSFWorkbook(pkg)) {
System.out.println("sheet count: " + workbook.getNumberOfSheets());
}
}

View File

@ -35,7 +35,6 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
/**
@ -73,17 +72,12 @@ public class UpdateEmbeddedDoc {
*/
public UpdateEmbeddedDoc(String filename) throws FileNotFoundException, IOException {
this.docFile = new File(filename);
FileInputStream fis = null;
if (!this.docFile.exists()) {
throw new FileNotFoundException("The Word document " + filename + " does not exist.");
}
try {
// Open the Word document file and instantiate the XWPFDocument
// class.
fis = new FileInputStream(this.docFile);
try (FileInputStream fis = new FileInputStream(this.docFile)) {
// Open the Word document file and instantiate the XWPFDocument class.
this.doc = new XWPFDocument(fis);
} finally {
IOUtils.closeQuietly(fis);
}
}
@ -113,30 +107,23 @@ public class UpdateEmbeddedDoc {
// to the create method of the WorkbookFactory class. Update
// the resulting Workbook and then stream that out again
// using an OutputStream obtained from the same PackagePart.
InputStream is = pPart.getInputStream();
Workbook workbook = null;
OutputStream os = null;
try {
workbook = WorkbookFactory.create(is);
try (InputStream is = pPart.getInputStream();
Workbook workbook = WorkbookFactory.create(is);
OutputStream os = pPart.getOutputStream()) {
Sheet sheet = workbook.getSheetAt(SHEET_NUM);
Row row = sheet.getRow(ROW_NUM);
Cell cell = row.getCell(CELL_NUM);
cell.setCellValue(NEW_VALUE);
os = pPart.getOutputStream();
workbook.write(os);
} finally {
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(workbook);
IOUtils.closeQuietly(is);
}
}
}
if (!embeddedDocs.isEmpty()) {
// Finally, write the newly modified Word document out to file.
FileOutputStream fos = new FileOutputStream(this.docFile);
this.doc.write(fos);
fos.close();
try (FileOutputStream fos = new FileOutputStream(this.docFile)) {
this.doc.write(fos);
}
}
}
@ -169,19 +156,14 @@ public class UpdateEmbeddedDoc {
for (PackagePart pPart : this.doc.getAllEmbedds()) {
String ext = pPart.getPartName().getExtension();
if (BINARY_EXTENSION.equals(ext) || OPENXML_EXTENSION.equals(ext)) {
InputStream is = pPart.getInputStream();
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(is);
try (InputStream is = pPart.getInputStream();
Workbook workbook = WorkbookFactory.create(is)) {
Sheet sheet = workbook.getSheetAt(SHEET_NUM);
Row row = sheet.getRow(ROW_NUM);
Cell cell = row.getCell(CELL_NUM);
if(cell.getNumericCellValue() != NEW_VALUE) {
throw new IllegalStateException("Failed to validate document content.");
}
} finally {
IOUtils.closeQuietly(workbook);
IOUtils.closeQuietly(is);
}
}
}