I have some code which uses the ImageReader class to read in a large number of TIF images. The imageReader
object is final and created in the constructor.
synchronized(imageReader) {
LOG.debug(file);
FileInputStream fin = new FileInputStream(file);
ImageInputStream iis = ImageIO.createImageInputStream(fin);
imageReader.setInput(iis, false);
int sourceXSubSampling = targetSize == null ?
1 : Math.max(1, imageReader.getWidth(0) / targetSize.width);
int sourceYSubSampling = targetSize == null ?
1 : Math.max(1, imageReader.getHeight(0) / targetSize.height);
ImageReadParam subSamplingParam = new ImageReadParam();
subSamplingParam.setSourceSubsampling(sourceXSubSampling, sourceYSubSampling, 0, 0);
return imageReader.read(0, subSamplingParam);
}
About one instance in four the ImageReader get "stuck" on the first image it loaded and keeps loading that same image over and over again even though it is provided with different ImageInputStreams. This is evidenced by the output to the logger.
How do I solve this. I was thinking about taking a "fingerprint" of the image and getting a different ImageReader from the iterator if this occurs but that seems like overkill. Does anyone know how to solve this problem?