I am creating the image from inputStream and I have used two methods but both the methods fails.
1st Method:
FileOutputStream out=new FileOutputStream(file)
byte[] byteBuffer = new byte[1024];
int length = 0;
while ((stream != null) && ((length = stream.read(byteBuffer)) != -1)){
out.write(byteBuffer,0,length);
}
out.flush()
out.close();
stream.close();
This method generates the image but while trying to open it. It says "This is not a valid bitmap file".
And the 2nd method:
BufferedImage imBuff = ImageIO.read(stream);
ImageIO.write(imBuff, 'png', file);
And this one generates the exception.
java.lang.IllegalArgumentException: image == null! at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpe cifier.java:925) at javax.imageio.ImageIO.getWriter(ImageIO.java:1591) at javax.imageio.ImageIO.write(ImageIO.java:1520) at com.k12report.frontend.ClientAuthoringController$$EOU2Lt2o.getImages( ClientAuthoringController.groovy:55) at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(Pag eFragmentCachingFilter.java:195) at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter .java:63) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor. java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor .java:615) at java.lang.Thread.run(Thread.java:722)
Thanks
stream? What is it's contents? Have you tried dumping the contents to disk and verifying it's validity through an external image program?imBuffisnull, this happens when ImageIO don't recognize the file format. You need to test for that situation. Your 1st method looks good to me. You should probably check forstream != nulloutside the loop for readability and performance though.