/** * Gets the provider for the zone ID. * * @param zoneId the zone region ID as used by {@code ZoneId}, not null * @return the provider, not null * @throws ZoneRulesException if the zone ID is unknown */ private static ZoneRulesProvider getProvider(String zoneId) { ZoneRulesProvider provider = ZONES.get(zoneId); if (provider == null) { if (ZONES.isEmpty()) { throw new ZoneRulesException("No time-zone data files registered"); } throw new ZoneRulesException("Unknown time-zone ID: " + zoneId); } return provider; }
/** * Gets the provider for the zone ID. * * @param zoneId the zone region ID as used by {@code ZoneId}, not null * @return the provider, not null * @throws ZoneRulesException if the zone ID is unknown */ private static ZoneRulesProvider getProvider(String zoneId) { ZoneRulesProvider provider = ZONES.get(zoneId); if (provider == null) { if (ZONES.isEmpty()) { throw new ZoneRulesException("No time-zone data files registered"); } throw new ZoneRulesException("Unknown time-zone ID: " + zoneId); } return provider; }
/** * Creates an instance. * Created by the {@code ServiceLoader}. * * @throws ZoneRulesException if unable to load */ public TzdbZoneRulesProvider() { super(); if (load(ZoneRulesProvider.class.getClassLoader()) == false) { throw new ZoneRulesException("No time-zone rules found for 'TZDB'"); } }
/** * Creates an instance and loads the specified URL. * <p> * This could be used to wrap this provider in another instance. * * @param url the URL to load, not null * @throws ZoneRulesException if unable to load */ public TzdbZoneRulesProvider(URL url) { super(); try { if (load(url) == false) { throw new ZoneRulesException("No time-zone rules found: " + url); } } catch (Exception ex) { throw new ZoneRulesException("Unable to load TZDB time-zone rules: " + url, ex); } }
/** * Creates an instance and loads the specified URL. * <p> * This could be used to wrap this provider in another instance. * * @param url the URL to load, not null * @throws ZoneRulesException if unable to load */ public TzdbZoneRulesProvider(URL url) { super(); try { if (load(url) == false) { throw new ZoneRulesException("No time-zone rules found: " + url); } } catch (Exception ex) { throw new ZoneRulesException("Unable to load TZDB time-zone rules: " + url, ex); } }
/** * Creates an instance. * Created by the {@code ServiceLoader}. * * @throws ZoneRulesException if unable to load */ public TzdbZoneRulesProvider() { super(); if (load(ZoneRulesProvider.class.getClassLoader()) == false) { throw new ZoneRulesException("No time-zone rules found for 'TZDB'"); } }
ZoneRules getRules(String regionId) { int regionIndex = Arrays.binarySearch(regionArray, regionId); if (regionIndex < 0) { return null; } try { return createRule(ruleIndices[regionIndex]); } catch (Exception ex) { throw new ZoneRulesException("Invalid binary time-zone data: TZDB:" + regionId + ", version: " + versionId, ex); } }
ZoneRules getRules(String regionId) { int regionIndex = Arrays.binarySearch(regionArray, regionId); if (regionIndex < 0) { return null; } try { return createRule(ruleIndices[regionIndex]); } catch (Exception ex) { throw new ZoneRulesException("Invalid binary time-zone data: TZDB:" + regionId + ", version: " + versionId, ex); } }
/** * Creates an instance and loads the specified input stream. * <p> * This could be used to wrap this provider in another instance. * * @param stream the stream to load, not null, not closed after use * @throws ZoneRulesException if unable to load */ public TzdbZoneRulesProvider(InputStream stream) { super(); try { load(stream); } catch (Exception ex) { throw new ZoneRulesException("Unable to load TZDB time-zone rules", ex); } }
/** * Creates an instance and loads the specified input stream. * <p> * This could be used to wrap this provider in another instance. * * @param stream the stream to load, not null, not closed after use * @throws ZoneRulesException if unable to load */ public TzdbZoneRulesProvider(InputStream stream) { super(); try { load(stream); } catch (Exception ex) { throw new ZoneRulesException("Unable to load TZDB time-zone rules", ex); } }
private ZoneRules loadData(String zoneId) { String fileName = "tzdb/" + zoneId + ".dat";; InputStream is = null; try { is = context.getAssets().open(fileName); return loadData(is); } catch (Exception ex) { throw new ZoneRulesException("Invalid binary time-zone data: " + fileName, ex); } finally { close(is); } }
/** * Loads the rules. * * @param classLoader the class loader to use, not null * @return true if updated * @throws ZoneRulesException if unable to load */ private boolean load(ClassLoader classLoader) { boolean updated = false; URL url = null; try { Enumeration<URL> en = classLoader.getResources("org/threeten/bp/TZDB.dat"); while (en.hasMoreElements()) { url = en.nextElement(); updated |= load(url); } } catch (Exception ex) { throw new ZoneRulesException("Unable to load TZDB time-zone rules: " + url, ex); } return updated; }
/** * Loads the rules. * * @param classLoader the class loader to use, not null * @return true if updated * @throws ZoneRulesException if unable to load */ private boolean load(ClassLoader classLoader) { boolean updated = false; URL url = null; try { Enumeration<URL> en = classLoader.getResources("org/threeten/bp/TZDB.dat"); while (en.hasMoreElements()) { url = en.nextElement(); updated |= load(url); } } catch (Exception ex) { throw new ZoneRulesException("Unable to load TZDB time-zone rules: " + url, ex); } return updated; }
/** * Registers the provider. * * @param provider the provider to register, not null * @throws ZoneRulesException if unable to complete the registration */ private static void registerProvider0(ZoneRulesProvider provider) { for (String zoneId : provider.provideZoneIds()) { Jdk8Methods.requireNonNull(zoneId, "zoneId"); ZoneRulesProvider old = ZONES.putIfAbsent(zoneId, provider); if (old != null) { throw new ZoneRulesException( "Unable to register zone as one already registered with that ID: " + zoneId + ", currently loading from provider: " + provider); } } }
/** * Registers the provider. * * @param provider the provider to register, not null * @throws ZoneRulesException if unable to complete the registration */ private static void registerProvider0(ZoneRulesProvider provider) { for (String zoneId : provider.provideZoneIds()) { Jdk8Methods.requireNonNull(zoneId, "zoneId"); ZoneRulesProvider old = ZONES.putIfAbsent(zoneId, provider); if (old != null) { throw new ZoneRulesException( "Unable to register zone as one already registered with that ID: " + zoneId + ", currently loading from provider: " + provider); } } }
@Override protected ZoneRules provideRules(String zoneId, boolean forCaching) { Jdk8Methods.requireNonNull(zoneId, "zoneId"); ZoneRules rules = versions.lastEntry().getValue().getRules(zoneId); if (rules == null) { throw new ZoneRulesException("Unknown time-zone ID: " + zoneId); } return rules; }
@Override protected ZoneRules provideRules(String zoneId, boolean forCaching) { Jdk8Methods.requireNonNull(zoneId, "zoneId"); ZoneRules rules = versions.lastEntry().getValue().getRules(zoneId); if (rules == null) { throw new ZoneRulesException("Unknown time-zone ID: " + zoneId); } return rules; }
/** * Loads the rules from an input stream. * * @param in the stream to load, not null, not closed after use * @throws Exception if an error occurs */ private boolean load(InputStream in) throws IOException, StreamCorruptedException { boolean updated = false; Iterable<Version> loadedVersions = loadData(in); for (Version loadedVersion : loadedVersions) { // see https://github.com/ThreeTen/threetenbp/pull/28 for issue wrt // multiple versions of lib on classpath Version existing = versions.putIfAbsent(loadedVersion.versionId, loadedVersion); if (existing != null && !existing.versionId.equals(loadedVersion.versionId)) { throw new ZoneRulesException("Data already loaded for TZDB time-zone rules version: " + loadedVersion.versionId); } updated = true; } return updated; }
/** * Loads the rules from an input stream. * * @param in the stream to load, not null, not closed after use * @throws Exception if an error occurs */ private boolean load(InputStream in) throws IOException, StreamCorruptedException { boolean updated = false; Iterable<Version> loadedVersions = loadData(in); for (Version loadedVersion : loadedVersions) { // see https://github.com/ThreeTen/threetenbp/pull/28 for issue wrt // multiple versions of lib on classpath Version existing = versions.putIfAbsent(loadedVersion.versionId, loadedVersion); if (existing != null && !existing.versionId.equals(loadedVersion.versionId)) { throw new ZoneRulesException("Data already loaded for TZDB time-zone rules version: " + loadedVersion.versionId); } updated = true; } return updated; }