View Javadoc

1   /***
2    *
3    * Copyright 2004 Hiram Chirino
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   **/
18  package org.codehaus.activemq.journal.impl;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.DataInputStream;
23  import java.io.DataOutputStream;
24  import java.io.IOException;
25  
26  /***
27   * Serializes/Deserializes Mark records.  When a mark is set in the journal, a Mark record 
28   * must be journaled so that on restart the Journal knows what was the last mark.
29   * 
30   * @version $Revision: 1.1 $
31   */
32  class Mark {  
33      
34      public static final int MARK_RECORD_SIZE = 12;
35      
36      public long sequenceId;
37      public int offsetId;
38      
39      public Mark() {}
40      public Mark(RecordLocationImpl location) {
41          sequenceId = location.getSequenceId();
42          offsetId = location.getSegmentOffset();
43      }    
44      
45      public byte[] writeExternal() throws IOException {
46          ByteArrayOutputStream stream = new ByteArrayOutputStream(16);
47          DataOutputStream os = new DataOutputStream(stream);
48          os.writeLong(sequenceId);
49          os.writeInt(offsetId);
50          os.close();
51          return stream.toByteArray();
52      }
53      
54      public void readExternal(byte data[]) throws IOException {
55          DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));
56          sequenceId = is.readLong();
57          offsetId = is.readInt();
58          is.close();
59      }
60      /***
61       * @param mark
62       */
63      public void copy(Mark mark) {
64          this.offsetId = mark.offsetId;
65          this.sequenceId = mark.sequenceId;
66      }
67      
68  }