public byte[] writeAnimation(ImageAnimation animation) {
ImageWriter writer = ImageIO.getImageWritersBySuffix("gif").next();
ImageWriteParam param = writer.getDefaultWriteParam();
ImageTypeSpecifier imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_4BYTE_ABGR);
IIOMetadata metaData = writer.getDefaultImageMetadata(imageTypeSpecifier, param);
String metaFormatName = metaData.getNativeMetadataFormatName();
IIOMetadataNode root = (IIOMetadataNode) metaData.getAsTree(metaFormatName);
IIOMetadataNode graphicsControlExtensionNode = getNode(root, "GraphicControlExtension");
graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
graphicsControlExtensionNode.setAttribute("transparentColorFlag", "FALSE");
graphicsControlExtensionNode.setAttribute("delayTime", Integer.toString(animation.getFrameDuration() / 10));
graphicsControlExtensionNode.setAttribute("transparentColorIndex", "0");
IIOMetadataNode commentsNode = getNode(root, "CommentExtensions");
commentsNode.setAttribute("CommentExtension", "Created by MAH");
IIOMetadataNode appEntensionsNode = getNode(root, "ApplicationExtensions");
IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");
child.setAttribute("applicationID", "NETSCAPE");
child.setAttribute("authenticationCode", "2.0");
int loop = 0;
child.setUserObject(new byte[] { 0x1, (byte) (loop & 0xFF), (byte)
((loop >> 8) & 0xFF) });
appEntensionsNode.appendChild(child);
ByteArrayOutputStream os = null;
ImageOutputStream ios = null;
try {
os = new ByteArrayOutputStream();
ios = new MemoryCacheImageOutputStream(os);
metaData.setFromTree(metaFormatName, root);
writer.setOutput(ios);
writer.prepareWriteSequence(null);