aboutsummaryrefslogtreecommitdiffstats
path: root/src/scratchpad/testcases/org/apache/poi
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2018-04-02 17:16:01 +0000
committerDominik Stadler <centic@apache.org>2018-04-02 17:16:01 +0000
commit2444a5396d73d60042137efe0dc355250ce83da0 (patch)
tree98241213bd9e3d8ae78400815cf339f4e6b060ce /src/scratchpad/testcases/org/apache/poi
parentff1120d7ded7e373d3b15aefc3c1823b5812c9e8 (diff)
downloadpoi-2444a5396d73d60042137efe0dc355250ce83da0.tar.gz
poi-2444a5396d73d60042137efe0dc355250ce83da0.zip
Close resources in tests and in case of Exceptions and use try-with-resources. Close the socket-connection during encrpyting/decrypting as soon as it is not needed any more.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1828178 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad/testcases/org/apache/poi')
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestDataSamples.java71
1 files changed, 29 insertions, 42 deletions
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestDataSamples.java b/src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestDataSamples.java
index 9978447781..8f1e8c12c6 100644
--- a/src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestDataSamples.java
+++ b/src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestDataSamples.java
@@ -37,7 +37,12 @@ public class HWPFTestDataSamples {
public static HWPFDocument openSampleFile(String sampleFileName) {
try {
InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(sampleFileName);
- return new HWPFDocument(is);
+ try {
+ return new HWPFDocument(is);
+ } catch (Throwable e) {
+ is.close();
+ throw e;
+ }
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -48,42 +53,33 @@ public class HWPFTestDataSamples {
final long start = System.currentTimeMillis();
try
{
- ZipInputStream is = new ZipInputStream( POIDataSamples
+ try (ZipInputStream is = new ZipInputStream(POIDataSamples
.getDocumentInstance()
- .openResourceAsStream( sampleFileName ) );
- try
- {
+ .openResourceAsStream(sampleFileName))) {
is.getNextEntry();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try
- {
- IOUtils.copy( is, baos );
- }
- finally
- {
+ try {
+ IOUtils.copy(is, baos);
+ } finally {
baos.close();
}
final long endUnzip = System.currentTimeMillis();
byte[] byteArray = baos.toByteArray();
- logger.log( POILogger.DEBUG, "Unzipped in ",
- Long.valueOf( endUnzip - start ), " ms -- ",
- Long.valueOf( byteArray.length ), " byte(s)" );
+ logger.log(POILogger.DEBUG, "Unzipped in ",
+ Long.valueOf(endUnzip - start), " ms -- ",
+ Long.valueOf(byteArray.length), " byte(s)");
- ByteArrayInputStream bais = new ByteArrayInputStream( byteArray );
- HWPFDocument doc = new HWPFDocument( bais );
+ ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
+ HWPFDocument doc = new HWPFDocument(bais);
final long endParse = System.currentTimeMillis();
- logger.log( POILogger.DEBUG, "Parsed in ",
- Long.valueOf( endParse - start ), " ms" );
+ logger.log(POILogger.DEBUG, "Parsed in ",
+ Long.valueOf(endParse - start), " ms");
return doc;
}
- finally
- {
- is.close();
- }
}
catch ( IOException e )
{
@@ -105,39 +101,30 @@ public class HWPFTestDataSamples {
{
logger.log(POILogger.DEBUG, "Downloading ", sampleFileUrl, " ...");
- InputStream is = new URL( sampleFileUrl ).openStream();
- try
- {
+ try (InputStream is = new URL(sampleFileUrl).openStream()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try
- {
- IOUtils.copy( is, baos );
- }
- finally
- {
+ try {
+ IOUtils.copy(is, baos);
+ } finally {
baos.close();
}
final long endDownload = System.currentTimeMillis();
byte[] byteArray = baos.toByteArray();
- logger.log( POILogger.DEBUG, "Downloaded in ",
- Long.valueOf( endDownload - start ), " ms -- ",
- Long.valueOf( byteArray.length ), " byte(s)" );
+ logger.log(POILogger.DEBUG, "Downloaded in ",
+ Long.valueOf(endDownload - start), " ms -- ",
+ Long.valueOf(byteArray.length), " byte(s)");
- ByteArrayInputStream bais = new ByteArrayInputStream( byteArray );
- HWPFDocument doc = new HWPFDocument( bais );
+ ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
+ HWPFDocument doc = new HWPFDocument(bais);
final long endParse = System.currentTimeMillis();
- logger.log( POILogger.DEBUG, "Parsed in ",
- Long.valueOf( endParse - start ), " ms" );
+ logger.log(POILogger.DEBUG, "Parsed in ",
+ Long.valueOf(endParse - start), " ms");
return doc;
}
- finally
- {
- is.close();
- }
}
catch ( IOException e )
{