Browse Source

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
tags/REL_3_12_FINAL
Dominik Stadler 9 years ago
parent
commit
2f8c89683f

+ 9
- 5
src/java/org/apache/poi/poifs/dev/POIFSDump.java View File

@@ -22,6 +22,7 @@ import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Iterator;

/**
@@ -49,8 +50,8 @@ public class POIFSDump {


public static void dump(DirectoryEntry root, File parent) throws IOException {
for(Iterator it = root.getEntries(); it.hasNext();){
Entry entry = (Entry)it.next();
for(Iterator<Entry> it = root.getEntries(); it.hasNext();){
Entry entry = it.next();
if(entry instanceof DocumentNode){
DocumentNode node = (DocumentNode)entry;
DocumentInputStream is = new DocumentInputStream(node);
@@ -58,9 +59,12 @@ public class POIFSDump {
is.read(bytes);
is.close();

FileOutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
out.write(bytes);
out.close();
OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
try {
out.write(bytes);
} finally {
out.close();
}
} else if (entry instanceof DirectoryEntry){
DirectoryEntry dir = (DirectoryEntry)entry;
File file = new File(parent, entry.getName());

+ 5
- 2
src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java View File

@@ -1409,8 +1409,11 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
} catch (FileNotFoundException e) {
throw new IOException(e.getLocalizedMessage());
}
this.save(fos);
fos.close();
try {
this.save(fos);
} finally {
fos.close();
}
}

/**

+ 9
- 5
src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java View File

@@ -33,6 +33,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
@@ -242,12 +243,15 @@ public class AgileEncryptor extends Encryptor {
LittleEndian.putLong(buf, 0, oleStreamSize);
integrityMD.update(buf, 0, LittleEndian.LONG_SIZE);
FileInputStream fis = new FileInputStream(tmpFile);
int readBytes;
while ((readBytes = fis.read(buf)) != -1) {
integrityMD.update(buf, 0, readBytes);
InputStream fis = new FileInputStream(tmpFile);
try {
int readBytes;
while ((readBytes = fis.read(buf)) != -1) {
integrityMD.update(buf, 0, readBytes);
}
} finally {
fis.close();
}
fis.close();
byte hmacValue[] = integrityMD.doFinal();

+ 5
- 1
src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java View File

@@ -38,7 +38,11 @@ public class PresetGeometries extends LinkedHashMap<String, CustomGeometry> {
try {
InputStream is =
XMLSlideShow.class.getResourceAsStream("presetShapeDefinitions.xml");
read(is);
try {
read(is);
} finally {
is.close();
}
} catch (Exception e){
throw new RuntimeException(e);
}

+ 23
- 25
src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java View File

@@ -19,13 +19,12 @@ package org.apache.poi.xssf.dev;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.poi.util.IOUtils;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;

@@ -40,7 +39,11 @@ public final class XSSFDump {
for (int i = 0; i < args.length; i++) {
System.out.println("Dumping " + 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('.');
File root = new File(zipname.substring(0, sep));
root.mkdir();
System.out.println("Dupming to directory " + root);

Enumeration<? extends ZipEntry> en = zip.entries();
while(en.hasMoreElements()){
@@ -61,30 +65,24 @@ public final class XSSFDump {
}

File f = new File(root, entry.getName());
FileOutputStream out = new FileOutputStream(f);

if(entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml") || entry.getName().endsWith(".rels")){
try {
XmlObject xml = XmlObject.Factory.parse(zip.getInputStream(entry));
XmlOptions options = new XmlOptions();
options.setSavePrettyPrint();
xml.save(out, options);
} catch (Exception e){
System.err.println("Failed to parse " + entry.getName() + ", dumping raw content");
dump(zip.getInputStream(entry), out);
OutputStream out = new FileOutputStream(f);
try {
if(entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml") || entry.getName().endsWith(".rels")){
try {
XmlObject xml = XmlObject.Factory.parse(zip.getInputStream(entry));
XmlOptions options = new XmlOptions();
options.setSavePrettyPrint();
xml.save(out, options);
} catch (Exception e){
System.err.println("Failed to parse " + entry.getName() + ", dumping raw content");
IOUtils.copy(zip.getInputStream(entry), out);
}
} else {
IOUtils.copy(zip.getInputStream(entry), out);
}
} else {
dump(zip.getInputStream(entry), out);
} finally {
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);

}
}

+ 15
- 10
src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java View File

@@ -22,6 +22,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hmef.Attachment;
import org.apache.poi.hmef.HMEFMessage;
@@ -70,13 +71,14 @@ public final class HMEFContentsExtractor {
* Extracts the RTF message body to the supplied file
*/
public void extractMessageBody(File dest) throws IOException {
FileOutputStream fout = new FileOutputStream(dest);
MAPIRtfAttribute body = (MAPIRtfAttribute)
message.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
fout.write(body.getData());
fout.close();
OutputStream fout = new FileOutputStream(dest);
try {
MAPIRtfAttribute body = (MAPIRtfAttribute)
message.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
fout.write(body.getData());
} finally {
fout.close();
}
}
/**
@@ -101,9 +103,12 @@ public final class HMEFContentsExtractor {
// Save it
File file = new File(dir, filename);
FileOutputStream fout = new FileOutputStream(file);
fout.write( att.getContents() );
fout.close();
OutputStream fout = new FileOutputStream(file);
try {
fout.write( att.getContents() );
} finally {
fout.close();
}
}
}
}

Loading…
Cancel
Save