KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > storage > raf > RecordHeader


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.storage.raf;
25
26 import java.io.DataInput JavaDoc;
27 import java.io.DataOutput JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 public class RecordHeader {
31     /** Number of bytes of data that this record can hold (4 bytes). */
32     private int dataCapacity;
33
34     /** Actual number of bytes of data held in this record (4 bytes). */
35     private int dataCount;
36
37     /** Indicates this header's position in the file index. */
38     private int indexPosition;
39
40     /** File pointer to the first byte of record data (8 bytes). */
41     private long dataPointer;
42
43     public RecordHeader() {
44     }
45
46     public RecordHeader(long dataPointer, int dataCapacity) {
47         if (dataCapacity < 1) {
48             throw new IllegalArgumentException JavaDoc("Bad record size: " + dataCapacity);
49         }
50         this.dataPointer = dataPointer;
51         this.dataCapacity = dataCapacity;
52         this.dataCount = 0;
53     }
54
55     public void setDataCapacity(int dataCapacity) {
56         this.dataCapacity = dataCapacity;
57     }
58
59     public int getDataCapacity() {
60         return dataCapacity;
61     }
62
63     public void setDataCount(int dataCount) {
64         this.dataCount = dataCount;
65     }
66
67     public int getDataCount() {
68         return dataCount;
69     }
70
71     public void setDataPointer(long dataPointer) {
72         this.dataPointer = dataPointer;
73     }
74
75     public long getDataPointer() {
76         return dataPointer;
77     }
78
79     public int getFreeSpace() {
80         return dataCapacity - dataCount;
81     }
82
83     public void setIndexPosition(int indexPosition) {
84         this.indexPosition = indexPosition;
85     }
86
87     public int getIndexPosition() {
88         return indexPosition;
89     }
90
91     public static RecordHeader readHeader(DataInput JavaDoc in) throws IOException JavaDoc {
92         RecordHeader r = new RecordHeader();
93         r.read(in);
94         return r;
95     }
96
97     /**
98      * Returns a new record header which occupies the free space of this record.
99      * Shrinks this record size by the size of its free space.
100      */

101     public RecordHeader split() {
102         long newFp = dataPointer + (long) dataCount;
103         RecordHeader newRecord = new RecordHeader(newFp, getFreeSpace());
104         dataCapacity = dataCount;
105         return newRecord;
106     }
107
108     public void write(DataOutput JavaDoc out) throws IOException JavaDoc {
109         out.writeLong(dataPointer);
110         out.writeInt(dataCapacity);
111         out.writeInt(dataCount);
112     }
113
114     public void read(DataInput JavaDoc in) throws IOException JavaDoc {
115         dataPointer = in.readLong();
116         dataCapacity = in.readInt();
117         dataCount = in.readInt();
118     }
119
120     public boolean equals(Object JavaDoc object) {
121         try {
122             RecordHeader candidate = (RecordHeader) object;
123 // return ((dataCapacity == candidate.dataCapacity) &&
124
// (dataCount == candidate.dataCount) &&
125
// (dataPointer == candidate.dataPointer) &&
126
// (indexPosition == candidate.indexPosition));
127
return (dataCount == candidate.dataCount);
128         } catch (Exception JavaDoc e) {
129         }
130         return false;
131     }
132
133     public String JavaDoc toString() {
134         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
135         sb.append("RecordHeader(");
136         sb.append("dCap").append(dataCapacity);
137         sb.append(",dCnt").append(dataCount);
138         sb.append("dPnt").append(dataPointer);
139         sb.append("iPos").append(indexPosition);
140         sb.append(")");
141         return sb.toString();
142     }
143 }
144
Popular Tags