/**
 *
 * Copyright 2004 Hiram Chirino
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **/
package org.codehaus.activemq.journal.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Serializes/Deserializes Mark records.  When a mark is set in the journal, a Mark record 
 * must be journaled so that on restart the Journal knows what was the last mark.
 * 
 * @version $Revision: 1.1 $
 */
class Mark {  
    
    public static final int MARK_RECORD_SIZE = 12;
    
    public long sequenceId;
    public int offsetId;
    
    public Mark() {}
    public Mark(RecordLocationImpl location) {
        sequenceId = location.getSequenceId();
        offsetId = location.getSegmentOffset();
    }    
    
    public byte[] writeExternal() throws IOException {
        ByteArrayOutputStream stream = new ByteArrayOutputStream(16);
        DataOutputStream os = new DataOutputStream(stream);
        os.writeLong(sequenceId);
        os.writeInt(offsetId);
        os.close();
        return stream.toByteArray();
    }
    
    public void readExternal(byte data[]) throws IOException {
        DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));
        sequenceId = is.readLong();
        offsetId = is.readInt();
        is.close();
    }
    /**
     * @param mark
     */
    public void copy(Mark mark) {
        this.offsetId = mark.offsetId;
        this.sequenceId = mark.sequenceId;
    }
    
}