Skip to content

Commit

Permalink
Merge pull request #6829 from utzcoz/add-solution-log-for-unhandled-P…
Browse files Browse the repository at this point in the history
…NG-file

Add solution log for unhandled PNG file
  • Loading branch information
hoisie committed Nov 22, 2021
2 parents 6a6182b + 2d3729e commit ddccf11
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Expand Up @@ -18,6 +18,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.IIOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
Expand Down Expand Up @@ -59,29 +60,48 @@ static Point getImageSizeFromStream(InputStream is) {
}

static RobolectricBufferedImage getImageFromStream(InputStream is) {
return getImageFromStream(null, is);
}

static RobolectricBufferedImage getImageFromStream(String fileName, InputStream is) {
if (!initialized) {
// Stops ImageIO from creating temp files when reading images
// from input stream.
ImageIO.setUseCache(false);
initialized = true;
}

String format = null;
try {
ImageInputStream imageStream = createImageInputStream(is);
Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
if (!readers.hasNext()) return null;
if (!readers.hasNext()) {
return null;
}

ImageReader reader = readers.next();
try {
reader.setInput(imageStream);
String format = reader.getFormatName();
format = reader.getFormatName();
int minIndex = reader.getMinIndex();
BufferedImage image = reader.read(minIndex);
return RobolectricBufferedImage.create(image, ("image/" + format).toLowerCase());
} finally {
reader.dispose();
}
} catch (IOException e) {
Throwable cause = e.getCause();
if (FORMAT_NAME_PNG.equalsIgnoreCase(format)
&& cause instanceof IIOException
&& cause.getMessage() != null
&& cause.getMessage().contains("Invalid chunk length")) {
String pngFileName = "(" + (fileName == null ? "not given PNG file name" : fileName) + ")";
System.err.println(
"The PNG file"
+ pngFileName
+ " cannot be decoded. This may be due to an OpenJDK issue with certain PNG files."
+ " See https://github.com/robolectric/robolectric/issues/6812 for more details.");
}
throw new RuntimeException(e);
}
}
Expand Down
Expand Up @@ -75,15 +75,16 @@ protected static Bitmap decodeResource(Resources res, int id, BitmapFactory.Opti
final TypedValue value = new TypedValue();
InputStream is = res.openRawResource(id, value);

RobolectricBufferedImage image = getImageFromStream(is);
String resourceName = res.getResourceName(id);
RobolectricBufferedImage image = getImageFromStream(resourceName, is);
if (!allowInvalidImageData && image == null) {
if (options != null) {
options.outWidth = -1;
options.outHeight = -1;
}
return null;
}
Bitmap bitmap = create("resource:" + res.getResourceName(id), options, image);
Bitmap bitmap = create("resource:" + resourceName, options, image);
ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
shadowBitmap.createdFromResId = id;
return bitmap;
Expand All @@ -102,7 +103,7 @@ protected static Bitmap decodeFile(String pathName, BitmapFactory.Options option
if (pathName != null && new File(pathName).exists()) {
try (FileInputStream fileInputStream = new FileInputStream(pathName);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
image = getImageFromStream(bufferedInputStream);
image = getImageFromStream(pathName, bufferedInputStream);
} catch (IOException e) {
Logger.warn("Error getting size of bitmap file", e);
}
Expand Down

0 comments on commit ddccf11

Please sign in to comment.