1. /*
  2. * @(#)COMMarkerSegment.java 1.6 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.imageio.plugins.jpeg;
  8. import javax.imageio.metadata.IIOMetadataNode;
  9. import javax.imageio.stream.ImageOutputStream;
  10. import javax.imageio.metadata.IIOInvalidTreeException;
  11. import java.io.IOException;
  12. import java.io.UnsupportedEncodingException;
  13. import org.w3c.dom.Node;
  14. /**
  15. * A Comment marker segment. Retains an array of bytes representing the
  16. * comment data as it is read from the stream. If the marker segment is
  17. * constructed from a String, then local default encoding is assumed
  18. * when creating the byte array. If the marker segment is created from
  19. * an <code>IIOMetadataNode</code>, the user object, if present is
  20. * assumed to be a byte array containing the comment data. If there is
  21. * no user object then the comment attribute is used to create the
  22. * byte array, again assuming the default local encoding.
  23. */
  24. class COMMarkerSegment extends MarkerSegment {
  25. private static final String ENCODING = "ISO-8859-1";
  26. /**
  27. * Constructs a marker segment from the given buffer, which contains
  28. * data from an <code>ImageInputStream</code>. This is used when
  29. * reading metadata from a stream.
  30. */
  31. COMMarkerSegment(JPEGBuffer buffer) throws IOException {
  32. super(buffer);
  33. loadData(buffer);
  34. }
  35. /**
  36. * Constructs a marker segment from a String. This is used when
  37. * modifying metadata from a non-native tree and when transcoding.
  38. * The default encoding is used to construct the byte array.
  39. */
  40. COMMarkerSegment(String comment) {
  41. super(JPEG.COM);
  42. data = comment.getBytes(); // Default encoding
  43. }
  44. /**
  45. * Constructs a marker segment from a native tree node. If the node
  46. * is an <code>IIOMetadataNode</code> and contains a user object,
  47. * that object is used rather than the string attribute. If the
  48. * string attribute is used, the default encoding is used.
  49. */
  50. COMMarkerSegment(Node node) throws IIOInvalidTreeException{
  51. super(JPEG.COM);
  52. if (node instanceof IIOMetadataNode) {
  53. IIOMetadataNode ourNode = (IIOMetadataNode) node;
  54. data = (byte []) ourNode.getUserObject();
  55. }
  56. if (data == null) {
  57. String comment =
  58. node.getAttributes().getNamedItem("comment").getNodeValue();
  59. if (comment != null) {
  60. data = comment.getBytes(); // Default encoding
  61. } else {
  62. throw new IIOInvalidTreeException("Empty comment node!", node);
  63. }
  64. }
  65. }
  66. /**
  67. * Returns the array encoded as a String, using ISO-Latin-1 encoding.
  68. * If an application needs another encoding, the data array must be
  69. * consulted directly.
  70. */
  71. String getComment() {
  72. try {
  73. return new String (data, ENCODING);
  74. } catch (UnsupportedEncodingException e) {} // Won't happen
  75. return null;
  76. }
  77. /**
  78. * Returns an <code>IIOMetadataNode</code> containing the data array
  79. * as a user object and a string encoded using ISO-8895-1, as an
  80. * attribute.
  81. */
  82. IIOMetadataNode getNativeNode() {
  83. IIOMetadataNode node = new IIOMetadataNode("com");
  84. node.setAttribute("comment", getComment());
  85. if (data != null) {
  86. node.setUserObject(data.clone());
  87. }
  88. return node;
  89. }
  90. /**
  91. * Writes the data for this segment to the stream in
  92. * valid JPEG format, directly from the data array.
  93. */
  94. void write(ImageOutputStream ios) throws IOException {
  95. length = 2 + data.length;
  96. writeTag(ios);
  97. ios.write(data);
  98. }
  99. void print() {
  100. printTag("COM");
  101. System.out.println("<" + getComment() + ">");
  102. }
  103. }