1 7 8 package com.sun.imageio.plugins.jpeg; 9 10 import javax.imageio.metadata.IIOMetadataNode ; 11 import javax.imageio.stream.ImageOutputStream ; 12 import javax.imageio.metadata.IIOInvalidTreeException ; 13 14 import java.io.IOException ; 15 import java.io.UnsupportedEncodingException ; 16 17 import org.w3c.dom.Node ; 18 19 29 class COMMarkerSegment extends MarkerSegment { 30 private static final String ENCODING = "ISO-8859-1"; 31 32 37 COMMarkerSegment(JPEGBuffer buffer) throws IOException { 38 super(buffer); 39 loadData(buffer); 40 } 41 42 47 COMMarkerSegment(String comment) { 48 super(JPEG.COM); 49 data = comment.getBytes(); } 51 52 58 COMMarkerSegment(Node node) throws IIOInvalidTreeException { 59 super(JPEG.COM); 60 if (node instanceof IIOMetadataNode ) { 61 IIOMetadataNode ourNode = (IIOMetadataNode ) node; 62 data = (byte []) ourNode.getUserObject(); 63 } 64 if (data == null) { 65 String comment = 66 node.getAttributes().getNamedItem("comment").getNodeValue(); 67 if (comment != null) { 68 data = comment.getBytes(); } else { 70 throw new IIOInvalidTreeException ("Empty comment node!", node); 71 } 72 } 73 } 74 75 80 String getComment() { 81 try { 82 return new String (data, ENCODING); 83 } catch (UnsupportedEncodingException e) {} return null; 85 } 86 87 92 IIOMetadataNode getNativeNode() { 93 IIOMetadataNode node = new IIOMetadataNode ("com"); 94 node.setAttribute("comment", getComment()); 95 if (data != null) { 96 node.setUserObject(data.clone()); 97 } 98 return node; 99 } 100 101 105 void write(ImageOutputStream ios) throws IOException { 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 |