• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 import java.io.*;
3 import java.util.*;
4 
5 // usage: java ZoneCompiler <setup file> <top-level directory>
6 //
7 // Compile a set of tzfile-formatted files into a single file plus
8 // an index file.
9 //
10 // The compilation is controlled by a setup file, which is provided as a
11 // command-line argument.  The setup file has the form:
12 //
13 // Link <toName> <fromName>
14 // ...
15 // <zone filename>
16 // ...
17 //
18 // Note that the links must be declared prior to the zone names.  A
19 // zone name is a filename relative to the source directory such as
20 // 'GMT', 'Africa/Dakar', or 'America/Argentina/Jujuy'.
21 //
22 // Use the 'zic' command-line tool to convert from flat files
23 // (e.g., 'africa', 'northamerica') into a suitable source directory
24 // hierarchy for this tool (e.g., 'data/Africa/Abidjan').
25 //
26 // Example:
27 //     zic -d data tz2007h
28 //     javac ZoneCompactor.java
29 //     java ZoneCompactor setup data
30 //     <produces zoneinfo.dat and zoneinfo.idx>
31 
32 public class ZoneCompactor {
33 
34     // Zone name synonyms
35     Map<String,String> links = new HashMap<String,String>();
36 
37     // File starting bytes by zone name
38     Map<String,Integer> starts = new HashMap<String,Integer>();
39 
40     // File lengths by zone name
41     Map<String,Integer> lengths = new HashMap<String,Integer>();
42 
43     // Raw GMT offsets by zone name
44     Map<String,Integer> offsets = new HashMap<String,Integer>();
45     int start = 0;
46 
47     // Maximum number of characters in a zone name, including '\0' terminator
48     private static final int MAXNAME = 40;
49 
50     // Concatenate the contents of 'inFile' onto 'out'
copyFile(File inFile, OutputStream out)51     private static void copyFile(File inFile, OutputStream out)
52         throws Exception {
53         InputStream in = new FileInputStream(inFile);
54         byte[] buf = new byte[8192];
55         while (true) {
56             int nbytes = in.read(buf);
57             if (nbytes == -1) {
58                 break;
59             }
60             out.write(buf, 0, nbytes);
61         }
62         out.flush();
63         return;
64     }
65 
66     // Write a 32-bit integer in network byte order
writeInt(OutputStream os, int x)67     private void writeInt(OutputStream os, int x) throws IOException {
68         os.write((x >> 24) & 0xff);
69         os.write((x >> 16) & 0xff);
70         os.write((x >>  8) & 0xff);
71         os.write( x        & 0xff);
72     }
73 
ZoneCompactor(String setupFilename, String dirName)74     public ZoneCompactor(String setupFilename, String dirName)
75         throws Exception {
76         File zoneInfoFile = new File("zoneinfo.dat");
77         zoneInfoFile.delete();
78         OutputStream zoneInfo = new FileOutputStream(zoneInfoFile);
79 
80         BufferedReader rdr = new BufferedReader(new FileReader(setupFilename));
81 
82         String s;
83         while ((s = rdr.readLine()) != null) {
84             s = s.trim();
85             if (s.startsWith("Link")) {
86                 StringTokenizer st = new StringTokenizer(s);
87                 st.nextToken();
88                 String to = st.nextToken();
89                 String from = st.nextToken();
90                 links.put(from, to);
91             } else {
92                 String link = links.get(s);
93                 if (link == null) {
94                     File f = new File(dirName, s);
95                     long length = f.length();
96                     starts.put(s, new Integer(start));
97                     lengths.put(s, new Integer((int)length));
98 
99                     TimeZone tz = TimeZone.getTimeZone(s);
100                     int gmtOffset = tz.getRawOffset();
101                     offsets.put(s, new Integer(gmtOffset));
102 
103                     start += length;
104                     copyFile(f, zoneInfo);
105                 }
106             }
107         }
108         zoneInfo.close();
109 
110         // Fill in fields for links
111         Iterator<String> iter = links.keySet().iterator();
112         while (iter.hasNext()) {
113             String from = iter.next();
114             String to = links.get(from);
115 
116             starts.put(from, starts.get(to));
117             lengths.put(from, lengths.get(to));
118             offsets.put(from, offsets.get(to));
119         }
120 
121         File idxFile = new File("zoneinfo.idx");
122         idxFile.delete();
123         FileOutputStream idx = new FileOutputStream(idxFile);
124 
125         ArrayList l = new ArrayList();
126         l.addAll(starts.keySet());
127         Collections.sort(l);
128         Iterator<String> ziter = l.iterator();
129         while (ziter.hasNext()) {
130             String zname = ziter.next();
131             if (zname.length() >= MAXNAME) {
132                 System.err.println("Error - zone filename exceeds " +
133                                    (MAXNAME - 1) + " characters!");
134             }
135 
136             byte[] znameBuf = new byte[MAXNAME];
137             for (int i = 0; i < zname.length(); i++) {
138                 znameBuf[i] = (byte)zname.charAt(i);
139             }
140             idx.write(znameBuf);
141             writeInt(idx, starts.get(zname).intValue());
142             writeInt(idx, lengths.get(zname).intValue());
143             writeInt(idx, offsets.get(zname).intValue());
144         }
145         idx.close();
146 
147         // System.out.println("maxLength = " + maxLength);
148     }
149 
main(String[] args)150     public static void main(String[] args) throws Exception {
151         if (args.length != 2) {
152             System.err.println("usage: java ZoneCompactor <setup> <data dir>");
153             System.exit(0);
154         }
155         new ZoneCompactor(args[0], args[1]);
156     }
157 
158 }
159