Ensure streams are closed always, reformat code somewhat
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1776647 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6f616d0a51
commit
36406ad3a1
@ -90,6 +90,20 @@ public class OldExcelExtractor implements Closeable {
|
||||
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")
|
||||
|
@ -57,9 +57,9 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
|
||||
private final File fileOut;
|
||||
private final DirectoryNode dir;
|
||||
|
||||
private long pos = 0;
|
||||
private long totalPos = 0;
|
||||
private long written = 0;
|
||||
private long pos;
|
||||
private long totalPos;
|
||||
private long written;
|
||||
|
||||
// the cipher can't be final, because for the last chunk we change the padding
|
||||
// and therefore need to change the cipher too
|
||||
@ -281,8 +281,11 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
|
||||
os.write(buf);
|
||||
|
||||
FileInputStream fis = new FileInputStream(fileOut);
|
||||
IOUtils.copy(fis, os);
|
||||
fis.close();
|
||||
try {
|
||||
IOUtils.copy(fis, os);
|
||||
} finally {
|
||||
fis.close();
|
||||
}
|
||||
|
||||
os.close();
|
||||
|
||||
|
@ -119,8 +119,7 @@ public class StandardEncryptor extends Encryptor implements Cloneable {
|
||||
throws IOException, GeneralSecurityException {
|
||||
createEncryptionInfoEntry(dir);
|
||||
DataSpaceMapUtils.addDefaultDataSpace(dir);
|
||||
OutputStream countStream = new StandardCipherOutputStream(dir);
|
||||
return countStream;
|
||||
return new StandardCipherOutputStream(dir);
|
||||
}
|
||||
|
||||
protected class StandardCipherOutputStream extends FilterOutputStream implements POIFSWriterListener {
|
||||
@ -188,8 +187,11 @@ public class StandardEncryptor extends Encryptor implements Cloneable {
|
||||
leos.writeLong(countBytes);
|
||||
|
||||
FileInputStream fis = new FileInputStream(fileOut);
|
||||
IOUtils.copy(fis, leos);
|
||||
fis.close();
|
||||
try {
|
||||
IOUtils.copy(fis, leos);
|
||||
} finally {
|
||||
fis.close();
|
||||
}
|
||||
if (!fileOut.delete()) {
|
||||
logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut);
|
||||
}
|
||||
|
@ -24,15 +24,13 @@ import java.awt.GraphicsEnvironment;
|
||||
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();
|
||||
@ -68,13 +66,10 @@ public class FontMetricsDumper
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,7 @@ import java.util.ArrayList;
|
||||
* 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.
|
||||
@ -35,16 +34,12 @@ public class HexRead
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
@ -59,16 +54,12 @@ public class HexRead
|
||||
* @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;
|
||||
@ -87,18 +78,15 @@ public class HexRead
|
||||
}
|
||||
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")
|
||||
@ -110,8 +98,7 @@ public class HexRead
|
||||
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' ) {
|
||||
@ -131,8 +118,7 @@ public class HexRead
|
||||
b <<= 4;
|
||||
b += (byte) digitValue;
|
||||
characterCount++;
|
||||
if ( characterCount == 2 )
|
||||
{
|
||||
if ( characterCount == 2 ) {
|
||||
bytes.add( Byte.valueOf( b ) );
|
||||
characterCount = 0;
|
||||
b = (byte) 0;
|
||||
@ -141,8 +127,7 @@ public class HexRead
|
||||
}
|
||||
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;
|
||||
@ -156,11 +141,9 @@ public class HexRead
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -95,8 +95,11 @@ public class VsdxToPng {
|
||||
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,
|
||||
|
@ -235,6 +235,14 @@ public final class TestOldExcelExtractor {
|
||||
} 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
|
||||
|
Loading…
Reference in New Issue
Block a user