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 package org.openoffice.xmerge.converter.palm;
25 
26 import java.io.DataInput;
27 import java.io.DataOutput;
28 import java.io.IOException;
29 
30 /**
31  *  <p>Class used only internally by <code>PdbEncoder</code> and
32  *  <code>PdbDecoder</code> to store, read and write a PDB header.</p>
33  *
34  *  <p>Note that fields are intended to be accessible only at the
35  *  package level.</p>
36  *
37  *  <p>Some of the fields are internally represented using a
38  *  larger type since Java does not have unsigned types.
39  *  Some are not since they are not relevant for now.
40  *  The <code>read</code> and <code>write</code> methods should
41  *  handle them properly.</p>
42  *
43  *  @author  Herbie Ong
44  *  @see     PalmDB
45  *  @see     Record
46  */
47 final class PdbHeader {
48 
49 
50     /**  Name of the database. 32 bytes. */
51     byte[] pdbName = null;
52 
53     /**
54      *  Flags for the database.  Palm UInt16.  Unsignedness should be
55      *  irrelevant.
56      */
57     short attribute = 0;
58 
59     /**  Application-specific version for the database.  Palm UInt16. */
60     int version = 0;
61 
62     /**  Date created. Palm UInt32. */
63     long creationDate = 0;
64 
65     /**  Date last modified. Palm UInt32.  */
66     long modificationDate = 0;
67 
68     /**  Date last backup. Palm UInt32. */
69     long lastBackupDate = 0;
70 
71     /**
72      *  Incremented every time a <code>Record</code> is
73      *  added, deleted or modified.  Palm UInt32.
74      */
75     long modificationNumber = 0;
76 
77     /**  Optional field. Palm UInt32. Unsignedness should be irrelevant. */
78     int appInfoID = 0;
79 
80     /**  Optional field. Palm UInt32. Unsignedness should be irrelevant. */
81     int sortInfoID = 0;
82 
83     /**  Database type ID. Palm UInt32. Unsignedness should be irrelevant. */
84     int typeID = 0;
85 
86     /**  Database creator ID. Palm UInt32. Unsignedness should be irrelevant. */
87     int creatorID = 0;
88 
89     /**  ??? */
90     int uniqueIDSeed = 0;
91 
92     /**  See numRecords.  4 bytes. */
93     int nextRecordListID = 0;
94 
95     /**
96      *  Number of Records stored in the database header.
97      *  If all the <code>Record</code> entries cannot fit in the header,
98      *  then <code>nextRecordList</code> has the local ID of a
99      *  RecordList that contains the next set of <code>Record</code>.
100      *  Palm UInt16.
101      */
102     int numRecords = 0;
103 
104 
105     /**
106      *  Read in the data for the PDB header.  Need to
107      *  preserve the unsigned value for some of the fields.
108      *
109      *  @param  in  A <code>DataInput</code> object.
110      *
111      *  @throws  IOException  If any I/O error occurs.
112      */
read(DataInput in)113     public void read(DataInput in) throws IOException {
114 
115         pdbName = new byte[PalmDB.NAME_LENGTH];
116         in.readFully(pdbName);
117         attribute = in.readShort();
118         version = in.readUnsignedShort();
119         creationDate = ((long) in.readInt()) & 0xffffffffL;
120         modificationDate = ((long) in.readInt()) & 0xffffffffL;
121         lastBackupDate = ((long) in.readInt())  & 0xffffffffL;
122         modificationNumber = ((long) in.readInt()) & 0xffffffffL;
123         appInfoID = in.readInt();
124         sortInfoID = in.readInt();
125         creatorID = in.readInt();
126         typeID = in.readInt();
127         uniqueIDSeed = in.readInt();
128         nextRecordListID = in.readInt();
129         numRecords = in.readUnsignedShort();
130     }
131 
132 
133     /**
134      *  Write out PDB header data.
135      *
136      *  @param  out  A <code>DataOutput</code> object.
137      *
138      *  @throws  IOException  If any I/O error occurs.
139      */
write(DataOutput out)140     public void write(DataOutput out) throws IOException {
141 
142         out.write(pdbName);
143         out.writeShort(attribute);
144         out.writeShort(version);
145         out.writeInt((int) creationDate);
146         out.writeInt((int) modificationDate);
147         out.writeInt((int) lastBackupDate);
148         out.writeInt((int) modificationNumber);
149         out.writeInt(appInfoID);
150         out.writeInt(sortInfoID);
151         out.writeInt(typeID);
152         out.writeInt(creatorID);
153         out.writeInt(uniqueIDSeed);
154         out.writeInt(nextRecordListID);
155         out.writeShort(numRecords);
156     }
157 }
158 
159