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) {
|
if (poifs != null) {
|
||||||
poifs.close();
|
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")
|
@SuppressWarnings("resource")
|
||||||
|
@ -57,9 +57,9 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
|
|||||||
private final File fileOut;
|
private final File fileOut;
|
||||||
private final DirectoryNode dir;
|
private final DirectoryNode dir;
|
||||||
|
|
||||||
private long pos = 0;
|
private long pos;
|
||||||
private long totalPos = 0;
|
private long totalPos;
|
||||||
private long written = 0;
|
private long written;
|
||||||
|
|
||||||
// the cipher can't be final, because for the last chunk we change the padding
|
// the cipher can't be final, because for the last chunk we change the padding
|
||||||
// and therefore need to change the cipher too
|
// and therefore need to change the cipher too
|
||||||
@ -281,8 +281,11 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
|
|||||||
os.write(buf);
|
os.write(buf);
|
||||||
|
|
||||||
FileInputStream fis = new FileInputStream(fileOut);
|
FileInputStream fis = new FileInputStream(fileOut);
|
||||||
|
try {
|
||||||
IOUtils.copy(fis, os);
|
IOUtils.copy(fis, os);
|
||||||
|
} finally {
|
||||||
fis.close();
|
fis.close();
|
||||||
|
}
|
||||||
|
|
||||||
os.close();
|
os.close();
|
||||||
|
|
||||||
|
@ -119,8 +119,7 @@ public class StandardEncryptor extends Encryptor implements Cloneable {
|
|||||||
throws IOException, GeneralSecurityException {
|
throws IOException, GeneralSecurityException {
|
||||||
createEncryptionInfoEntry(dir);
|
createEncryptionInfoEntry(dir);
|
||||||
DataSpaceMapUtils.addDefaultDataSpace(dir);
|
DataSpaceMapUtils.addDefaultDataSpace(dir);
|
||||||
OutputStream countStream = new StandardCipherOutputStream(dir);
|
return new StandardCipherOutputStream(dir);
|
||||||
return countStream;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class StandardCipherOutputStream extends FilterOutputStream implements POIFSWriterListener {
|
protected class StandardCipherOutputStream extends FilterOutputStream implements POIFSWriterListener {
|
||||||
@ -188,8 +187,11 @@ public class StandardEncryptor extends Encryptor implements Cloneable {
|
|||||||
leos.writeLong(countBytes);
|
leos.writeLong(countBytes);
|
||||||
|
|
||||||
FileInputStream fis = new FileInputStream(fileOut);
|
FileInputStream fis = new FileInputStream(fileOut);
|
||||||
|
try {
|
||||||
IOUtils.copy(fis, leos);
|
IOUtils.copy(fis, leos);
|
||||||
|
} finally {
|
||||||
fis.close();
|
fis.close();
|
||||||
|
}
|
||||||
if (!fileOut.delete()) {
|
if (!fileOut.delete()) {
|
||||||
logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut);
|
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.awt.Toolkit;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public class FontMetricsDumper
|
public class FontMetricsDumper {
|
||||||
{
|
|
||||||
@SuppressForbidden("command line tool")
|
@SuppressForbidden("command line tool")
|
||||||
public static void main( String[] args ) throws IOException
|
public static void main(String[] args) throws IOException {
|
||||||
{
|
|
||||||
|
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
|
|
||||||
Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
|
Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
|
||||||
@ -68,13 +66,10 @@ public class FontMetricsDumper
|
|||||||
props.setProperty("font." + fontName + ".widths", widths.toString());
|
props.setProperty("font." + fontName + ".widths", widths.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("font_metrics.properties");
|
OutputStream fileOut = new FileOutputStream("font_metrics.properties");
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
props.store(fileOut, "Font Metrics");
|
props.store(fileOut, "Font Metrics");
|
||||||
}
|
} finally {
|
||||||
finally
|
|
||||||
{
|
|
||||||
fileOut.close();
|
fileOut.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,7 @@ import java.util.ArrayList;
|
|||||||
* Utilities to read hex from files.
|
* Utilities to read hex from files.
|
||||||
* TODO - move to test packages
|
* TODO - move to test packages
|
||||||
*/
|
*/
|
||||||
public class HexRead
|
public class HexRead {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* This method reads hex data from a filename and returns a byte array.
|
* 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.
|
* 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.
|
* @return The bytes read from the file.
|
||||||
* @throws IOException If there was a problem while reading 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 );
|
File file = new File( filename );
|
||||||
FileInputStream stream = new FileInputStream( file );
|
InputStream stream = new FileInputStream( file );
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
return readData( stream, -1 );
|
return readData( stream, -1 );
|
||||||
}
|
} finally {
|
||||||
finally
|
|
||||||
{
|
|
||||||
stream.close();
|
stream.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,16 +54,12 @@ public class HexRead
|
|||||||
* @see #readData(String)
|
* @see #readData(String)
|
||||||
*/
|
*/
|
||||||
public static byte[] readData(InputStream stream, String section ) throws IOException {
|
public static byte[] readData(InputStream stream, String section ) throws IOException {
|
||||||
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
StringBuffer sectionText = new StringBuffer();
|
StringBuffer sectionText = new StringBuffer();
|
||||||
boolean inSection = false;
|
boolean inSection = false;
|
||||||
int c = stream.read();
|
int c = stream.read();
|
||||||
while ( c != -1 )
|
while ( c != -1 ) {
|
||||||
{
|
switch ( c ) {
|
||||||
switch ( c )
|
|
||||||
{
|
|
||||||
case '[':
|
case '[':
|
||||||
inSection = true;
|
inSection = true;
|
||||||
break;
|
break;
|
||||||
@ -87,18 +78,15 @@ public class HexRead
|
|||||||
}
|
}
|
||||||
c = stream.read();
|
c = stream.read();
|
||||||
}
|
}
|
||||||
}
|
} finally {
|
||||||
finally
|
|
||||||
{
|
|
||||||
stream.close();
|
stream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IOException( "Section '" + section + "' not found" );
|
throw new IOException( "Section '" + section + "' not found" );
|
||||||
}
|
}
|
||||||
public static byte[] readData( String filename, String section ) throws IOException
|
|
||||||
{
|
public static byte[] readData( String filename, String section ) throws IOException {
|
||||||
File file = new File( filename );
|
return readData(new FileInputStream( filename ), section);
|
||||||
FileInputStream stream = new FileInputStream( file );
|
|
||||||
return readData(stream, section);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("fallthrough")
|
@SuppressWarnings("fallthrough")
|
||||||
@ -110,8 +98,7 @@ public class HexRead
|
|||||||
List<Byte> bytes = new ArrayList<Byte>();
|
List<Byte> bytes = new ArrayList<Byte>();
|
||||||
final char a = 'a' - 10;
|
final char a = 'a' - 10;
|
||||||
final char A = 'A' - 10;
|
final char A = 'A' - 10;
|
||||||
while ( true )
|
while ( true ) {
|
||||||
{
|
|
||||||
int count = stream.read();
|
int count = stream.read();
|
||||||
int digitValue = -1;
|
int digitValue = -1;
|
||||||
if ( '0' <= count && count <= '9' ) {
|
if ( '0' <= count && count <= '9' ) {
|
||||||
@ -131,8 +118,7 @@ public class HexRead
|
|||||||
b <<= 4;
|
b <<= 4;
|
||||||
b += (byte) digitValue;
|
b += (byte) digitValue;
|
||||||
characterCount++;
|
characterCount++;
|
||||||
if ( characterCount == 2 )
|
if ( characterCount == 2 ) {
|
||||||
{
|
|
||||||
bytes.add( Byte.valueOf( b ) );
|
bytes.add( Byte.valueOf( b ) );
|
||||||
characterCount = 0;
|
characterCount = 0;
|
||||||
b = (byte) 0;
|
b = (byte) 0;
|
||||||
@ -141,8 +127,7 @@ public class HexRead
|
|||||||
}
|
}
|
||||||
Byte[] polished = bytes.toArray(new Byte[bytes.size()]);
|
Byte[] polished = bytes.toArray(new Byte[bytes.size()]);
|
||||||
byte[] rval = new byte[polished.length];
|
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();
|
rval[j] = polished[j].byteValue();
|
||||||
}
|
}
|
||||||
return rval;
|
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();
|
int c = stream.read();
|
||||||
while ( c != -1 && c != '\n' && c != '\r' )
|
while ( c != -1 && c != '\n' && c != '\r' ) {
|
||||||
{
|
|
||||||
c = stream.read();
|
c = stream.read();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,9 +95,12 @@ public class VsdxToPng {
|
|||||||
graphics.dispose();
|
graphics.dispose();
|
||||||
|
|
||||||
FileOutputStream out = new FileOutputStream(outFile);
|
FileOutputStream out = new FileOutputStream(outFile);
|
||||||
|
try {
|
||||||
ImageIO.write(img, "png", out);
|
ImageIO.write(img, "png", out);
|
||||||
|
} finally {
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void renderToPng(XmlVisioDocument document,
|
public static void renderToPng(XmlVisioDocument document,
|
||||||
String outDirname, double scale, ShapeRenderer renderer)
|
String outDirname, double scale, ShapeRenderer renderer)
|
||||||
|
@ -235,6 +235,14 @@ public final class TestOldExcelExtractor {
|
|||||||
} catch (RecordFormatException e) {
|
} catch (RecordFormatException e) {
|
||||||
// expected here
|
// 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
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user