Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/zone/TzdbZoneRulesProvider.java
38918 views
/*1* Copyright (c) 2012, 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*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time.zone;6263import java.io.ByteArrayInputStream;64import java.io.BufferedInputStream;65import java.io.DataInputStream;66import java.io.File;67import java.io.FileInputStream;68import java.io.IOException;69import java.io.StreamCorruptedException;70import java.util.Arrays;71import java.util.HashSet;72import java.util.List;73import java.util.Map;74import java.util.NavigableMap;75import java.util.Objects;76import java.util.Set;77import java.util.TreeMap;78import java.util.concurrent.ConcurrentHashMap;7980/**81* Loads time-zone rules for 'TZDB'.82*83* @since 1.884*/85final class TzdbZoneRulesProvider extends ZoneRulesProvider {8687/**88* All the regions that are available.89*/90private List<String> regionIds;91/**92* Version Id of this tzdb rules93*/94private String versionId;95/**96* Region to rules mapping97*/98private final Map<String, Object> regionToRules = new ConcurrentHashMap<>();99100/**101* Creates an instance.102* Created by the {@code ServiceLoader}.103*104* @throws ZoneRulesException if unable to load105*/106public TzdbZoneRulesProvider() {107try {108String libDir = System.getProperty("java.home") + File.separator + "lib";109try (DataInputStream dis = new DataInputStream(110new BufferedInputStream(new FileInputStream(111new File(libDir, "tzdb.dat"))))) {112load(dis);113}114} catch (Exception ex) {115throw new ZoneRulesException("Unable to load TZDB time-zone rules", ex);116}117}118119@Override120protected Set<String> provideZoneIds() {121return new HashSet<>(regionIds);122}123124@Override125protected ZoneRules provideRules(String zoneId, boolean forCaching) {126// forCaching flag is ignored because this is not a dynamic provider127Object obj = regionToRules.get(zoneId);128if (obj == null) {129throw new ZoneRulesException("Unknown time-zone ID: " + zoneId);130}131try {132if (obj instanceof byte[]) {133byte[] bytes = (byte[]) obj;134DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));135obj = Ser.read(dis);136regionToRules.put(zoneId, obj);137}138return (ZoneRules) obj;139} catch (Exception ex) {140throw new ZoneRulesException("Invalid binary time-zone data: TZDB:" + zoneId + ", version: " + versionId, ex);141}142}143144@Override145protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {146TreeMap<String, ZoneRules> map = new TreeMap<>();147ZoneRules rules = getRules(zoneId, false);148if (rules != null) {149map.put(versionId, rules);150}151return map;152}153154/**155* Loads the rules from a DateInputStream, often in a jar file.156*157* @param dis the DateInputStream to load, not null158* @throws Exception if an error occurs159*/160private void load(DataInputStream dis) throws Exception {161if (dis.readByte() != 1) {162throw new StreamCorruptedException("File format not recognised");163}164// group165String groupId = dis.readUTF();166if ("TZDB".equals(groupId) == false) {167throw new StreamCorruptedException("File format not recognised");168}169// versions170int versionCount = dis.readShort();171for (int i = 0; i < versionCount; i++) {172versionId = dis.readUTF();173}174// regions175int regionCount = dis.readShort();176String[] regionArray = new String[regionCount];177for (int i = 0; i < regionCount; i++) {178regionArray[i] = dis.readUTF();179}180regionIds = Arrays.asList(regionArray);181// rules182int ruleCount = dis.readShort();183Object[] ruleArray = new Object[ruleCount];184for (int i = 0; i < ruleCount; i++) {185byte[] bytes = new byte[dis.readShort()];186dis.readFully(bytes);187ruleArray[i] = bytes;188}189// link version-region-rules190for (int i = 0; i < versionCount; i++) {191int versionRegionCount = dis.readShort();192regionToRules.clear();193for (int j = 0; j < versionRegionCount; j++) {194String region = regionArray[dis.readShort()];195Object rule = ruleArray[dis.readShort() & 0xffff];196regionToRules.put(region, rule);197}198}199}200201@Override202public String toString() {203return "TZDB[" + versionId + "]";204}205}206207208