4343import org .eclipse .swt .graphics .ImageLoader ;
4444import org .eclipse .swt .internal .DPIUtil .ElementAtZoom ;
4545import org .eclipse .swt .internal .NativeImageLoader ;
46+ import org .eclipse .swt .internal .image .FileFormat ;
4647
4748/**
4849 * An image descriptor that loads its image information from a file.
@@ -54,7 +55,7 @@ private class ImageProvider implements ImageFileNameProvider {
5455 @ Override
5556 public String getImagePath (int zoom ) {
5657 final boolean logIOException = zoom == 100 ;
57- if (zoom == 100 ) {
58+ if (canLoadAtZoom ( zoom ) || zoom == 100 ) {
5859 return getFilePath (name , logIOException );
5960 }
6061 SourceAtZoom <String > xName = getxName (name , zoom );
@@ -155,8 +156,8 @@ public ImageData getImageData(int zoom) {
155156 * file cannot be found
156157 */
157158 private SourceAtZoom <InputStream > getStream (int zoom ) {
158- if (zoom == 100 ) {
159- return getStream (new SourceAtZoom <>(name , 100 ));
159+ if (canLoadAtZoom ( zoom ) || zoom == 100 ) {
160+ return getStream (new SourceAtZoom <>(name , zoom ));
160161 }
161162
162163 SourceAtZoom <InputStream > xstream = getStream (getxName (name , zoom ));
@@ -172,6 +173,18 @@ private SourceAtZoom<InputStream> getStream(int zoom) {
172173 return null ;
173174 }
174175
176+ @ SuppressWarnings ("restriction" )
177+ private boolean canLoadAtZoom (int zoom ) {
178+ try (InputStream in = getStream (new SourceAtZoom <>(name , zoom )).source ()) {
179+ if (in != null ) {
180+ return FileFormat .canLoadAtZoom (new ElementAtZoom <>(in , 100 ), zoom );
181+ }
182+ } catch (IOException e ) {
183+ Policy .getLog ().log (new Status (IStatus .ERROR , Policy .JFACE , e .getLocalizedMessage (), e ));
184+ }
185+ return false ;
186+ }
187+
175188 /**
176189 * try to obtain a stream for a given name, if the name does not match a valid
177190 * resource null is returned
@@ -180,9 +193,9 @@ private SourceAtZoom<InputStream> getStream(int zoom) {
180193 * @return an {@link InputStream} to read from, or <code>null</code> if fileName
181194 * does not denotes an existing resource
182195 */
196+ @ SuppressWarnings ("resource" )
183197 private SourceAtZoom <InputStream > getStream (SourceAtZoom <String > fileName ) {
184198 if (fileName != null ) {
185- // TODO DO we need to close these?
186199 if (location != null ) {
187200 return new SourceAtZoom <>(location .getResourceAsStream (fileName .source ()), fileName .zoom ());
188201 }
0 commit comments