这个Java文件中的构造函数是做什么的?

时间:2013-07-21 17:29:19

标签: java constructor metadata gif

我在这里偶然发现了这个GifSequenceWriter http://dke-tron.googlecode.com/svn-history/r39/trunk/src/nl/unimaas/games/tron/util/GifSequenceWriter.java。构造函数的代码是这样的:

public GifSequenceWriter(
  ImageOutputStream outputStream,
  int imageType,
  int timeBetweenFramesMS,
  boolean loopContinuously) throws IIOException, IOException {
// my method to create a writer
gifWriter = getWriter(); 
imageWriteParam = gifWriter.getDefaultWriteParam();
ImageTypeSpecifier imageTypeSpecifier =
  ImageTypeSpecifier.createFromBufferedImageType(imageType);

imageMetaData =
  gifWriter.getDefaultImageMetadata(imageTypeSpecifier,
  imageWriteParam);

String metaFormatName = imageMetaData.getNativeMetadataFormatName();

IIOMetadataNode root = (IIOMetadataNode)
  imageMetaData.getAsTree(metaFormatName);

IIOMetadataNode graphicsControlExtensionNode = getNode(
  root,
  "GraphicControlExtension");

graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
graphicsControlExtensionNode.setAttribute(
  "transparentColorFlag",
  "FALSE");
graphicsControlExtensionNode.setAttribute(
  "delayTime",
  Integer.toString(timeBetweenFramesMS / 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 = loopContinuously ? 0 : 1;

child.setUserObject(new byte[]{ 0x1, (byte) (loop & 0xFF), (byte)
  ((loop >> 8) & 0xFF)});
appEntensionsNode.appendChild(child);

imageMetaData.setFromTree(metaFormatName, root);

gifWriter.setOutput(outputStream);

gifWriter.prepareWriteSequence(null);
}

根本不明白这个构造函数本质上在做什么?只写一些元数据?谁能解释一下这个?为什么我需要元数据?

1 个答案:

答案 0 :(得分:1)

构造函数显然正在设置GifSequenceWriter,以便将具有适当内容的初始元数据写入基础输出流。

类似的行为可以在ObjectOutputStream中看到,其中构造函数将头信息写入基础流。