Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Close ImageInputStream when failing & debug logging for #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Kahle committed Jun 4, 2018
1 parent 1ce1871 commit 4a0835b
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions src/main/java/eu/transkribus/interfaces/types/util/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static BufferedImage convertToBufferedImage(URL u) throws IOException {
BufferedImage b = read(u);
logger.debug("read buffered image from url: "+b);

if (b == null) {
if (b == null && u.getProtocol().startsWith("http")) {
//ImageIO.read() can't handle 302 status code on url
File tmpFile = ImageUtils.downloadImgFile(u);
b = read(tmpFile);
Expand All @@ -115,11 +115,14 @@ public static BufferedImage read(URL u) throws IOException {
if(USE_IMAGE_IO_READ_IMPL) {
return ImageIO.read(u);
} else {
try (
InputStream is = u.openStream();
ImageInputStream iis = ImageIO.createImageInputStream(is)
) {
return read(iis);
try (InputStream is = u.openStream();) {
ImageInputStream iis = ImageIO.createImageInputStream(is);
logger.debug("Got stream: " + iis);
BufferedImage bi = read(iis);
if(bi == null) {
iis.close();
}
return bi;
}
}
}
Expand All @@ -128,12 +131,48 @@ public static BufferedImage read(File f) throws IOException {
if(USE_IMAGE_IO_READ_IMPL) {
return ImageIO.read(f);
} else {
try (ImageInputStream iis = ImageIO.createImageInputStream(f)) {
return read(iis);
}
ImageInputStream iis = ImageIO.createImageInputStream(f);
if (iis == null) {
throw new IIOException("Can't create an ImageInputStream!");
}
logger.debug("Got stream: " + iis);
try {
return read(iis);
} catch (IIOException e) {
iis.close();
throw e;
}
}
}

// public static ImageInputStream createImageInputStream(Object input) throws IOException {
// if (input == null) {
// throw new IllegalArgumentException("input == null!");
// }
//
// Iterator<?> iter;
// // Ensure category is present
// try {
// iter = IIORegistry.getDefaultInstance().getServiceProviders(ImageInputStreamSpi.class,
// true);
// } catch (IllegalArgumentException e) {
// return null;
// }
//
// while (iter.hasNext()) {
// ImageInputStreamSpi spi = (ImageInputStreamSpi)iter.next();
// if (spi.getInputClass().isInstance(input)) {
// try {
// return spi.createInputStreamInstance(input);
// } catch (IOException e) {
// throw new IIOException("Can't create cache file!", e);
// }
// }
// }
//
// return null;
// }

/**
* Alternative implementation of ImageIO::read.</br>
* If one image reader claims to be able to read an ImageInputStream but fails then,
Expand Down Expand Up @@ -178,6 +217,8 @@ private static BufferedImage read(ImageInputStream iis) throws IOException {
if(bi != null) {
break;
}
} else {
logger.debug(rSpi.getPluginClassName() + " denied reading the source.");
}
}
if(!fails.isEmpty()) {
Expand Down

0 comments on commit 4a0835b

Please sign in to comment.