if (poifs != null) {
poifs.close();
}
+ } catch (IOException e) {
+ // ensure streams are closed correctly
+ if (poifs != null) {
+ poifs.close();
+ }
+
+ throw e;
+ } catch (RuntimeException e) {
+ // ensure streams are closed correctly
+ if (poifs != null) {
+ poifs.close();
+ }
+
+ throw e;
}
-
+
@SuppressWarnings("resource")
FileInputStream biffStream = new FileInputStream(f); // NOSONAR
try {
private final File fileOut;\r
private final DirectoryNode dir;\r
\r
- private long pos = 0;\r
- private long totalPos = 0;\r
- private long written = 0;\r
+ private long pos;\r
+ private long totalPos;\r
+ private long written;\r
\r
// the cipher can't be final, because for the last chunk we change the padding\r
// and therefore need to change the cipher too\r
*\r
* @throws BadPaddingException \r
* @throws IllegalBlockSizeException \r
- * @throws ShortBufferException \r
+ * @throws ShortBufferException\r
*/\r
protected int invokeCipher(int posInChunk, boolean doFinal) throws GeneralSecurityException {\r
byte plain[] = (plainByteFlags.isEmpty()) ? null : chunk.clone();\r
os.write(buf);\r
\r
FileInputStream fis = new FileInputStream(fileOut);\r
- IOUtils.copy(fis, os);\r
- fis.close();\r
+ try {\r
+ IOUtils.copy(fis, os);\r
+ } finally {\r
+ fis.close();\r
+ }\r
\r
os.close();\r
\r
throws IOException, GeneralSecurityException {\r
createEncryptionInfoEntry(dir);\r
DataSpaceMapUtils.addDefaultDataSpace(dir);\r
- OutputStream countStream = new StandardCipherOutputStream(dir);\r
- return countStream;\r
+ return new StandardCipherOutputStream(dir);\r
}\r
\r
protected class StandardCipherOutputStream extends FilterOutputStream implements POIFSWriterListener {\r
leos.writeLong(countBytes);\r
\r
FileInputStream fis = new FileInputStream(fileOut);\r
- IOUtils.copy(fis, leos);\r
- fis.close();\r
+ try {\r
+ IOUtils.copy(fis, leos);\r
+ } finally {\r
+ fis.close();\r
+ }\r
if (!fileOut.delete()) {\r
logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut);\r
}\r
import java.awt.Toolkit;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.util.Properties;
@SuppressWarnings("deprecation")
-public class FontMetricsDumper
-{
+public class FontMetricsDumper {
@SuppressForbidden("command line tool")
- public static void main( String[] args ) throws IOException
- {
-
+ public static void main(String[] args) throws IOException {
Properties props = new Properties();
Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
props.setProperty("font." + fontName + ".widths", widths.toString());
}
- FileOutputStream fileOut = new FileOutputStream("font_metrics.properties");
- try
- {
+ OutputStream fileOut = new FileOutputStream("font_metrics.properties");
+ try {
props.store(fileOut, "Font Metrics");
- }
- finally
- {
+ } finally {
fileOut.close();
}
}
* Utilities to read hex from files.
* TODO - move to test packages
*/
-public class HexRead
-{
+public class HexRead {
/**
* This method reads hex data from a filename and returns a byte array.
* The file may contain line comments that are preceeded with a # symbol.
* @return The bytes read from the file.
* @throws IOException If there was a problem while reading the file.
*/
- public static byte[] readData( String filename ) throws IOException
- {
+ public static byte[] readData( String filename ) throws IOException {
File file = new File( filename );
- FileInputStream stream = new FileInputStream( file );
- try
- {
+ InputStream stream = new FileInputStream( file );
+ try {
return readData( stream, -1 );
- }
- finally
- {
+ } finally {
stream.close();
}
}
* @see #readData(String)
*/
public static byte[] readData(InputStream stream, String section ) throws IOException {
-
- try
- {
+ try {
StringBuffer sectionText = new StringBuffer();
boolean inSection = false;
int c = stream.read();
- while ( c != -1 )
- {
- switch ( c )
- {
+ while ( c != -1 ) {
+ switch ( c ) {
case '[':
inSection = true;
break;
}
c = stream.read();
}
- }
- finally
- {
+ } finally {
stream.close();
}
+
throw new IOException( "Section '" + section + "' not found" );
}
- public static byte[] readData( String filename, String section ) throws IOException
- {
- File file = new File( filename );
- FileInputStream stream = new FileInputStream( file );
- return readData(stream, section);
+
+ public static byte[] readData( String filename, String section ) throws IOException {
+ return readData(new FileInputStream( filename ), section);
}
@SuppressWarnings("fallthrough")
List<Byte> bytes = new ArrayList<Byte>();
final char a = 'a' - 10;
final char A = 'A' - 10;
- while ( true )
- {
+ while ( true ) {
int count = stream.read();
int digitValue = -1;
if ( '0' <= count && count <= '9' ) {
b <<= 4;
b += (byte) digitValue;
characterCount++;
- if ( characterCount == 2 )
- {
+ if ( characterCount == 2 ) {
bytes.add( Byte.valueOf( b ) );
characterCount = 0;
b = (byte) 0;
}
Byte[] polished = bytes.toArray(new Byte[bytes.size()]);
byte[] rval = new byte[polished.length];
- for ( int j = 0; j < polished.length; j++ )
- {
+ for ( int j = 0; j < polished.length; j++ ) {
rval[j] = polished[j].byteValue();
}
return rval;
}
}
- static private void readToEOL( InputStream stream ) throws IOException
- {
+ static private void readToEOL( InputStream stream ) throws IOException {
int c = stream.read();
- while ( c != -1 && c != '\n' && c != '\r' )
- {
+ while ( c != -1 && c != '\n' && c != '\r' ) {
c = stream.read();
}
}
graphics.dispose();
FileOutputStream out = new FileOutputStream(outFile);
- ImageIO.write(img, "png", out);
- out.close();
+ try {
+ ImageIO.write(img, "png", out);
+ } finally {
+ out.close();
+ }
}
public static void renderToPng(XmlVisioDocument document,
} catch (RecordFormatException e) {
// expected here
}
+
+ // a POIFS file which is not a Workbook
+ try {
+ new OldExcelExtractor(POIDataSamples.getDocumentInstance().getFile("47304.doc"));
+ fail("Should catch Exception here");
+ } catch (FileNotFoundException e) {
+ // expected here
+ }
}
@Test