import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
+import java.io.OutputStream;
import java.util.Iterator;
/**
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);
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());
} catch (FileNotFoundException e) {
throw new IOException(e.getLocalizedMessage());
}
- this.save(fos);
- fos.close();
+ try {
+ this.save(fos);
+ } finally {
+ fos.close();
+ }
}
/**
import java.io.File;\r
import java.io.FileInputStream;\r
import java.io.IOException;\r
+import java.io.InputStream;\r
import java.io.OutputStream;\r
import java.security.GeneralSecurityException;\r
import java.security.MessageDigest;\r
LittleEndian.putLong(buf, 0, oleStreamSize);\r
integrityMD.update(buf, 0, LittleEndian.LONG_SIZE);\r
\r
- FileInputStream fis = new FileInputStream(tmpFile);\r
- int readBytes;\r
- while ((readBytes = fis.read(buf)) != -1) {\r
- integrityMD.update(buf, 0, readBytes);\r
+ InputStream fis = new FileInputStream(tmpFile);\r
+ try {\r
+ int readBytes;\r
+ while ((readBytes = fis.read(buf)) != -1) {\r
+ integrityMD.update(buf, 0, readBytes);\r
+ }\r
+ } finally {\r
+ fis.close();\r
}\r
- fis.close();\r
\r
byte hmacValue[] = integrityMD.doFinal();\r
\r
try {
InputStream is =
XMLSlideShow.class.getResourceAsStream("presetShapeDefinitions.xml");
- read(is);
+ try {
+ read(is);
+ } finally {
+ is.close();
+ }
} catch (Exception e){
throw new RuntimeException(e);
}
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;
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();
+ }
}
}
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()){
}
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);
-
- }
}
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;
* 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();
+ }
}
/**
// 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();
+ }
}
}
}