* Add missing close() on streams in ImageUtils.getImageDimension()

* Add some unit tests for CellReference

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1650070 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2015-01-07 14:36:39 +00:00
parent f912ecac92
commit 8997d8900d
3 changed files with 53 additions and 19 deletions

View File

@ -71,25 +71,30 @@ public class ImageUtils {
try { try {
//read the image using javax.imageio.* //read the image using javax.imageio.*
ImageInputStream iis = ImageIO.createImageInputStream( is ); ImageInputStream iis = ImageIO.createImageInputStream( is );
Iterator<ImageReader> i = ImageIO.getImageReaders( iis ); try {
ImageReader r = i.next(); Iterator<ImageReader> i = ImageIO.getImageReaders( iis );
r.setInput( iis ); ImageReader r = i.next();
BufferedImage img = r.read(0); try {
r.setInput( iis );
BufferedImage img = r.read(0);
int[] dpi = getResolution(r);
//if DPI is zero then assume standard 96 DPI
//since cannot divide by zero
if (dpi[0] == 0) dpi[0] = PIXEL_DPI;
if (dpi[1] == 0) dpi[1] = PIXEL_DPI;
size.width = img.getWidth()*PIXEL_DPI/dpi[0];
size.height = img.getHeight()*PIXEL_DPI/dpi[1];
} finally {
r.dispose();
}
} finally {
iis.close();
}
int[] dpi = getResolution(r); } catch (IOException e) {
//if DPI is zero then assume standard 96 DPI
//since cannot divide by zero
if (dpi[0] == 0) dpi[0] = PIXEL_DPI;
if (dpi[1] == 0) dpi[1] = PIXEL_DPI;
size.width = img.getWidth()*PIXEL_DPI/dpi[0];
size.height = img.getHeight()*PIXEL_DPI/dpi[1];
r.dispose();
iis.close();
} catch (IOException e){
//silently return if ImageIO failed to read the image //silently return if ImageIO failed to read the image
logger.log(POILogger.WARN, e); logger.log(POILogger.WARN, e);
} }

View File

@ -50,7 +50,6 @@ import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.XLSBUnsupportedException; import org.apache.poi.xssf.XLSBUnsupportedException;
import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFITestDataProvider;

View File

@ -218,4 +218,34 @@ public final class TestCellReference extends TestCase {
throw new AssertionFailedError("expected (c='" + colStr + "', r='" + rowStr + "' to be " throw new AssertionFailedError("expected (c='" + colStr + "', r='" + rowStr + "' to be "
+ (expResult ? "within" : "out of") + " bounds for version " + sv.name()); + (expResult ? "within" : "out of") + " bounds for version " + sv.name());
} }
public void testConvertColStringToIndex() {
assertEquals(0, CellReference.convertColStringToIndex("A"));
assertEquals(1, CellReference.convertColStringToIndex("B"));
assertEquals(14, CellReference.convertColStringToIndex("O"));
assertEquals(701, CellReference.convertColStringToIndex("ZZ"));
assertEquals(18252, CellReference.convertColStringToIndex("ZZA"));
assertEquals(0, CellReference.convertColStringToIndex("$A"));
assertEquals(1, CellReference.convertColStringToIndex("$B"));
try {
CellReference.convertColStringToIndex("A$");
fail("Should throw exception here");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("A$"));
}
}
public void testConvertNumColColString() {
assertEquals("A", CellReference.convertNumToColString(0));
assertEquals("AV", CellReference.convertNumToColString(47));
assertEquals("AW", CellReference.convertNumToColString(48));
assertEquals("BF", CellReference.convertNumToColString(57));
assertEquals("", CellReference.convertNumToColString(-1));
assertEquals("", CellReference.convertNumToColString(Integer.MIN_VALUE));
assertEquals("", CellReference.convertNumToColString(Integer.MAX_VALUE));
assertEquals("FXSHRXW", CellReference.convertNumToColString(Integer.MAX_VALUE-1));
}
} }