mirror of https://github.com/apache/poi.git
use try-with-resources in more places
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1817238 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3c1ce22865
commit
49b7fd5342
|
@ -204,17 +204,16 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
|
|||
@Override
|
||||
public boolean canExtract(DirectoryNode dn) {
|
||||
ClassID clsId = dn.getStorageClsid();
|
||||
return (PdfClassID.equals(clsId)
|
||||
|| dn.hasEntry("CONTENTS"));
|
||||
return (PdfClassID.equals(clsId) || dn.hasEntry("CONTENTS"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public EmbeddedData extract(DirectoryNode dn) throws IOException {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
InputStream is = dn.createDocumentInputStream("CONTENTS");
|
||||
IOUtils.copy(is, bos);
|
||||
is.close();
|
||||
return new EmbeddedData(dn.getName() + ".pdf", bos.toByteArray(), CONTENT_TYPE_PDF);
|
||||
try(ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
InputStream is = dn.createDocumentInputStream("CONTENTS")) {
|
||||
IOUtils.copy(is, bos);
|
||||
return new EmbeddedData(dn.getName() + ".pdf", bos.toByteArray(), CONTENT_TYPE_PDF);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -74,12 +74,10 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor
|
|||
System.err.println(" XSSFExcelExtractor <filename.xlsx>");
|
||||
System.exit(1);
|
||||
}
|
||||
OPCPackage pkg = OPCPackage.create(args[0]);
|
||||
POIXMLTextExtractor extractor = new XSSFExcelExtractor(pkg);
|
||||
try {
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.create(args[0]);
|
||||
POIXMLTextExtractor extractor = new XSSFExcelExtractor(pkg)) {
|
||||
System.out.println(extractor.getText());
|
||||
} finally {
|
||||
extractor.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -517,10 +517,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||
public int addPicture(byte[] pictureData, int format) {
|
||||
int imageNumber = getAllPictures().size() + 1;
|
||||
XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true).getDocumentPart();
|
||||
try {
|
||||
OutputStream out = img.getPackagePart().getOutputStream();
|
||||
try (OutputStream out = img.getPackagePart().getOutputStream()) {
|
||||
out.write(pictureData);
|
||||
out.close();
|
||||
} catch (IOException e){
|
||||
throw new POIXMLException(e);
|
||||
}
|
||||
|
@ -1745,9 +1743,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook"));
|
||||
|
||||
PackagePart part = getPackagePart();
|
||||
OutputStream out = part.getOutputStream();
|
||||
workbook.save(out, xmlOptions);
|
||||
out.close();
|
||||
try (OutputStream out = part.getOutputStream()) {
|
||||
workbook.save(out, xmlOptions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue