Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/calendar/zi/ZoneInfoFile.java
38855 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.File;26import java.io.FileInputStream;27import java.io.FileNotFoundException;28import java.io.IOException;29import java.lang.ref.SoftReference;30import java.nio.file.FileSystems;31import java.util.ArrayList;32import java.util.HashMap;33import java.util.List;34import java.util.Map;3536import sun.util.calendar.*;3738/**39* <code>ZoneInfoFile</code> reads Zone information files in the40* <java.home>/lib/zi directory and provides time zone41* information in the form of a {@link ZoneInfo} object. Also, it42* reads the ZoneInfoMappings file to obtain time zone IDs information43* that is used by the {@link ZoneInfo} class. The directory layout44* and data file formats are as follows.45*46* <p><strong>Directory layout</strong><p>47*48* All zone data files and ZoneInfoMappings are put under the49* <java.home>/lib/zi directory. A path name for a given time50* zone ID is a concatenation of <java.home>/lib/zi/ and the51* time zone ID. (The file separator is replaced with the platform52* dependent value. e.g., '\' for Win32.) An example layout will look53* like as follows.54* <blockquote>55* <pre>56* <java.home>/lib/zi/Africa/Addis_Ababa57* /Africa/Dakar58* /America/Los_Angeles59* /Asia/Singapore60* /EET61* /Europe/Oslo62* /GMT63* /Pacific/Galapagos64* ...65* /ZoneInfoMappings66* </pre>67* </blockquote>68*69* A zone data file has specific information of each zone.70* <code>ZoneInfoMappings</code> has global information of zone IDs so71* that the information can be obtained without instantiating all time72* zones.73*74* <p><strong>File format</strong><p>75*76* Two binary-file formats based on a simple Tag-Length-Value format are used77* to describe TimeZone information. The generic format of a data file is:78* <blockquote>79* <pre>80* DataFile {81* u1 magic[7];82* u1 version;83* data_item data[];84* }85* </pre>86* </blockquote>87* where <code>magic</code> is a magic number identifying a file88* format, <code>version</code> is the format version number, and89* <code>data</code> is one or more <code>data_item</code>s. The90* <code>data_item</code> structure is:91* <blockquote>92* <pre>93* data_item {94* u1 tag;95* u2 length;96* u1 value[length];97* }98* </pre>99* </blockquote>100* where <code>tag</code> indicates the data type of the item,101* <code>length</code> is a byte count of the following102* <code>value</code> that is the content of item data.103* <p>104* All data is stored in the big-endian order. There is no boundary105* alignment between date items.106*107* <p><strong>1. ZoneInfo data file</strong><p>108*109* Each ZoneInfo data file consists of the following members.110* <br>111* <blockquote>112* <pre>113* ZoneInfoDataFile {114* u1 magic[7];115* u1 version;116* SET OF<sup>1</sup> {117* transition transitions<sup>2</sup>;118* offset_table offsets<sup>2</sup>;119* simpletimezone stzparams<sup>2</sup>;120* raw_offset rawoffset;121* dstsaving dst;122* checksum crc32;123* gmtoffsetwillchange gmtflag<sup>2</sup>;124* }125* }126* 1: an unordered collection of zero or one occurrences of each item127* 2: optional item128* </pre>129* </blockquote>130* <code>magic</code> is a byte-string constant identifying the131* ZoneInfo data file. This field must be <code>"javazi\0"</code>132* defined as {@link #JAVAZI_LABEL}.133* <p>134* <code>version</code> is the version number of the file format. This135* will be used for compatibility check. This field must be136* <code>0x01</code> in this version.137* <p>138* <code>transition</code>, <code>offset_table</code> and139* <code>simpletimezone</code> have information of time transition140* from the past to the future. Therefore, these structures don't141* exist if the zone didn't change zone names and haven't applied DST in142* the past, and haven't planned to apply it. (e.g. Asia/Tokyo zone)143* <p>144* <code>raw_offset</code>, <code>dstsaving</code> and <code>checksum</code>145* exist in every zoneinfo file. They are used by TimeZone.class indirectly.146*147* <p><strong>1.1 <code>transition</code> structure</strong><p><a name="transition"></a>148* <blockquote>149* <pre>150* transition {151* u1 tag; // 0x04 : constant152* u2 length; // byte length of whole values153* s8 value[length/8]; // transitions in `long'154* }155* </pre>156* </blockquote>157* See {@link ZoneInfo#transitions ZoneInfo.transitions} about the value.158*159* <p><strong>1.2 <code>offset_table</code> structure</strong><p>160* <blockquote>161* <pre>162* offset_table {163* u1 tag; // 0x05 : constant164* u2 length; // byte length of whole values165* s4 value[length/4]; // offset values in `int'166* }167* </pre>168* </blockquote>169*170* <p><strong>1.3 <code>simpletimezone</code> structure</strong><p>171* See {@link ZoneInfo#simpleTimeZoneParams ZoneInfo.simpleTimeZoneParams}172* about the value.173* <blockquote>174* <pre>175* simpletimezone {176* u1 tag; // 0x06 : constant177* u2 length; // byte length of whole values178* s4 value[length/4]; // SimpleTimeZone parameters179* }180* </pre>181* </blockquote>182* See {@link ZoneInfo#offsets ZoneInfo.offsets} about the value.183*184* <p><strong>1.4 <code>raw_offset</code> structure</strong><p>185* <blockquote>186* <pre>187* raw_offset {188* u1 tag; // 0x01 : constant189* u2 length; // must be 4.190* s4 value; // raw GMT offset [millisecond]191* }192* </pre>193* </blockquote>194* See {@link ZoneInfo#rawOffset ZoneInfo.rawOffset} about the value.195*196* <p><strong>1.5 <code>dstsaving</code> structure</strong><p>197* Value has dstSaving in seconds.198* <blockquote>199* <pre>200* dstsaving {201* u1 tag; // 0x02 : constant202* u2 length; // must be 2.203* s2 value; // DST save value [second]204* }205* </pre>206* </blockquote>207* See {@link ZoneInfo#dstSavings ZoneInfo.dstSavings} about value.208*209* <p><strong>1.6 <code>checksum</code> structure</strong><p>210* <blockquote>211* <pre>212* checksum {213* u1 tag; // 0x03 : constant214* u2 length; // must be 4.215* s4 value; // CRC32 value of transitions216* }217* </pre>218* </blockquote>219* See {@link ZoneInfo#checksum ZoneInfo.checksum}.220*221* <p><strong>1.7 <code>gmtoffsetwillchange</code> structure</strong><p>222* This record has a flag value for {@link ZoneInfo#rawOffsetWillChange}.223* If this record is not present in a zoneinfo file, 0 is assumed for224* the value.225* <blockquote>226* <pre>227* gmtoffsetwillchange {228* u1 tag; // 0x07 : constant229* u2 length; // must be 1.230* u1 value; // 1: if the GMT raw offset will change231* // in the future, 0, otherwise.232* }233* </pre>234* </blockquote>235*236*237* <p><strong>2. ZoneInfoMappings file</strong><p>238*239* The ZoneInfoMappings file consists of the following members.240* <br>241* <blockquote>242* <pre>243* ZoneInfoMappings {244* u1 magic[7];245* u1 version;246* SET OF {247* versionName version;248* zone_id_table zoneIDs;249* raw_offset_table rawoffsets;250* raw_offset_index_table rawoffsetindices;251* alias_table aliases;252* excluded_list excludedList;253* }254* }255* </pre>256* </blockquote>257*258* <code>magic</code> is a byte-string constant which has the file type.259* This field must be <code>"javazm\0"</code> defined as {@link #JAVAZM_LABEL}.260* <p>261* <code>version</code> is the version number of this file262* format. This will be used for compatibility check. This field must263* be <code>0x01</code> in this version.264* <p>265* <code>versionName</code> shows which version of Olson's data has been used266* to generate this ZoneInfoMappings. (e.g. <code>tzdata2000g</code>) <br>267* This field is for trouble-shooting and isn't usually used in runtime.268* <p>269* <code>zone_id_table</code>, <code>raw_offset_index_table</code> and270* <code>alias_table</code> are general information of supported271* zones.272*273* <p><strong>2.1 <code>zone_id_table</code> structure</strong><p>274* The list of zone IDs included in the zi database. The list does275* <em>not</em> include zone IDs, if any, listed in excludedList.276* <br>277* <blockquote>278* <pre>279* zone_id_table {280* u1 tag; // 0x40 : constant281* u2 length; // byte length of whole values282* u2 zone_id_count;283* zone_id value[zone_id_count];284* }285*286* zone_id {287* u1 byte_length; // byte length of id288* u1 id[byte_length]; // zone name string289* }290* </pre>291* </blockquote>292*293* <p><strong>2.2 <code>raw_offset_table</code> structure</strong><p>294* <br>295* <blockquote>296* <pre>297* raw_offset_table {298* u1 tag; // 0x41 : constant299* u2 length; // byte length of whole values300* s4 value[length/4]; // raw GMT offset in milliseconds301* }302* </pre>303* </blockquote>304*305* <p><strong>2.3 <code>raw_offset_index_table</code> structure</strong><p>306* <br>307* <blockquote>308* <pre>309* raw_offset_index_table {310* u1 tag; // 0x42 : constant311* u2 length; // byte length of whole values312* u1 value[length];313* }314* </pre>315* </blockquote>316*317* <p><strong>2.4 <code>alias_table</code> structure</strong><p>318* <br>319* <blockquote>320* <pre>321* alias_table {322* u1 tag; // 0x43 : constant323* u2 length; // byte length of whole values324* u2 nentries; // number of id-pairs325* id_pair value[nentries];326* }327*328* id_pair {329* zone_id aliasname;330* zone_id ID;331* }332* </pre>333* </blockquote>334*335* <p><strong>2.5 <code>versionName</code> structure</strong><p>336* <br>337* <blockquote>338* <pre>339* versionName {340* u1 tag; // 0x44 : constant341* u2 length; // byte length of whole values342* u1 value[length];343* }344* </pre>345* </blockquote>346*347* <p><strong>2.6 <code>excludeList</code> structure</strong><p>348* The list of zone IDs whose zones will change their GMT offsets349* (a.k.a. raw offsets) some time in the future. Those IDs must be350* added to the list of zone IDs for getAvailableIDs(). Also they must351* be examined for getAvailableIDs(int) to determine the352* <em>current</em> GMT offsets.353* <br>354* <blockquote>355* <pre>356* excluded_list {357* u1 tag; // 0x45 : constant358* u2 length; // byte length of whole values359* u2 nentries; // number of zone_ids360* zone_id value[nentries]; // excluded zone IDs361* }362* </pre>363* </blockquote>364*365* @since 1.4366*/367368public class ZoneInfoFile {369370/**371* The magic number for the ZoneInfo data file format.372*/373public static final byte[] JAVAZI_LABEL = {374(byte)'j', (byte)'a', (byte)'v', (byte)'a', (byte)'z', (byte)'i', (byte)'\0'375};376private static final int JAVAZI_LABEL_LENGTH = JAVAZI_LABEL.length;377378/**379* The ZoneInfo data file format version number. Must increase380* one when any incompatible change has been made.381*/382public static final byte JAVAZI_VERSION = 0x01;383384/**385* Raw offset data item tag.386*/387public static final byte TAG_RawOffset = 1;388389/**390* Known last Daylight Saving Time save value data item tag.391*/392public static final byte TAG_LastDSTSaving = 2;393394/**395* Checksum data item tag.396*/397public static final byte TAG_CRC32 = 3;398399/**400* Transition data item tag.401*/402public static final byte TAG_Transition = 4;403404/**405* Offset table data item tag.406*/407public static final byte TAG_Offset = 5;408409/**410* SimpleTimeZone parameters data item tag.411*/412public static final byte TAG_SimpleTimeZone = 6;413414/**415* Raw GMT offset will change in the future.416*/417public static final byte TAG_GMTOffsetWillChange = 7;418419420/**421* The ZoneInfoMappings file name.422*/423public static final String JAVAZM_FILE_NAME = "ZoneInfoMappings";424425/**426* The magic number for the ZoneInfoMappings file format.427*/428public static final byte[] JAVAZM_LABEL = {429(byte)'j', (byte)'a', (byte)'v', (byte)'a', (byte)'z', (byte)'m', (byte)'\0'430};431private static final int JAVAZM_LABEL_LENGTH = JAVAZM_LABEL.length;432433/**434* The ZoneInfoMappings file format version number. Must increase435* one when any incompatible change has been made.436*/437public static final byte JAVAZM_VERSION = 0x01;438439/**440* Time zone IDs data item tag.441*/442public static final byte TAG_ZoneIDs = 64;443444/**445* Raw GMT offsets table data item tag.446*/447public static final byte TAG_RawOffsets = 65;448449/**450* Indices to the raw GMT offset table data item tag.451*/452public static final byte TAG_RawOffsetIndices = 66;453454/**455* Time zone aliases table data item tag.456*/457public static final byte TAG_ZoneAliases = 67;458459/**460* Olson's public zone information version tag.461*/462public static final byte TAG_TZDataVersion = 68;463464/**465* Excluded zones item tag. (Added in Mustang)466*/467public static final byte TAG_ExcludedZones = 69;468469private static Map<String, ZoneInfoOld> zoneInfoObjects = null;470471private static final ZoneInfoOld GMT = new ZoneInfoOld("GMT", 0);472473static String ziDir;474475/**476* Converts the given time zone ID to a platform dependent path477* name. For example, "America/Los_Angeles" is converted to478* "America\Los_Angeles" on Win32.479* @return a modified ID replacing '/' with {@link480* java.io.File#separatorChar File.separatorChar} if needed.481*/482public static String getFileName(String ID) {483if (File.separatorChar == '/') {484return ID;485}486return ID.replace('/', File.separatorChar);487}488489/**490* Gets a ZoneInfo with the given GMT offset. The object491* has its ID in the format of GMT{+|-}hh:mm.492*493* @param originalId the given custom id (before normalized such as "GMT+9")494* @param gmtOffset GMT offset <em>in milliseconds</em>495* @return a ZoneInfo constructed with the given GMT offset496*/497public static ZoneInfoOld getCustomTimeZone(String originalId, int gmtOffset) {498String id = toCustomID(gmtOffset);499500ZoneInfoOld zi = getFromCache(id);501if (zi == null) {502zi = new ZoneInfoOld(id, gmtOffset);503zi = addToCache(id, zi);504if (!id.equals(originalId)) {505zi = addToCache(originalId, zi);506}507}508return (ZoneInfoOld) zi.clone();509}510511public static String toCustomID(int gmtOffset) {512char sign;513int offset = gmtOffset / 60000;514515if (offset >= 0) {516sign = '+';517} else {518sign = '-';519offset = -offset;520}521int hh = offset / 60;522int mm = offset % 60;523524char[] buf = new char[] { 'G', 'M', 'T', sign, '0', '0', ':', '0', '0' };525if (hh >= 10) {526buf[4] += hh / 10;527}528buf[5] += hh % 10;529if (mm != 0) {530buf[7] += mm / 10;531buf[8] += mm % 10;532}533return new String(buf);534}535536/**537* @return a ZoneInfo instance created for the specified id, or538* null if there is no time zone data file found for the specified539* id.540*/541public static ZoneInfoOld getZoneInfoOld(String id) {542//treat GMT zone as special543if ("GMT".equals(id))544return (ZoneInfoOld) GMT.clone();545ZoneInfoOld zi = getFromCache(id);546if (zi == null) {547Map<String, String> aliases = ZoneInfoOld.getCachedAliasTable();548if (aliases != null && aliases.get(id) != null) {549return null;550}551zi = createZoneInfoOld(id);552if (zi == null) {553return null;554}555zi = addToCache(id, zi);556}557return (ZoneInfoOld) zi.clone();558}559560synchronized static ZoneInfoOld getFromCache(String id) {561if (zoneInfoObjects == null) {562return null;563}564return zoneInfoObjects.get(id);565}566567synchronized static ZoneInfoOld addToCache(String id, ZoneInfoOld zi) {568if (zoneInfoObjects == null) {569zoneInfoObjects = new HashMap<>();570} else {571ZoneInfoOld zone = zoneInfoObjects.get(id);572if (zone != null) {573return zone;574}575}576zoneInfoObjects.put(id, zi);577return zi;578}579580private static ZoneInfoOld createZoneInfoOld(String id) {581byte[] buf = readZoneInfoFile(getFileName(id));582if (buf == null) {583return null;584}585586int index = 0;587int filesize = buf.length;588int rawOffset = 0;589int dstSavings = 0;590int checksum = 0;591boolean willGMTOffsetChange = false;592long[] transitions = null;593int[] offsets = null;594int[] simpleTimeZoneParams = null;595596try {597for (index = 0; index < JAVAZI_LABEL.length; index++) {598if (buf[index] != JAVAZI_LABEL[index]) {599System.err.println("ZoneInfoOld: wrong magic number: " + id);600return null;601}602}603if (buf[index++] > JAVAZI_VERSION) {604System.err.println("ZoneInfo: incompatible version ("605+ buf[index - 1] + "): " + id);606return null;607}608609while (index < filesize) {610byte tag = buf[index++];611int len = ((buf[index++] & 0xFF) << 8) + (buf[index++] & 0xFF);612613if (filesize < index+len) {614break;615}616617switch (tag) {618case TAG_CRC32:619{620int val = buf[index++] & 0xff;621val = (val << 8) + (buf[index++] & 0xff);622val = (val << 8) + (buf[index++] & 0xff);623val = (val << 8) + (buf[index++] & 0xff);624checksum = val;625}626break;627628case TAG_LastDSTSaving:629{630short val = (short)(buf[index++] & 0xff);631val = (short)((val << 8) + (buf[index++] & 0xff));632dstSavings = val * 1000;633}634break;635636case TAG_RawOffset:637{638int val = buf[index++] & 0xff;639val = (val << 8) + (buf[index++] & 0xff);640val = (val << 8) + (buf[index++] & 0xff);641val = (val << 8) + (buf[index++] & 0xff);642rawOffset = val;643}644break;645646case TAG_Transition:647{648int n = len / 8;649transitions = new long[n];650for (int i = 0; i < n; i ++) {651long val = buf[index++] & 0xff;652val = (val << 8) + (buf[index++] & 0xff);653val = (val << 8) + (buf[index++] & 0xff);654val = (val << 8) + (buf[index++] & 0xff);655val = (val << 8) + (buf[index++] & 0xff);656val = (val << 8) + (buf[index++] & 0xff);657val = (val << 8) + (buf[index++] & 0xff);658val = (val << 8) + (buf[index++] & 0xff);659transitions[i] = val;660}661}662break;663664case TAG_Offset:665{666int n = len / 4;667offsets = new int[n];668for (int i = 0; i < n; i ++) {669int val = buf[index++] & 0xff;670val = (val << 8) + (buf[index++] & 0xff);671val = (val << 8) + (buf[index++] & 0xff);672val = (val << 8) + (buf[index++] & 0xff);673offsets[i] = val;674}675}676break;677678case TAG_SimpleTimeZone:679{680if (len != 32 && len != 40) {681System.err.println("ZoneInfo: wrong SimpleTimeZone parameter size");682return null;683}684int n = len / 4;685simpleTimeZoneParams = new int[n];686for (int i = 0; i < n; i++) {687int val = buf[index++] & 0xff;688val = (val << 8) + (buf[index++] & 0xff);689val = (val << 8) + (buf[index++] & 0xff);690val = (val << 8) + (buf[index++] & 0xff);691simpleTimeZoneParams[i] = val;692}693}694break;695696case TAG_GMTOffsetWillChange:697{698if (len != 1) {699System.err.println("ZoneInfo: wrong byte length for TAG_GMTOffsetWillChange");700}701willGMTOffsetChange = buf[index++] == 1;702}703break;704705default:706System.err.println("ZoneInfo: unknown tag < " + tag + ">. ignored.");707index += len;708break;709}710}711} catch (Exception e) {712System.err.println("ZoneInfo: corrupted zoneinfo file: " + id);713return null;714}715716if (index != filesize) {717System.err.println("ZoneInfo: wrong file size: " + id);718return null;719}720721return new ZoneInfoOld(id, rawOffset, dstSavings, checksum,722transitions, offsets, simpleTimeZoneParams,723willGMTOffsetChange);724}725726private volatile static SoftReference<List<String>> zoneIDs = null;727728static List<String> getZoneIDs() {729List<String> ids = null;730SoftReference<List<String>> cache = zoneIDs;731if (cache != null) {732ids = cache.get();733if (ids != null) {734return ids;735}736}737byte[] buf = null;738buf = getZoneInfoOldMappings();739int index = JAVAZM_LABEL_LENGTH + 1;740int filesize = buf.length;741try {742loop:743while (index < filesize) {744byte tag = buf[index++];745int len = ((buf[index++] & 0xFF) << 8) + (buf[index++] & 0xFF);746747switch (tag) {748case TAG_ZoneIDs:749{750int n = (buf[index++] << 8) + (buf[index++] & 0xFF);751ids = new ArrayList<>(n);752753for (int i = 0; i < n; i++) {754byte m = buf[index++];755ids.add(new String(buf, index, m, "UTF-8"));756index += m;757}758}759break loop;760761default:762index += len;763break;764}765}766} catch (Exception e) {767System.err.println("ZoneInfoOld: corrupted " + JAVAZM_FILE_NAME);768}769770zoneIDs = new SoftReference<>(ids);771return ids;772}773774/**775* @return an alias table in HashMap where a key is an alias ID776* (e.g., "PST") and its value is a real time zone ID (e.g.,777* "America/Los_Angeles").778*/779static Map<String, String> getZoneAliases() {780byte[] buf = getZoneInfoOldMappings();781int index = JAVAZM_LABEL_LENGTH + 1;782int filesize = buf.length;783Map<String, String> aliases = null;784785try {786loop:787while (index < filesize) {788byte tag = buf[index++];789int len = ((buf[index++] & 0xFF) << 8) + (buf[index++] & 0xFF);790791switch (tag) {792case TAG_ZoneAliases:793{794int n = (buf[index++] << 8) + (buf[index++] & 0xFF);795aliases = new HashMap<>(n);796for (int i = 0; i < n; i++) {797byte m = buf[index++];798String name = new String(buf, index, m, "UTF-8");799index += m;800m = buf[index++];801String realName = new String(buf, index, m, "UTF-8");802index += m;803aliases.put(name, realName);804}805}806break loop;807808default:809index += len;810break;811}812}813} catch (Exception e) {814System.err.println("ZoneInfoOld: corrupted " + JAVAZM_FILE_NAME);815return null;816}817return aliases;818}819820private volatile static SoftReference<List<String>> excludedIDs = null;821private volatile static boolean hasNoExcludeList = false;822823/**824* @return a List of zone IDs for zones that will change their GMT825* offsets in some future time.826*827* @since 1.6828*/829static List<String> getExcludedZones() {830if (hasNoExcludeList) {831return null;832}833834List<String> excludeList = null;835836SoftReference<List<String>> cache = excludedIDs;837if (cache != null) {838excludeList = cache.get();839if (excludeList != null) {840return excludeList;841}842}843844byte[] buf = getZoneInfoOldMappings();845int index = JAVAZM_LABEL_LENGTH + 1;846int filesize = buf.length;847848try {849loop:850while (index < filesize) {851byte tag = buf[index++];852int len = ((buf[index++] & 0xFF) << 8) + (buf[index++] & 0xFF);853854switch (tag) {855case TAG_ExcludedZones:856{857int n = (buf[index++] << 8) + (buf[index++] & 0xFF);858excludeList = new ArrayList<>();859for (int i = 0; i < n; i++) {860byte m = buf[index++];861String name = new String(buf, index, m, "UTF-8");862index += m;863excludeList.add(name);864}865}866break loop;867868default:869index += len;870break;871}872}873} catch (Exception e) {874System.err.println("ZoneInfoOld: corrupted " + JAVAZM_FILE_NAME);875return null;876}877878if (excludeList != null) {879excludedIDs = new SoftReference<>(excludeList);880} else {881hasNoExcludeList = true;882}883return excludeList;884}885886private volatile static SoftReference<byte[]> rawOffsetIndices = null;887888static byte[] getRawOffsetIndices() {889byte[] indices = null;890891SoftReference<byte[]> cache = rawOffsetIndices;892if (cache != null) {893indices = cache.get();894if (indices != null) {895return indices;896}897}898899byte[] buf = getZoneInfoOldMappings();900int index = JAVAZM_LABEL_LENGTH + 1;901int filesize = buf.length;902903try {904loop:905while (index < filesize) {906byte tag = buf[index++];907int len = ((buf[index++] & 0xFF) << 8) + (buf[index++] & 0xFF);908909switch (tag) {910case TAG_RawOffsetIndices:911{912indices = new byte[len];913for (int i = 0; i < len; i++) {914indices[i] = buf[index++];915}916}917break loop;918919default:920index += len;921break;922}923}924} catch (ArrayIndexOutOfBoundsException e) {925System.err.println("ZoneInfoOld: corrupted " + JAVAZM_FILE_NAME);926}927928rawOffsetIndices = new SoftReference<>(indices);929return indices;930}931932private volatile static SoftReference<int[]> rawOffsets = null;933934static int[] getRawOffsets() {935int[] offsets = null;936937SoftReference<int[]> cache = rawOffsets;938if (cache != null) {939offsets = cache.get();940if (offsets != null) {941return offsets;942}943}944945byte[] buf = getZoneInfoOldMappings();946int index = JAVAZM_LABEL_LENGTH + 1;947int filesize = buf.length;948949try {950loop:951while (index < filesize) {952byte tag = buf[index++];953int len = ((buf[index++] & 0xFF) << 8) + (buf[index++] & 0xFF);954955switch (tag) {956case TAG_RawOffsets:957{958int n = len/4;959offsets = new int[n];960for (int i = 0; i < n; i++) {961int val = buf[index++] & 0xff;962val = (val << 8) + (buf[index++] & 0xff);963val = (val << 8) + (buf[index++] & 0xff);964val = (val << 8) + (buf[index++] & 0xff);965offsets[i] = val;966}967}968break loop;969970default:971index += len;972break;973}974}975} catch (ArrayIndexOutOfBoundsException e) {976System.err.println("ZoneInfoOld: corrupted " + JAVAZM_FILE_NAME);977}978979rawOffsets = new SoftReference<>(offsets);980return offsets;981}982983private volatile static SoftReference<byte[]> zoneInfoMappings = null;984985private static byte[] getZoneInfoOldMappings() {986byte[] data;987SoftReference<byte[]> cache = zoneInfoMappings;988if (cache != null) {989data = cache.get();990if (data != null) {991return data;992}993}994data = readZoneInfoFile(JAVAZM_FILE_NAME);995if (data == null) {996throw new RuntimeException("ZoneInfoOldMapping " +997JAVAZM_FILE_NAME + " either doesn't exist or doesn't have data");998}9991000int index;1001for (index = 0; index < JAVAZM_LABEL.length; index++) {1002if (data[index] != JAVAZM_LABEL[index]) {1003System.err.println("ZoneInfoOld: wrong magic number: " + JAVAZM_FILE_NAME);1004return null;1005}1006}1007if (data[index++] > JAVAZM_VERSION) {1008System.err.println("ZoneInfoOld: incompatible version ("1009+ data[index - 1] + "): " + JAVAZM_FILE_NAME);1010return null;1011}10121013zoneInfoMappings = new SoftReference<>(data);1014return data;1015}10161017/**1018* Reads the specified file under <java.home>/lib/zi into a buffer.1019* @return the buffer, or null if any I/O error occurred.1020*/1021private static byte[] readZoneInfoFile(final String fileName) {1022if (fileName.indexOf("..") >= 0) {1023return null;1024}1025byte[] buffer = null;1026File file = new File(ziDir, fileName);1027try {1028int filesize = (int)file.length();1029if (filesize > 0) {1030FileInputStream fis = new FileInputStream(file);1031buffer = new byte[filesize];1032try {1033if (fis.read(buffer) != filesize) {1034throw new IOException("read error on " + fileName);1035}1036} finally {1037fis.close();1038}1039}1040} catch (Exception ex) {1041if (!(ex instanceof FileNotFoundException) || JAVAZM_FILE_NAME.equals(fileName)) {1042System.err.println("ZoneInfoOld: " + ex.getMessage());1043}1044}1045return buffer;1046}10471048private ZoneInfoFile() {1049}1050}105110521053