KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > jpeg > COMMarkerSegment


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
8 package com.sun.imageio.plugins.jpeg;
9
10 import javax.imageio.metadata.IIOMetadataNode JavaDoc;
11 import javax.imageio.stream.ImageOutputStream JavaDoc;
12 import javax.imageio.metadata.IIOInvalidTreeException JavaDoc;
13
14 import java.io.IOException JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16
17 import org.w3c.dom.Node JavaDoc;
18
19 /**
20  * A Comment marker segment. Retains an array of bytes representing the
21  * comment data as it is read from the stream. If the marker segment is
22  * constructed from a String, then local default encoding is assumed
23  * when creating the byte array. If the marker segment is created from
24  * an <code>IIOMetadataNode</code>, the user object, if present is
25  * assumed to be a byte array containing the comment data. If there is
26  * no user object then the comment attribute is used to create the
27  * byte array, again assuming the default local encoding.
28  */

29 class COMMarkerSegment extends MarkerSegment {
30     private static final String JavaDoc ENCODING = "ISO-8859-1";
31         
32     /**
33      * Constructs a marker segment from the given buffer, which contains
34      * data from an <code>ImageInputStream</code>. This is used when
35      * reading metadata from a stream.
36      */

37     COMMarkerSegment(JPEGBuffer buffer) throws IOException JavaDoc {
38         super(buffer);
39         loadData(buffer);
40     }
41
42     /**
43      * Constructs a marker segment from a String. This is used when
44      * modifying metadata from a non-native tree and when transcoding.
45      * The default encoding is used to construct the byte array.
46      */

47     COMMarkerSegment(String JavaDoc comment) {
48         super(JPEG.COM);
49         data = comment.getBytes(); // Default encoding
50
}
51
52     /**
53      * Constructs a marker segment from a native tree node. If the node
54      * is an <code>IIOMetadataNode</code> and contains a user object,
55      * that object is used rather than the string attribute. If the
56      * string attribute is used, the default encoding is used.
57      */

58     COMMarkerSegment(Node JavaDoc node) throws IIOInvalidTreeException JavaDoc{
59         super(JPEG.COM);
60         if (node instanceof IIOMetadataNode JavaDoc) {
61             IIOMetadataNode JavaDoc ourNode = (IIOMetadataNode JavaDoc) node;
62             data = (byte []) ourNode.getUserObject();
63         }
64         if (data == null) {
65             String JavaDoc comment =
66                 node.getAttributes().getNamedItem("comment").getNodeValue();
67             if (comment != null) {
68                 data = comment.getBytes(); // Default encoding
69
} else {
70                 throw new IIOInvalidTreeException JavaDoc("Empty comment node!", node);
71             }
72         }
73     }
74
75     /**
76      * Returns the array encoded as a String, using ISO-Latin-1 encoding.
77      * If an application needs another encoding, the data array must be
78      * consulted directly.
79      */

80     String JavaDoc getComment() {
81         try {
82             return new String JavaDoc (data, ENCODING);
83         } catch (UnsupportedEncodingException JavaDoc e) {} // Won't happen
84
return null;
85     }
86
87     /**
88      * Returns an <code>IIOMetadataNode</code> containing the data array
89      * as a user object and a string encoded using ISO-8895-1, as an
90      * attribute.
91      */

92     IIOMetadataNode JavaDoc getNativeNode() {
93         IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("com");
94         node.setAttribute("comment", getComment());
95         if (data != null) {
96             node.setUserObject(data.clone());
97         }
98         return node;
99     }
100
101     /**
102      * Writes the data for this segment to the stream in
103      * valid JPEG format, directly from the data array.
104      */

105     void write(ImageOutputStream JavaDoc ios) throws IOException JavaDoc {
106         length = 2 + data.length;
107         writeTag(ios);
108         ios.write(data);
109     }
110
111     void print() {
112         printTag("COM");
113         System.out.println("<" + getComment() + ">");
114     }
115 }
116
117
Popular Tags