1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 import java.io.DataInput;
29 import java.io.DataOutput;
30 import java.io.IOException;
31 
32 /**
33  *  <p>Class used only internally by <code>PDBEncoder</code> and
34  *  <code>PDBDecoder</code> to store, read and write a pdb header.</p>
35  *
36  *  <p>Note that fields are intended to be accessible only at the
37  *  package level.</p>
38  *
39  *  <p>Some of the fields are internally represented using a
40  *  larger type since Java does not have unsigned types.
41  *  Some are not since they are not relevant for now.
42  *  The <code>read</code> and <code>write</code> methods should
43  *  handle them properly.</p>
44  *
45  *  @author    Herbie Ong
46  */
47 
48 final class PDBHeader {
49 
50     /** name of the database. 32 bytes. */
51     byte[] pdbName = null;
52 
53     /** flags for the database. Palm UInt16. Unsignedness should be irrelevant. */
54     short attribute = 0;
55 
56     /** application-specific version for the database. Palm UInt16 */
57     int version = 0;
58 
59     /** date created. Palm UInt32 */
60     long creationDate = 0;
61 
62     /** date last modified. Palm UInt32  */
63     long modificationDate = 0;
64 
65     /** date last backup. Palm UInt32 */
66     long lastBackupDate = 0;
67 
68     /**
69      *  incremented every time a record is
70      *  added, deleted or modified.  Palm UInt32.
71      */
72     long modificationNumber = 0;
73 
74     /** optional field. Palm UInt32. Unsignedness should be irrelevant. */
75     int appInfoID = 0;
76 
77     /** optional field. Palm UInt32. Unsignedness should be irrelevant. */
78     int sortInfoID = 0;
79 
80     /** database type id. Palm UInt32. Unsignedness should be irrelevant. */
81     int typeID = 0;
82 
83     /** database creator id. Palm UInt32. Unsignedness should be irrelevant. */
84     int creatorID = 0;
85 
86     /** ??? */
87     int uniqueIDSeed = 0;
88 
89     /** see numRecords. 4 bytes. */
90     int nextRecordListID = 0;
91 
92     /**
93      *  number of records stored in the database header.
94      *  If all the record entries cannot fit in the header,
95      *  then nextRecordList has the local ID of a
96      *  recordList that contains the next set of records.
97      *  Palm UInt16.
98      */
99     int numRecords = 0;
100 
101     /**
102      *  Read in the data for the pdb header.  Need to
103      *  preserve the unsigned value for some of the fields.
104      *
105      *  @param   di    a DataInput object
106      *  @throws   IOException    if I/O error occurs
107      */
108 
109     public void read(DataInput in) throws IOException {
110 
111         pdbName = new byte[PalmDB.NAME_LENGTH];
112         in.readFully(pdbName);
113         attribute = in.readShort();
114         version = in.readUnsignedShort();
115         creationDate = ((long) in.readInt()) & 0xffffffffL;
116         modificationDate = ((long) in.readInt()) & 0xffffffffL;
117         lastBackupDate = ((long) in.readInt())  & 0xffffffffL;
118         modificationNumber = ((long) in.readInt()) & 0xffffffffL;
119         appInfoID = in.readInt();
120         sortInfoID = in.readInt();
121         creatorID = in.readInt();
122         typeID = in.readInt();
123         uniqueIDSeed = in.readInt();
124         nextRecordListID = in.readInt();
125         numRecords = in.readUnsignedShort();
126     }
127 
128     /**
129      *  Write out pdb header data.
130      *
131      *  @param   out    a DataOut object
132      *  @throws   IOException    if I/O error occurs
133      */
134 
135     public void write(DataOutput out) throws IOException {
136 
137         out.write(pdbName);
138         out.writeShort(attribute);
139         out.writeShort(version);
140         out.writeInt((int) creationDate);
141         out.writeInt((int) modificationDate);
142         out.writeInt((int) lastBackupDate);
143         out.writeInt((int) modificationNumber);
144         out.writeInt(appInfoID);
145         out.writeInt(sortInfoID);
146         out.writeInt(typeID);
147         out.writeInt(creatorID);
148         out.writeInt(uniqueIDSeed);
149         out.writeInt(nextRecordListID);
150         out.writeShort(numRecords);
151     }
152 }
153 
154