Skip to main content

MemoryCacheImageOutputStream problems

1 reply [Last post]
potato95
Offline
Joined: 2006-06-23
Points: 0

Hi
In the below method I am trying to take an image and use a tiff writer to return a InputStream but for some reason it fails. If I change from a MemoryCacheImageOutputStream to a FileImageOutputStream and tweak the code then the method writes the image out to file with no problems. I guess I am not using the MemoryCacheImageOutputStream properly. Ultimately I am trying to get back an InputStream from the tiff image so if there is a better way please let me know.

Thanks

Giles

public static InputStream writeToInputStream(PlanarImage scaledIm, ImageWriteParam iwparam) throws FileNotFoundException, IOException {

TIFFImageWriter writer = (TIFFImageWriter) ImageIO.getImageWritersByFormatName("tif").next();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

MemoryCacheImageOutputStream memoryCacheImageOutputStream = new MemoryCacheImageOutputStream(outputStream);

writer.setOutput(memoryCacheImageOutputStream);

IIOImage iioImage = new IIOImage(scaledIm, null, null);
writer.write(null, iioImage, iwparam);

outputStream.flush();
byte [] bytes = outputStream.toByteArray();
long bLength = bytes.length;

System.out.print("L: "+bLength + " ");

InputStream inputStream = new ByteArrayInputStream(bytes);

outputStream.flush();
outputStream.close();
writer.dispose();
return inputStream;
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
potato95
Offline
Joined: 2006-06-23
Points: 0

Hi,

Solved. I was flushing the wrong thing!...

outputStream.flush();

this should have been...

memoryCacheImageOutputStream.flush();

Now working!