1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import java.io.DataInput;
25 import java.io.DataOutput;
26 import java.io.IOException;
27 
28 /**
29  *  <p>Class used only internally by <code>PDBEncoder</code> and
30  *  <code>PDBDecoder</code> to store, read and write a pdb header.</p>
31  *
32  *  <p>Note that fields are intended to be accessible only at the
33  *  package level.</p>
34  *
35  *  <p>Some of the fields are internally represented using a
36  *  larger type since Java does not have unsigned types.
37  *  Some are not since they are not relevant for now.
38  *  The <code>read</code> and <code>write</code> methods should
39  *  handle them properly.</p>
40  *
41  *  @author    Herbie Ong
42  */
43 
44 final class PDBHeader {
45 
46     /** name of the database. 32 bytes. */
47     byte[] pdbName = null;
48 
49     /** flags for the database. Palm UInt16. Unsignedness should be irrelevant. */
50     short attribute = 0;
51 
52     /** application-specific version for the database. Palm UInt16 */
53     int version = 0;
54 
55     /** date created. Palm UInt32 */
56     long creationDate = 0;
57 
58     /** date last modified. Palm UInt32  */
59     long modificationDate = 0;
60 
61     /** date last backup. Palm UInt32 */
62     long lastBackupDate = 0;
63 
64     /**
65      *  incremented every time a record is
66      *  added, deleted or modified.  Palm UInt32.
67      */
68     long modificationNumber = 0;
69 
70     /** optional field. Palm UInt32. Unsignedness should be irrelevant. */
71     int appInfoID = 0;
72 
73     /** optional field. Palm UInt32. Unsignedness should be irrelevant. */
74     int sortInfoID = 0;
75 
76     /** database type id. Palm UInt32. Unsignedness should be irrelevant. */
77     int typeID = 0;
78 
79     /** database creator id. Palm UInt32. Unsignedness should be irrelevant. */
80     int creatorID = 0;
81 
82     /** ??? */
83     int uniqueIDSeed = 0;
84 
85     /** see numRecords. 4 bytes. */
86     int nextRecordListID = 0;
87 
88     /**
89      *  number of records stored in the database header.
90      *  If all the record entries cannot fit in the header,
91      *  then nextRecordList has the local ID of a
92      *  recordList that contains the next set of records.
93      *  Palm UInt16.
94      */
95     int numRecords = 0;
96 
97     /**
98      *  Read in the data for the pdb header.  Need to
99      *  preserve the unsigned value for some of the fields.
100      *
101      *  @param   di    a DataInput object
102      *  @throws   IOException    if I/O error occurs
103      */
104 
read(DataInput in)105     public void read(DataInput in) throws IOException {
106 
107         pdbName = new byte[PalmDB.NAME_LENGTH];
108         in.readFully(pdbName);
109         attribute = in.readShort();
110         version = in.readUnsignedShort();
111         creationDate = ((long) in.readInt()) & 0xffffffffL;
112         modificationDate = ((long) in.readInt()) & 0xffffffffL;
113         lastBackupDate = ((long) in.readInt())  & 0xffffffffL;
114         modificationNumber = ((long) in.readInt()) & 0xffffffffL;
115         appInfoID = in.readInt();
116         sortInfoID = in.readInt();
117         creatorID = in.readInt();
118         typeID = in.readInt();
119         uniqueIDSeed = in.readInt();
120         nextRecordListID = in.readInt();
121         numRecords = in.readUnsignedShort();
122     }
123 
124     /**
125      *  Write out pdb header data.
126      *
127      *  @param   out    a DataOut object
128      *  @throws   IOException    if I/O error occurs
129      */
130 
write(DataOutput out)131     public void write(DataOutput out) throws IOException {
132 
133         out.write(pdbName);
134         out.writeShort(attribute);
135         out.writeShort(version);
136         out.writeInt((int) creationDate);
137         out.writeInt((int) modificationDate);
138         out.writeInt((int) lastBackupDate);
139         out.writeInt((int) modificationNumber);
140         out.writeInt(appInfoID);
141         out.writeInt(sortInfoID);
142         out.writeInt(typeID);
143         out.writeInt(creatorID);
144         out.writeInt(uniqueIDSeed);
145         out.writeInt(nextRecordListID);
146         out.writeShort(numRecords);
147     }
148 }
149 
150