mirror of https://github.com/apache/poi.git
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:
parent
625c511503
commit
61cad2865e
|
@ -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
|
||||
* @throws MarkUnsupportedException if a POI document stream does not
|
||||
* support the mark() operation.
|
||||
* @exception NoPropertySetStreamException if the application tries to
|
||||
* @throws 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
|
||||
* @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
|
||||
{
|
||||
UnsupportedEncodingException, IOException {
|
||||
String originalFileName = null;
|
||||
String copyFileName = null;
|
||||
|
||||
|
@ -132,11 +129,8 @@ 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();
|
||||
|
@ -145,14 +139,10 @@ public class CopyCompare
|
|||
} else {
|
||||
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
|
||||
|
@ -164,19 +154,18 @@ public class CopyCompare
|
|||
* 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
|
||||
* @throws MarkUnsupportedException if a POI document stream does not
|
||||
* support the mark() operation.
|
||||
* @exception NoPropertySetStreamException if the application tries to
|
||||
* @throws 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 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
|
||||
{
|
||||
UnsupportedEncodingException, IOException {
|
||||
boolean equal = true;
|
||||
/* Iterate over d1 and compare each entry with its counterpart in d2. */
|
||||
for (final Entry e1 : d1) {
|
||||
|
@ -215,7 +204,6 @@ 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.
|
||||
|
@ -227,18 +215,17 @@ public class CopyCompare
|
|||
* 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
|
||||
* @throws MarkUnsupportedException if a POI document stream does not
|
||||
* support the mark() operation.
|
||||
* @exception NoPropertySetStreamException if the application tries to
|
||||
* @throws 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 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
|
||||
{
|
||||
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
|
||||
|
@ -345,7 +331,6 @@ public class CopyCompare
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>Writes a {@link PropertySet} to a POI filesystem.</p>
|
||||
*
|
||||
|
@ -365,7 +350,6 @@ public class CopyCompare
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>Copies the bytes from a {@link DocumentInputStream} to a new
|
||||
* stream in a POI filesystem.</p>
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,25 +46,16 @@ 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 {
|
||||
try (FileInputStream fis = new FileInputStream(filename);
|
||||
InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password)) {
|
||||
printSheetCount(unencryptedStream);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(unencryptedStream);
|
||||
}
|
||||
} finally {
|
||||
IOUtils.closeQuietly(fis);
|
||||
}
|
||||
TempFileUtils.checkTempFiles();
|
||||
}
|
||||
|
||||
public static void printSheetCount(final InputStream inputStream) throws Exception {
|
||||
AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
|
||||
try {
|
||||
OPCPackage pkg = OPCPackage.open(source);
|
||||
try {
|
||||
try (AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
|
||||
OPCPackage pkg = OPCPackage.open(source)) {
|
||||
XSSFReader reader = new XSSFReader(pkg);
|
||||
SheetIterator iter = (SheetIterator)reader.getSheetsData();
|
||||
int count = 0;
|
||||
|
@ -74,11 +64,6 @@ public class LoadPasswordProtectedXlsxStreaming {
|
|||
count++;
|
||||
}
|
||||
System.out.println("sheet count: " + count);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(pkg);
|
||||
}
|
||||
} finally {
|
||||
IOUtils.closeQuietly(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
try (FileInputStream fis = new FileInputStream(filename);
|
||||
InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password)) {
|
||||
printSheetCount(unencryptedStream);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(unencryptedStream);
|
||||
}
|
||||
} finally {
|
||||
IOUtils.closeQuietly(fis);
|
||||
}
|
||||
TempFileUtils.checkTempFiles();
|
||||
}
|
||||
|
||||
public static void printSheetCount(final InputStream inputStream) throws Exception {
|
||||
AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
|
||||
try {
|
||||
try (AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
|
||||
OPCPackage pkg = OPCPackage.open(source);
|
||||
try {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook(pkg);
|
||||
try {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook(pkg)) {
|
||||
System.out.println("sheet count: " + workbook.getNumberOfSheets());
|
||||
} finally {
|
||||
IOUtils.closeQuietly(workbook);
|
||||
}
|
||||
} finally {
|
||||
IOUtils.closeQuietly(pkg);
|
||||
}
|
||||
} finally {
|
||||
IOUtils.closeQuietly(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
try (FileOutputStream fos = new FileOutputStream(this.docFile)) {
|
||||
this.doc.write(fos);
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue