float q = 1.0f;
if (quality != null)
q = quality.floatValue();
writeParam.setCompressionQuality(q);
DAByteArrayOutputStream buffer = new DAByteArrayOutputStream();
writer.setOutput(new MemoryCacheImageOutputStream(buffer));
IIOImage ioImage = new IIOImage(bufferedImage, null, null);
writer.write(null, ioImage, writeParam);
writer.dispose();
return buffer.getDirectByteArray();
}