From 0614835c55f44ab6f3e9b0850ca51e0e53a65a49 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Fri, 14 May 2021 00:37:50 +0000 Subject: #65304 - Add commons-io as a dependency git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1889871 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/stress/HPSFFileHandler.java | 6 +++--- .../org/apache/poi/stress/HSSFFileHandler.java | 2 +- .../org/apache/poi/stress/POIFSFileHandler.java | 16 ++++++++-------- .../org/apache/poi/stress/SlideShowHandler.java | 21 ++++----------------- .../org/apache/poi/stress/SpreadsheetHandler.java | 18 ++++++------------ .../org/apache/poi/stress/XSSFBFileHandler.java | 5 +---- .../org/apache/poi/stress/XSSFFileHandler.java | 22 +++++++++------------- 7 files changed, 32 insertions(+), 58 deletions(-) (limited to 'poi-integration/src') diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HPSFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HPSFFileHandler.java index 088f77ef45..53ca398b5c 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HPSFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HPSFFileHandler.java @@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeFalse; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -33,6 +32,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; +import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.examples.hpsf.CopyCompare; import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument; @@ -104,11 +104,11 @@ class HPSFFileHandler extends POIFSFileHandler { public void handleAdditional(File file) throws Exception { assumeFalse(EXCLUDES_HANDLE_ADD.contains(file.getParentFile().getName()+"/"+file.getName())); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(); PrintStream psNew = new PrintStream(bos, true, "ISO-8859-1"); CopyCompare.setOut(psNew); CopyCompare.main(new String[]{file.getAbsolutePath(), copyOutput.get().getAbsolutePath()}); - assertEquals("Equal" + NL, bos.toString(StandardCharsets.UTF_8.name())); + assertEquals("Equal" + NL, bos.toString(StandardCharsets.UTF_8)); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HSSFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HSSFFileHandler.java index 1e0348cac5..f8e0de4ff1 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HSSFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HSSFFileHandler.java @@ -33,7 +33,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.util.NullPrintStream; +import org.apache.commons.io.output.NullPrintStream; import org.junit.jupiter.api.Test; class HSSFFileHandler extends SpreadsheetHandler { diff --git a/poi-integration/src/test/java/org/apache/poi/stress/POIFSFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/POIFSFileHandler.java index b92f91f5f9..d492979992 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/POIFSFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/POIFSFileHandler.java @@ -18,13 +18,12 @@ package org.apache.poi.stress; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.POIDocument; import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -58,13 +57,14 @@ class POIFSFileHandler extends AbstractFileHandler { } protected void handlePOIDocument(POIDocument doc) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - doc.write(out); + try (UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream()) { + doc.write(out); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - POIFSFileSystem fs = new POIFSFileSystem(in); - handlePOIFSFileSystem(fs); - fs.close(); + try (InputStream in = out.toInputStream(); + POIFSFileSystem fs = new POIFSFileSystem(in)) { + handlePOIFSFileSystem(fs); + } + } } // a test-case to test this locally without executing the full TestAllFiles diff --git a/poi-integration/src/test/java/org/apache/poi/stress/SlideShowHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/SlideShowHandler.java index 9a1defbf21..ec291e1c0b 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/SlideShowHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/SlideShowHandler.java @@ -23,11 +23,10 @@ import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.lang.ref.WeakReference; +import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.sl.draw.Drawable; import org.apache.poi.sl.usermodel.GroupShape; import org.apache.poi.sl.usermodel.Notes; @@ -40,7 +39,6 @@ import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.sl.usermodel.TextParagraph; import org.apache.poi.sl.usermodel.TextRun; import org.apache.poi.sl.usermodel.TextShape; -import org.junit.jupiter.api.Assumptions; import org.junit.platform.commons.util.ExceptionUtils; public abstract class SlideShowHandler extends POIFSFileHandler { @@ -51,29 +49,18 @@ public abstract class SlideShowHandler extends POIFSFileHandler { readPictures(ss); // write out the file - ByteArrayOutputStream out = writeToArray(ss); + UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream(); + ss.write(out); readContent(ss); // read in the written file - try (SlideShow read = SlideShowFactory.create(new ByteArrayInputStream(out.toByteArray()))) { + try (SlideShow read = SlideShowFactory.create(out.toInputStream())) { assertNotNull(read); readContent(read); } } - private ByteArrayOutputStream writeToArray(SlideShow ss) throws IOException { - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - try { - ss.write(stream); - } finally { - stream.close(); - } - - return stream; - } - - private void readContent(SlideShow ss) { for (Slide s : ss.getSlides()) { s.getTitle(); diff --git a/poi-integration/src/test/java/org/apache/poi/stress/SpreadsheetHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/SpreadsheetHandler.java index dcee603bf2..341902b4ea 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/SpreadsheetHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/SpreadsheetHandler.java @@ -18,10 +18,9 @@ package org.apache.poi.stress; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; +import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.ss.extractor.EmbeddedData; import org.apache.poi.ss.extractor.EmbeddedExtractor; import org.apache.poi.ss.usermodel.Cell; @@ -45,10 +44,10 @@ public abstract class SpreadsheetHandler extends AbstractFileHandler { readContent(wb); // write once more - ByteArrayOutputStream out = writeToArray(wb); + UnsynchronizedByteArrayOutputStream out = writeToArray(wb); // read in the written file - Workbook read = WorkbookFactory.create(new ByteArrayInputStream(out.toByteArray())); + Workbook read = WorkbookFactory.create(out.toInputStream()); assertNotNull(read); @@ -61,14 +60,9 @@ public abstract class SpreadsheetHandler extends AbstractFileHandler { read.close(); } - private ByteArrayOutputStream writeToArray(Workbook wb) throws IOException { - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - try { - wb.write(stream); - } finally { - stream.close(); - } - + private UnsynchronizedByteArrayOutputStream writeToArray(Workbook wb) throws IOException { + UnsynchronizedByteArrayOutputStream stream = new UnsynchronizedByteArrayOutputStream(); + wb.write(stream); return stream; } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/XSSFBFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/XSSFBFileHandler.java index 1bc23c49ed..dd25e4148d 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/XSSFBFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/XSSFBFileHandler.java @@ -17,7 +17,6 @@ package org.apache.poi.stress; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -40,10 +39,8 @@ class XSSFBFileHandler extends AbstractFileHandler { @Override public void handleFile(InputStream stream, String path) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - IOUtils.copy(stream, out); + byte[] bytes = IOUtils.toByteArray(stream); - final byte[] bytes = out.toByteArray(); try (OPCPackage opcPackage = OPCPackage.open(new ByteArrayInputStream(bytes))) { testOne(opcPackage); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java index beb3a8edf8..6f9571564f 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java @@ -16,14 +16,13 @@ ==================================================================== */ package org.apache.poi.stress; +import static org.apache.commons.io.output.NullOutputStream.NULL_OUTPUT_STREAM; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeFalse; import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -35,6 +34,8 @@ import java.util.Set; import javax.xml.transform.TransformerException; +import org.apache.commons.io.output.NullPrintStream; +import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.examples.ss.ExcelComparator; import org.apache.poi.examples.xssf.eventusermodel.FromHowTo; @@ -50,7 +51,6 @@ import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.util.IOUtils; -import org.apache.poi.util.NullPrintStream; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.extractor.XSSFExportToXml; import org.apache.poi.xssf.usermodel.XSSFMap; @@ -70,12 +70,12 @@ class XSSFFileHandler extends SpreadsheetHandler { // make sure the potentially large byte-array is freed up quickly again { - ByteArrayOutputStream out = new ByteArrayOutputStream(); + + UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream(); IOUtils.copy(stream, out); - ByteArrayInputStream bytes = new ByteArrayInputStream(out.toByteArray()); if (pass != null) { - POIFSFileSystem poifs = new POIFSFileSystem(bytes); + POIFSFileSystem poifs = new POIFSFileSystem(out.toInputStream()); EncryptionInfo ei = new EncryptionInfo(poifs); Decryptor dec = ei.getDecryptor(); try { @@ -91,11 +91,9 @@ class XSSFFileHandler extends SpreadsheetHandler { IOUtils.copy(is, out); is.close(); poifs.close(); - bytes = new ByteArrayInputStream(out.toByteArray()); } - checkXSSFReader(OPCPackage.open(bytes)); - bytes.reset(); - wb = new XSSFWorkbook(bytes); + checkXSSFReader(OPCPackage.open(out.toInputStream())); + wb = new XSSFWorkbook(out.toInputStream()); } // use the combined handler for HSSF/XSSF @@ -157,9 +155,7 @@ class XSSFFileHandler extends SpreadsheetHandler { TransformerException { for (XSSFMap map : wb.getCustomXMLMappings()) { XSSFExportToXml exporter = new XSSFExportToXml(map); - - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); + exporter.exportToXML(NULL_OUTPUT_STREAM, true); } } -- cgit v1.2.3