/*
 * @(#)COMMarkerSegment.java	1.1 01/03/15
 *
 * Copyright 2000 by Sun Microsystems, Inc.,
 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Sun Microsystems, Inc. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Sun.
 */

package com.sun.imageio.plugins.jpeg;

import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream;

import java.io.IOException;

import org.w3c.dom.Node;

/**
 * A Comment marker segment.
 */ 
class COMMarkerSegment extends MarkerSegment {
    String comment;
        
    COMMarkerSegment(JPEGBuffer buffer) throws IOException {
        super(buffer);
        comment = new String(buffer.buf, buffer.bufPtr, length);
        buffer.bufAvail -= length;
        buffer.bufPtr += length;
    }

    COMMarkerSegment(String comment) {
        super(JPEG.COM);
        this.comment = comment;
    }


    COMMarkerSegment(Node node) {
        super(JPEG.COM);
        comment = node.getAttributes().getNamedItem("comment").getNodeValue();
    }

    IIOMetadataNode getNativeNode() {
        IIOMetadataNode node = new IIOMetadataNode("com");
        node.setAttribute("comment", comment);
        return node;
    }

    /**
     * Writes the data for this segment to the stream in
     * valid JPEG format.
     */
    void write(ImageOutputStream ios) throws IOException {
        length = 2 + comment.length();
        writeTag(ios);
        ios.writeBytes(comment);
        // FIXME - encoding issue.  Keep the original data
        // and use that if its available.
    }

    void print() {
        printTag("COM");
        System.out.println("<" + comment + ">");
    }
}

