package o; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StreamCorruptedException; import java.net.URL; import java.util.Arrays; import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.NavigableMap; import java.util.Set; import java.util.TreeMap; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.atomic.AtomicReferenceArray; import org.threeten.bp.zone.ZoneRulesException; /* loaded from: classes6.dex */ public final class gtY extends AbstractC15576guc { private List regionIds; private final ConcurrentNavigableMap versions = new ConcurrentSkipListMap(); private Set loadedUrls = new CopyOnWriteArraySet(); public gtY() { if (!load(AbstractC15576guc.class.getClassLoader())) { throw new ZoneRulesException("No time-zone rules found for 'TZDB'"); } } public gtY(URL url) { try { if (load(url)) { return; } StringBuilder sb = new StringBuilder("No time-zone rules found: "); sb.append(url); throw new ZoneRulesException(sb.toString()); } catch (Exception e) { throw new ZoneRulesException("Unable to load TZDB time-zone rules: ".concat(String.valueOf(url)), e); } } public gtY(InputStream inputStream) { try { load(inputStream); } catch (Exception e) { throw new ZoneRulesException("Unable to load TZDB time-zone rules", e); } } @Override // o.AbstractC15576guc protected final Set provideZoneIds() { return new HashSet(this.regionIds); } @Override // o.AbstractC15576guc protected final AbstractC15574gua provideRules(String str, boolean z) { gtG.requireNonNull(str, "zoneId"); AbstractC15574gua rules = this.versions.lastEntry().getValue().getRules(str); if (rules != null) { return rules; } throw new ZoneRulesException("Unknown time-zone ID: ".concat(String.valueOf(str))); } @Override // o.AbstractC15576guc protected final NavigableMap provideVersions(String str) { TreeMap treeMap = new TreeMap(); for (Sts sts : this.versions.values()) { AbstractC15574gua rules = sts.getRules(str); if (rules != null) { treeMap.put(sts.versionId, rules); } } return treeMap; } private boolean load(ClassLoader classLoader) { URL url; URL url2 = null; try { Enumeration resources = classLoader.getResources("org/threeten/bp/TZDB.dat"); boolean z = false; while (resources.hasMoreElements()) { url = resources.nextElement(); try { z |= load(url); url2 = url; } catch (Exception e) { e = e; throw new ZoneRulesException("Unable to load TZDB time-zone rules: ".concat(String.valueOf(url)), e); } } return z; } catch (Exception e2) { e = e2; url = url2; } } private boolean load(URL url) throws ClassNotFoundException, IOException, ZoneRulesException { InputStream inputStream; if (!this.loadedUrls.add(url.toExternalForm())) { return false; } try { inputStream = url.openStream(); try { boolean load = load(inputStream); if (inputStream == null) { return load; } inputStream.close(); return load; } catch (Throwable th) { th = th; if (inputStream != null) { inputStream.close(); } throw th; } } catch (Throwable th2) { th = th2; inputStream = null; } } private boolean load(InputStream inputStream) throws IOException, StreamCorruptedException { boolean z = false; for (Sts sts : loadData(inputStream)) { Sts putIfAbsent = this.versions.putIfAbsent(sts.versionId, sts); if (putIfAbsent != null && !putIfAbsent.versionId.equals(sts.versionId)) { StringBuilder sb = new StringBuilder("Data already loaded for TZDB time-zone rules version: "); sb.append(sts.versionId); throw new ZoneRulesException(sb.toString()); } z = true; } return z; } private Iterable loadData(InputStream inputStream) throws IOException, StreamCorruptedException { DataInputStream dataInputStream = new DataInputStream(inputStream); if (dataInputStream.readByte() != 1) { throw new StreamCorruptedException("File format not recognised"); } if (!"TZDB".equals(dataInputStream.readUTF())) { throw new StreamCorruptedException("File format not recognised"); } int readShort = dataInputStream.readShort(); String[] strArr = new String[readShort]; for (int i = 0; i < readShort; i++) { strArr[i] = dataInputStream.readUTF(); } int readShort2 = dataInputStream.readShort(); String[] strArr2 = new String[readShort2]; for (int i2 = 0; i2 < readShort2; i2++) { strArr2[i2] = dataInputStream.readUTF(); } this.regionIds = Arrays.asList(strArr2); int readShort3 = dataInputStream.readShort(); Object[] objArr = new Object[readShort3]; for (int i3 = 0; i3 < readShort3; i3++) { byte[] bArr = new byte[dataInputStream.readShort()]; dataInputStream.readFully(bArr); objArr[i3] = bArr; } AtomicReferenceArray atomicReferenceArray = new AtomicReferenceArray(objArr); HashSet hashSet = new HashSet(readShort); for (int i4 = 0; i4 < readShort; i4++) { int readShort4 = dataInputStream.readShort(); String[] strArr3 = new String[readShort4]; short[] sArr = new short[readShort4]; for (int i5 = 0; i5 < readShort4; i5++) { strArr3[i5] = strArr2[dataInputStream.readShort()]; sArr[i5] = dataInputStream.readShort(); } hashSet.add(new Sts(strArr[i4], strArr3, sArr, atomicReferenceArray)); } return hashSet; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static class Sts { private final String[] regionArray; private final AtomicReferenceArray ruleData; private final short[] ruleIndices; private final String versionId; Sts(String str, String[] strArr, short[] sArr, AtomicReferenceArray atomicReferenceArray) { this.ruleData = atomicReferenceArray; this.versionId = str; this.regionArray = strArr; this.ruleIndices = sArr; } AbstractC15574gua getRules(String str) { int binarySearch = Arrays.binarySearch(this.regionArray, str); if (binarySearch < 0) { return null; } try { return createRule(this.ruleIndices[binarySearch]); } catch (Exception e) { StringBuilder sb = new StringBuilder("Invalid binary time-zone data: TZDB:"); sb.append(str); sb.append(", version: "); sb.append(this.versionId); throw new ZoneRulesException(sb.toString(), e); } } AbstractC15574gua createRule(short s) throws Exception { Object obj = this.ruleData.get(s); if (obj instanceof byte[]) { obj = gtU.read(new DataInputStream(new ByteArrayInputStream((byte[]) obj))); this.ruleData.set(s, obj); } return (AbstractC15574gua) obj; } public String toString() { return this.versionId; } } public final String toString() { return "TZDB"; } }