Remove some findbugs warnings about missing close of streams, use existing IOUtils.copy() to copy from one stream to another
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1666683 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5d4a5c2bb4
commit
7bf9b90fb2
@ -22,6 +22,7 @@ import java.io.FileInputStream;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,8 +50,8 @@ public class POIFSDump {
|
|||||||
|
|
||||||
|
|
||||||
public static void dump(DirectoryEntry root, File parent) throws IOException {
|
public static void dump(DirectoryEntry root, File parent) throws IOException {
|
||||||
for(Iterator it = root.getEntries(); it.hasNext();){
|
for(Iterator<Entry> it = root.getEntries(); it.hasNext();){
|
||||||
Entry entry = (Entry)it.next();
|
Entry entry = it.next();
|
||||||
if(entry instanceof DocumentNode){
|
if(entry instanceof DocumentNode){
|
||||||
DocumentNode node = (DocumentNode)entry;
|
DocumentNode node = (DocumentNode)entry;
|
||||||
DocumentInputStream is = new DocumentInputStream(node);
|
DocumentInputStream is = new DocumentInputStream(node);
|
||||||
@ -58,9 +59,12 @@ public class POIFSDump {
|
|||||||
is.read(bytes);
|
is.read(bytes);
|
||||||
is.close();
|
is.close();
|
||||||
|
|
||||||
FileOutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
|
OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
|
||||||
out.write(bytes);
|
try {
|
||||||
out.close();
|
out.write(bytes);
|
||||||
|
} finally {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
} else if (entry instanceof DirectoryEntry){
|
} else if (entry instanceof DirectoryEntry){
|
||||||
DirectoryEntry dir = (DirectoryEntry)entry;
|
DirectoryEntry dir = (DirectoryEntry)entry;
|
||||||
File file = new File(parent, entry.getName());
|
File file = new File(parent, entry.getName());
|
||||||
|
@ -1409,8 +1409,11 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
|
|||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
throw new IOException(e.getLocalizedMessage());
|
throw new IOException(e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
this.save(fos);
|
try {
|
||||||
fos.close();
|
this.save(fos);
|
||||||
|
} finally {
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,6 +33,7 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
@ -242,12 +243,15 @@ public class AgileEncryptor extends Encryptor {
|
|||||||
LittleEndian.putLong(buf, 0, oleStreamSize);
|
LittleEndian.putLong(buf, 0, oleStreamSize);
|
||||||
integrityMD.update(buf, 0, LittleEndian.LONG_SIZE);
|
integrityMD.update(buf, 0, LittleEndian.LONG_SIZE);
|
||||||
|
|
||||||
FileInputStream fis = new FileInputStream(tmpFile);
|
InputStream fis = new FileInputStream(tmpFile);
|
||||||
int readBytes;
|
try {
|
||||||
while ((readBytes = fis.read(buf)) != -1) {
|
int readBytes;
|
||||||
integrityMD.update(buf, 0, readBytes);
|
while ((readBytes = fis.read(buf)) != -1) {
|
||||||
|
integrityMD.update(buf, 0, readBytes);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
fis.close();
|
||||||
}
|
}
|
||||||
fis.close();
|
|
||||||
|
|
||||||
byte hmacValue[] = integrityMD.doFinal();
|
byte hmacValue[] = integrityMD.doFinal();
|
||||||
|
|
||||||
|
@ -38,7 +38,11 @@ public class PresetGeometries extends LinkedHashMap<String, CustomGeometry> {
|
|||||||
try {
|
try {
|
||||||
InputStream is =
|
InputStream is =
|
||||||
XMLSlideShow.class.getResourceAsStream("presetShapeDefinitions.xml");
|
XMLSlideShow.class.getResourceAsStream("presetShapeDefinitions.xml");
|
||||||
read(is);
|
try {
|
||||||
|
read(is);
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -19,13 +19,12 @@ package org.apache.poi.xssf.dev;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
|
import org.apache.poi.util.IOUtils;
|
||||||
import org.apache.xmlbeans.XmlObject;
|
import org.apache.xmlbeans.XmlObject;
|
||||||
import org.apache.xmlbeans.XmlOptions;
|
import org.apache.xmlbeans.XmlOptions;
|
||||||
|
|
||||||
@ -40,7 +39,11 @@ public final class XSSFDump {
|
|||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
System.out.println("Dumping " + args[i]);
|
System.out.println("Dumping " + args[i]);
|
||||||
ZipFile zip = new ZipFile(args[i]);
|
ZipFile zip = new ZipFile(args[i]);
|
||||||
dump(zip);
|
try {
|
||||||
|
dump(zip);
|
||||||
|
} finally {
|
||||||
|
zip.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,6 +52,7 @@ public final class XSSFDump {
|
|||||||
int sep = zipname.lastIndexOf('.');
|
int sep = zipname.lastIndexOf('.');
|
||||||
File root = new File(zipname.substring(0, sep));
|
File root = new File(zipname.substring(0, sep));
|
||||||
root.mkdir();
|
root.mkdir();
|
||||||
|
System.out.println("Dupming to directory " + root);
|
||||||
|
|
||||||
Enumeration<? extends ZipEntry> en = zip.entries();
|
Enumeration<? extends ZipEntry> en = zip.entries();
|
||||||
while(en.hasMoreElements()){
|
while(en.hasMoreElements()){
|
||||||
@ -61,30 +65,24 @@ public final class XSSFDump {
|
|||||||
}
|
}
|
||||||
|
|
||||||
File f = new File(root, entry.getName());
|
File f = new File(root, entry.getName());
|
||||||
FileOutputStream out = new FileOutputStream(f);
|
OutputStream out = new FileOutputStream(f);
|
||||||
|
try {
|
||||||
if(entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml") || entry.getName().endsWith(".rels")){
|
if(entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml") || entry.getName().endsWith(".rels")){
|
||||||
try {
|
try {
|
||||||
XmlObject xml = XmlObject.Factory.parse(zip.getInputStream(entry));
|
XmlObject xml = XmlObject.Factory.parse(zip.getInputStream(entry));
|
||||||
XmlOptions options = new XmlOptions();
|
XmlOptions options = new XmlOptions();
|
||||||
options.setSavePrettyPrint();
|
options.setSavePrettyPrint();
|
||||||
xml.save(out, options);
|
xml.save(out, options);
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
System.err.println("Failed to parse " + entry.getName() + ", dumping raw content");
|
System.err.println("Failed to parse " + entry.getName() + ", dumping raw content");
|
||||||
dump(zip.getInputStream(entry), out);
|
IOUtils.copy(zip.getInputStream(entry), out);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
IOUtils.copy(zip.getInputStream(entry), out);
|
||||||
}
|
}
|
||||||
} else {
|
} finally {
|
||||||
dump(zip.getInputStream(entry), out);
|
out.close();
|
||||||
}
|
}
|
||||||
out.close();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void dump(InputStream is, OutputStream out) throws IOException{
|
|
||||||
int pos;
|
|
||||||
byte[] chunk = new byte[2048];
|
|
||||||
while((pos = is.read(chunk)) > 0) out.write(chunk, 0, pos);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import java.io.FileInputStream;
|
|||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import org.apache.poi.hmef.Attachment;
|
import org.apache.poi.hmef.Attachment;
|
||||||
import org.apache.poi.hmef.HMEFMessage;
|
import org.apache.poi.hmef.HMEFMessage;
|
||||||
@ -70,13 +71,14 @@ public final class HMEFContentsExtractor {
|
|||||||
* Extracts the RTF message body to the supplied file
|
* Extracts the RTF message body to the supplied file
|
||||||
*/
|
*/
|
||||||
public void extractMessageBody(File dest) throws IOException {
|
public void extractMessageBody(File dest) throws IOException {
|
||||||
FileOutputStream fout = new FileOutputStream(dest);
|
OutputStream fout = new FileOutputStream(dest);
|
||||||
|
try {
|
||||||
MAPIRtfAttribute body = (MAPIRtfAttribute)
|
MAPIRtfAttribute body = (MAPIRtfAttribute)
|
||||||
message.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
|
message.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
|
||||||
fout.write(body.getData());
|
fout.write(body.getData());
|
||||||
|
} finally {
|
||||||
fout.close();
|
fout.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -101,9 +103,12 @@ public final class HMEFContentsExtractor {
|
|||||||
|
|
||||||
// Save it
|
// Save it
|
||||||
File file = new File(dir, filename);
|
File file = new File(dir, filename);
|
||||||
FileOutputStream fout = new FileOutputStream(file);
|
OutputStream fout = new FileOutputStream(file);
|
||||||
fout.write( att.getContents() );
|
try {
|
||||||
fout.close();
|
fout.write( att.getContents() );
|
||||||
|
} finally {
|
||||||
|
fout.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user