Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 1 | |
| 2 | import java.io.*; |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 3 | import java.nio.ByteOrder; |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 4 | import java.util.*; |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 5 | import libcore.io.BufferIterator; |
| 6 | import libcore.util.ZoneInfo; |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 7 | |
Elliott Hughes | 328a484 | 2012-10-19 13:03:52 -0700 | [diff] [blame] | 8 | // usage: java ZoneCompiler <setup file> <data directory> <output directory> <tzdata version> |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 9 | // |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 10 | // Compile a set of tzfile-formatted files into a single file containing an index. |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 11 | // |
| 12 | // The compilation is controlled by a setup file, which is provided as a |
| 13 | // command-line argument. The setup file has the form: |
| 14 | // |
| 15 | // Link <toName> <fromName> |
| 16 | // ... |
| 17 | // <zone filename> |
| 18 | // ... |
| 19 | // |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 20 | // Note that the links must be declared prior to the zone names. |
| 21 | // A zone name is a filename relative to the source directory such as |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 22 | // 'GMT', 'Africa/Dakar', or 'America/Argentina/Jujuy'. |
| 23 | // |
| 24 | // Use the 'zic' command-line tool to convert from flat files |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 25 | // (such as 'africa' or 'northamerica') to a directory |
| 26 | // hierarchy suitable for this tool (containing files such as 'data/Africa/Abidjan'). |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 27 | // |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 28 | |
| 29 | public class ZoneCompactor { |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 30 | public static class ByteArrayBufferIteratorBE extends BufferIterator { |
| 31 | private final byte[] bytes; |
| 32 | private int offset = 0; |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 33 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 34 | public ByteArrayBufferIteratorBE(byte[] bytes) { |
| 35 | this.bytes = bytes; |
| 36 | this.offset = 0; |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 37 | } |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 38 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 39 | public void seek(int offset) { |
| 40 | this.offset = offset; |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 41 | } |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 43 | public void skip(int byteCount) { |
| 44 | this.offset += byteCount; |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 47 | public void readByteArray(byte[] dst, int dstOffset, int byteCount) { |
| 48 | System.arraycopy(bytes, offset, dst, dstOffset, byteCount); |
| 49 | offset += byteCount; |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 52 | public byte readByte() { |
| 53 | return bytes[offset++]; |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 54 | } |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame^] | 55 | |
| 56 | public int readInt() { |
| 57 | return ((readByte() & 0xff) << 24) | ((readByte() & 0xff) << 16) | ((readByte() & 0xff) << 8) | (readByte() & 0xff); |
| 58 | } |
| 59 | |
| 60 | public void readIntArray(int[] dst, int dstOffset, int intCount) { |
| 61 | for (int i = 0; i < intCount; ++i) { |
| 62 | dst[dstOffset++] = readInt(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public short readShort() { |
| 67 | throw new UnsupportedOperationException(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Maximum number of characters in a zone name, including '\0' terminator |
| 72 | private static final int MAXNAME = 40; |
| 73 | |
| 74 | // Zone name synonyms |
| 75 | private Map<String,String> links = new HashMap<String,String>(); |
| 76 | |
| 77 | // File starting bytes by zone name |
| 78 | private Map<String,Integer> starts = new HashMap<String,Integer>(); |
| 79 | |
| 80 | // File lengths by zone name |
| 81 | private Map<String,Integer> lengths = new HashMap<String,Integer>(); |
| 82 | |
| 83 | // Raw GMT offsets by zone name |
| 84 | private Map<String,Integer> offsets = new HashMap<String,Integer>(); |
| 85 | private int start = 0; |
| 86 | |
| 87 | // Concatenate the contents of 'inFile' onto 'out' |
| 88 | // and return the contents as a byte array. |
| 89 | private static byte[] copyFile(File inFile, OutputStream out) throws Exception { |
| 90 | byte[] ret = new byte[0]; |
| 91 | |
| 92 | InputStream in = new FileInputStream(inFile); |
| 93 | byte[] buf = new byte[8192]; |
| 94 | while (true) { |
| 95 | int nbytes = in.read(buf); |
| 96 | if (nbytes == -1) { |
| 97 | break; |
| 98 | } |
| 99 | out.write(buf, 0, nbytes); |
| 100 | |
| 101 | byte[] nret = new byte[ret.length + nbytes]; |
| 102 | System.arraycopy(ret, 0, nret, 0, ret.length); |
| 103 | System.arraycopy(buf, 0, nret, ret.length, nbytes); |
| 104 | ret = nret; |
| 105 | } |
| 106 | out.flush(); |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | public ZoneCompactor(String setupFile, String dataDirectory, String outputDirectory, String version) throws Exception { |
| 111 | // Read the setup file, and concatenate all the data. |
| 112 | ByteArrayOutputStream allData = new ByteArrayOutputStream(); |
| 113 | BufferedReader reader = new BufferedReader(new FileReader(setupFile)); |
| 114 | String s; |
| 115 | while ((s = reader.readLine()) != null) { |
| 116 | s = s.trim(); |
| 117 | if (s.startsWith("Link")) { |
| 118 | StringTokenizer st = new StringTokenizer(s); |
| 119 | st.nextToken(); |
| 120 | String to = st.nextToken(); |
| 121 | String from = st.nextToken(); |
| 122 | links.put(from, to); |
| 123 | } else { |
| 124 | String link = links.get(s); |
| 125 | if (link == null) { |
| 126 | File sourceFile = new File(dataDirectory, s); |
| 127 | long length = sourceFile.length(); |
| 128 | starts.put(s, start); |
| 129 | lengths.put(s, (int) length); |
| 130 | |
| 131 | start += length; |
| 132 | byte[] data = copyFile(sourceFile, allData); |
| 133 | |
| 134 | BufferIterator it = new ByteArrayBufferIteratorBE(data); |
| 135 | TimeZone tz = ZoneInfo.makeTimeZone(s, it); |
| 136 | int gmtOffset = tz.getRawOffset(); |
| 137 | offsets.put(s, gmtOffset); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Fill in fields for links. |
| 143 | Iterator<String> it = links.keySet().iterator(); |
| 144 | while (it.hasNext()) { |
| 145 | String from = it.next(); |
| 146 | String to = links.get(from); |
| 147 | |
| 148 | starts.put(from, starts.get(to)); |
| 149 | lengths.put(from, lengths.get(to)); |
| 150 | offsets.put(from, offsets.get(to)); |
| 151 | } |
| 152 | |
| 153 | // Create/truncate the destination file. |
| 154 | RandomAccessFile f = new RandomAccessFile(new File(outputDirectory, "tzdata"), "rw"); |
| 155 | f.setLength(0); |
| 156 | |
| 157 | // Write the header. |
| 158 | |
| 159 | // byte[12] tzdata_version -- 'tzdata2012f\0' |
| 160 | // int file_format_version -- probably won't need this, but just in case |
| 161 | // int index_offset -- likewise |
| 162 | // int data_offset |
| 163 | // int zonetab_offset |
| 164 | |
| 165 | // tzdata_version |
| 166 | f.write(toAscii(new byte[12], version)); |
| 167 | |
| 168 | // file_format_version |
| 169 | f.writeInt(1); |
| 170 | |
| 171 | // Write dummy values for the three offsets, and remember where we need to seek back to later |
| 172 | // when we have the real values. |
| 173 | int index_offset_offset = (int) f.getFilePointer(); |
| 174 | f.writeInt(0); |
| 175 | int data_offset_offset = (int) f.getFilePointer(); |
| 176 | f.writeInt(0); |
| 177 | int zonetab_offset_offset = (int) f.getFilePointer(); |
| 178 | f.writeInt(0); |
| 179 | |
| 180 | int index_offset = (int) f.getFilePointer(); |
| 181 | |
| 182 | // Write the index. |
| 183 | ArrayList<String> sortedOlsonIds = new ArrayList<String>(); |
| 184 | sortedOlsonIds.addAll(starts.keySet()); |
| 185 | Collections.sort(sortedOlsonIds); |
| 186 | it = sortedOlsonIds.iterator(); |
| 187 | while (it.hasNext()) { |
| 188 | String zoneName = it.next(); |
| 189 | if (zoneName.length() >= MAXNAME) { |
| 190 | throw new RuntimeException("zone filename too long: " + zoneName.length()); |
| 191 | } |
| 192 | |
| 193 | f.write(toAscii(new byte[MAXNAME], zoneName)); |
| 194 | f.writeInt(starts.get(zoneName)); |
| 195 | f.writeInt(lengths.get(zoneName)); |
| 196 | f.writeInt(offsets.get(zoneName)); |
| 197 | } |
| 198 | |
| 199 | int data_offset = (int) f.getFilePointer(); |
| 200 | |
| 201 | // Write the data. |
| 202 | f.write(allData.toByteArray()); |
| 203 | |
| 204 | // TODO: append the zonetab. |
| 205 | int zonetab_offset = 0; |
| 206 | |
| 207 | // Go back and fix up the offsets in the header. |
| 208 | f.seek(index_offset_offset); |
| 209 | f.writeInt(index_offset); |
| 210 | f.seek(data_offset_offset); |
| 211 | f.writeInt(data_offset); |
| 212 | f.seek(zonetab_offset_offset); |
| 213 | f.writeInt(zonetab_offset); |
| 214 | |
| 215 | f.close(); |
| 216 | } |
| 217 | |
| 218 | private static byte[] toAscii(byte[] dst, String src) { |
| 219 | for (int i = 0; i < src.length(); ++i) { |
| 220 | if (src.charAt(i) > '~') { |
| 221 | throw new RuntimeException("non-ASCII string: " + src); |
| 222 | } |
| 223 | dst[i] = (byte) src.charAt(i); |
| 224 | } |
| 225 | return dst; |
| 226 | } |
| 227 | |
| 228 | public static void main(String[] args) throws Exception { |
| 229 | if (args.length != 4) { |
| 230 | System.err.println("usage: java ZoneCompactor <setup file> <data directory> <output directory> <tzdata version>"); |
| 231 | System.exit(0); |
| 232 | } |
| 233 | new ZoneCompactor(args[0], args[1], args[2], args[3]); |
| 234 | } |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 235 | } |