@Override protected void initializeProviders() { TzdbZoneRulesProvider provider; InputStream is = null; try { is = context.getAssets().open("org/threeten/bp/TZDB.dat"); provider = new TzdbZoneRulesProvider(is); } catch (IOException e) { throw new IllegalStateException("TZDB.dat missing from assets.", e); } finally { if (is != null) { try { is.close(); } catch (IOException ignored) { } } } ZoneRulesProvider.registerProvider(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'"); } }
/** * 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; }
/** * 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'"); } }
/** * 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 a URL, often in a jar file. * * @param url the jar file to load, not null * @return true if updated * @throws ClassNotFoundException if a classpath error occurs * @throws IOException if an IO error occurs * @throws ZoneRulesException if the data is already loaded for the version */ private boolean load(URL url) throws ClassNotFoundException, IOException, ZoneRulesException { boolean updated = false; if (loadedUrls.add(url.toExternalForm())) { InputStream in = null; try { in = url.openStream(); updated |= load(in); } finally { if (in != null) { in.close(); } } } return updated; }
/** * Loads the rules from a URL, often in a jar file. * * @param url the jar file to load, not null * @return true if updated * @throws ClassNotFoundException if a classpath error occurs * @throws IOException if an IO error occurs * @throws ZoneRulesException if the data is already loaded for the version */ private boolean load(URL url) throws ClassNotFoundException, IOException, ZoneRulesException { boolean updated = false; if (loadedUrls.add(url.toExternalForm())) { InputStream in = null; try { in = url.openStream(); updated |= load(in); } finally { if (in != null) { in.close(); } } } return updated; }
/** * 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); } }
/** * 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; }
/** * 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); } }