Fix some IntelliJ warnings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1809349 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-09-22 20:07:56 +00:00
parent 6e9afd88a2
commit 4ac6800974
2 changed files with 42 additions and 70 deletions

View File

@ -47,7 +47,7 @@ public final class IOUtils {
* the hard-coded maximum record lengths if they are willing to accept the risk * the hard-coded maximum record lengths if they are willing to accept the risk
* of an OutOfMemoryException. * of an OutOfMemoryException.
* *
* @param maxOverride * @param maxOverride The number of bytes that should be possible to be allocated in one step.
* @since 4.0.0 * @since 4.0.0
*/ */
public static void setByteArrayMaxOverride(int maxOverride) { public static void setByteArrayMaxOverride(int maxOverride) {

View File

@ -247,12 +247,9 @@ public final class TestPackage {
// Save and re-load // Save and re-load
pkg.close(); pkg.close();
File tmp = TempFile.createTempFile("testCreatePackageWithCoreDocument", ".zip"); File tmp = TempFile.createTempFile("testCreatePackageWithCoreDocument", ".zip");
OutputStream fout = new FileOutputStream(tmp); try (OutputStream fout = new FileOutputStream(tmp)) {
try { fout.write(baos.toByteArray());
fout.write(baos.toByteArray()); }
} finally {
fout.close();
}
pkg = OPCPackage.open(tmp.getPath()); pkg = OPCPackage.open(tmp.getPath());
//tmp.delete(); //tmp.delete();
@ -368,12 +365,9 @@ public final class TestPackage {
@SuppressWarnings("resource") @SuppressWarnings("resource")
OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE);
try { try {
FileOutputStream fout = new FileOutputStream(targetFile); try (FileOutputStream fout = new FileOutputStream(targetFile)) {
try { p.save(fout);
p.save(fout); }
} finally {
fout.close();
}
// Compare the original and newly saved document // Compare the original and newly saved document
assertTrue(targetFile.exists()); assertTrue(targetFile.exists());
@ -630,34 +624,31 @@ public final class TestPackage {
@Test @Test
public void getPartSize() throws IOException, InvalidFormatException { public void getPartSize() throws IOException, InvalidFormatException {
String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ); try (OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ)) {
try { int checked = 0;
int checked = 0; for (PackagePart part : pkg.getParts()) {
for (PackagePart part : pkg.getParts()) { // Can get the size of zip parts
// Can get the size of zip parts if (part.getPartName().getName().equals("/word/document.xml")) {
if (part.getPartName().getName().equals("/word/document.xml")) { checked++;
checked++; assertEquals(ZipPackagePart.class, part.getClass());
assertEquals(ZipPackagePart.class, part.getClass()); assertEquals(6031L, part.getSize());
assertEquals(6031L, part.getSize()); }
} if (part.getPartName().getName().equals("/word/fontTable.xml")) {
if (part.getPartName().getName().equals("/word/fontTable.xml")) { checked++;
checked++; assertEquals(ZipPackagePart.class, part.getClass());
assertEquals(ZipPackagePart.class, part.getClass()); assertEquals(1312L, part.getSize());
assertEquals(1312L, part.getSize()); }
}
// But not from the others
// But not from the others if (part.getPartName().getName().equals("/docProps/core.xml")) {
if (part.getPartName().getName().equals("/docProps/core.xml")) { checked++;
checked++; assertEquals(PackagePropertiesPart.class, part.getClass());
assertEquals(PackagePropertiesPart.class, part.getClass()); assertEquals(-1, part.getSize());
assertEquals(-1, part.getSize()); }
} }
} // Ensure we actually found the parts we want to check
// Ensure we actually found the parts we want to check assertEquals(3, checked);
assertEquals(3, checked); }
} finally {
pkg.close();
}
} }
@Test @Test
@ -695,11 +686,8 @@ public final class TestPackage {
// OLE2 - Stream // OLE2 - Stream
try { try {
InputStream stream = files.openResourceAsStream("SampleSS.xls"); try (InputStream stream = files.openResourceAsStream("SampleSS.xls")) {
try {
OPCPackage.open(stream); OPCPackage.open(stream);
} finally {
stream.close();
} }
fail("Shouldn't be able to open OLE2"); fail("Shouldn't be able to open OLE2");
} catch (OLE2NotOfficeXmlFileException e) { } catch (OLE2NotOfficeXmlFileException e) {
@ -717,11 +705,8 @@ public final class TestPackage {
// Raw XML - Stream // Raw XML - Stream
try { try {
InputStream stream = files.openResourceAsStream("SampleSS.xml"); try (InputStream stream = files.openResourceAsStream("SampleSS.xml")) {
try {
OPCPackage.open(stream); OPCPackage.open(stream);
} finally {
stream.close();
} }
fail("Shouldn't be able to open XML"); fail("Shouldn't be able to open XML");
} catch (NotOfficeXmlFileException e) { } catch (NotOfficeXmlFileException e) {
@ -739,11 +724,8 @@ public final class TestPackage {
// ODF / ODS - Stream // ODF / ODS - Stream
try { try {
InputStream stream = files.openResourceAsStream("SampleSS.ods"); try (InputStream stream = files.openResourceAsStream("SampleSS.ods")) {
try {
OPCPackage.open(stream); OPCPackage.open(stream);
} finally {
stream.close();
} }
fail("Shouldn't be able to open ODS"); fail("Shouldn't be able to open ODS");
} catch (ODFNotOfficeXmlFileException e) { } catch (ODFNotOfficeXmlFileException e) {
@ -761,11 +743,8 @@ public final class TestPackage {
// Plain Text - Stream // Plain Text - Stream
try { try {
InputStream stream = files.openResourceAsStream("SampleSS.txt"); try (InputStream stream = files.openResourceAsStream("SampleSS.txt")) {
try {
OPCPackage.open(stream); OPCPackage.open(stream);
} finally {
stream.close();
} }
fail("Shouldn't be able to open Plain Text"); fail("Shouldn't be able to open Plain Text");
} catch (NotOfficeXmlFileException e) { } catch (NotOfficeXmlFileException e) {
@ -850,12 +829,9 @@ public final class TestPackage {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook(file); Workbook wb = XSSFTestDataSamples.openSampleWorkbook(file);
wb.close(); wb.close();
POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx")); try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"))) {
try {
assertNotNull(extractor); assertNotNull(extractor);
extractor.getText(); extractor.getText();
} finally {
extractor.close();
} }
fail("Should catch an exception because of a ZipBomb"); fail("Should catch an exception because of a ZipBomb");
@ -901,23 +877,19 @@ public final class TestPackage {
// this is a bit strange, as there will be different exceptions thrown // this is a bit strange, as there will be different exceptions thrown
// depending if this executed via "ant test" or within eclipse // depending if this executed via "ant test" or within eclipse
// maybe a difference in JDK ... // maybe a difference in JDK ...
} catch (InvalidFormatException e) { } catch (InvalidFormatException | POIXMLException e) {
checkForZipBombException(e);
} catch (POIXMLException e) {
checkForZipBombException(e); checkForZipBombException(e);
} }
// check max entry size ouf of bounds // check max entry size ouf of bounds
ZipSecureFile.setMinInflateRatio(min_ratio-0.002); ZipSecureFile.setMinInflateRatio(min_ratio-0.002);
ZipSecureFile.setMaxEntrySize(max_size-1); ZipSecureFile.setMaxEntrySize(max_size-1);
try { try {
WorkbookFactory.create(file, null, true).close(); WorkbookFactory.create(file, null, true).close();
} catch (InvalidFormatException e) { } catch (InvalidFormatException | POIXMLException e) {
checkForZipBombException(e);
} catch (POIXMLException e) {
checkForZipBombException(e); checkForZipBombException(e);
} }
} finally { } finally {
// reset otherwise a lot of ooxml tests will fail // reset otherwise a lot of ooxml tests will fail
ZipSecureFile.setMinInflateRatio(0.01d); ZipSecureFile.setMinInflateRatio(0.01d);
ZipSecureFile.setMaxEntrySize(0xFFFFFFFFL); ZipSecureFile.setMaxEntrySize(0xFFFFFFFFL);