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 /**
25  *  Contains common static methods and contants for use within the package.
26  *
27  *  @author    Herbie Ong
28  */
29 
30 public final class PDBUtil {
31 
32     /** difference in seconds from Jan 01, 1904 to Jan 01, 1970 */
33     final static long TIME_DIFF = 2082844800;
34 
35     /** encoding scheme used */
36     final static String ENCODING = "8859_1";
37 
38     /** size of a pdb header in bytes */
39     final static int HEADER_SIZE = 78;
40 
41     /**
42      *  This method converts a 4 letter string into the Palm ID integer.
43      *
44      *  It is normally used to convert the Palm creator ID string into
45      *  the integer version of it.  Also use for data types, etc.
46      *
47      *  @param   s    4 character string.
48      *  @return   int    Palm ID representing the string.
49      *  @throws   ArrayIndexOutOfBoundsException    if string parameter
50      *                contains less than 4 characters.
51      */
52 
intID(String s)53     public static int intID(String s) {
54 
55         int id = -1;
56         int temp = 0;
57 
58         // grab the first char and put it in the high bits
59         // note that we only want 8 lower bits of it.
60         temp = (int) s.charAt(0);
61         id = temp << 24;
62 
63         // grab the second char and add it in.
64         temp = ((int) s.charAt(1)) & 0x00ff;
65         id += temp << 16;
66 
67         // grab the second char and add it in.
68         temp = ((int) s.charAt(2)) & 0x00ff;
69         id += temp << 8;
70 
71         // grab the last char and add it in
72         id += ((int) s.charAt(3)) & 0x00ff;
73 
74         return id;
75     }
76 
77     /**
78      *  This method converts an integer into a String given
79      *  the Palm ID format.
80      *
81      *  @param  i   Palm id.
82      *  @return  String   string representation.
83      */
84 
stringID(int i)85     public static String stringID(int i) {
86 
87         char ch[] = new char[4];
88         ch[0] = (char) (i >>> 24);
89         ch[1] = (char) ((i >> 16) & 0x00ff);
90         ch[2] = (char) ((i >> 8) & 0x00ff);
91         ch[3] = (char) (i & 0x00ff);
92 
93         return new String(ch);
94     }
95 }
96 
97