package nq;

import android.content.Context;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import net.danlew.android.joda.R$raw;
import org.joda.time.DateTimeZone;
import org.joda.time.tz.DateTimeZoneBuilder;

/* loaded from: classes3.dex */
public class c implements org.joda.time.tz.c {

    /* renamed from: a, reason: collision with root package name */
    private Context f30367a;

    /* renamed from: b, reason: collision with root package name */
    private final Map f30368b;

    public c(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("Context must not be null");
        }
        this.f30367a = context.getApplicationContext();
        this.f30368b = d(e("ZoneInfoMap"));
    }

    /* JADX WARN: Not initialized variable reg: 1, insn: 0x001a: MOVE (r0 I:??[OBJECT, ARRAY]) = (r1 I:??[OBJECT, ARRAY]), block:B:24:0x001a */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0032 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private DateTimeZone c(String str) {
        InputStream inputStream;
        InputStream inputStream2;
        InputStream inputStream3 = null;
        try {
            try {
                inputStream = e(str);
                try {
                    DateTimeZone b10 = DateTimeZoneBuilder.b(inputStream, str);
                    this.f30368b.put(str, new SoftReference(b10));
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException unused) {
                        }
                    }
                    return b10;
                } catch (IOException e10) {
                    e = e10;
                    g(e);
                    this.f30368b.remove(str);
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException unused2) {
                        }
                    }
                    return null;
                }
            } catch (Throwable th2) {
                th = th2;
                inputStream3 = inputStream2;
                if (inputStream3 != null) {
                    try {
                        inputStream3.close();
                    } catch (IOException unused3) {
                    }
                }
                throw th;
            }
        } catch (IOException e11) {
            e = e11;
            inputStream = null;
        } catch (Throwable th3) {
            th = th3;
            if (inputStream3 != null) {
            }
            throw th;
        }
    }

    private static Map d(InputStream inputStream) {
        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        try {
            f(dataInputStream, concurrentHashMap);
            concurrentHashMap.put("UTC", new SoftReference(DateTimeZone.f31852p));
            return concurrentHashMap;
        } finally {
            try {
                dataInputStream.close();
            } catch (IOException unused) {
            }
        }
    }

    private InputStream e(String str) {
        if (this.f30367a == null) {
            throw new RuntimeException("Need to call JodaTimeAndroid.init() before using joda-time-android");
        }
        String c10 = b.c(str);
        int b10 = b.b(R$raw.class, c10);
        if (b10 != 0) {
            return this.f30367a.getResources().openRawResource(b10);
        }
        throw new IOException("Resource not found: \"" + str + "\" (resName: \"" + c10 + "\")");
    }

    private static void f(DataInputStream dataInputStream, Map map) {
        int readUnsignedShort = dataInputStream.readUnsignedShort();
        String[] strArr = new String[readUnsignedShort];
        for (int i10 = 0; i10 < readUnsignedShort; i10++) {
            strArr[i10] = dataInputStream.readUTF().intern();
        }
        int readUnsignedShort2 = dataInputStream.readUnsignedShort();
        for (int i11 = 0; i11 < readUnsignedShort2; i11++) {
            try {
                map.put(strArr[dataInputStream.readUnsignedShort()], strArr[dataInputStream.readUnsignedShort()]);
            } catch (ArrayIndexOutOfBoundsException unused) {
                throw new IOException("Corrupt zone info map");
            }
        }
    }

    @Override // org.joda.time.tz.c
    public DateTimeZone a(String str) {
        Object obj;
        if (str == null || (obj = this.f30368b.get(str)) == null) {
            return null;
        }
        if (str.equals(obj)) {
            return c(str);
        }
        if (!(obj instanceof SoftReference)) {
            return a((String) obj);
        }
        DateTimeZone dateTimeZone = (DateTimeZone) ((SoftReference) obj).get();
        return dateTimeZone != null ? dateTimeZone : c(str);
    }

    @Override // org.joda.time.tz.c
    public Set b() {
        return new TreeSet(this.f30368b.keySet());
    }

    protected void g(Exception exc) {
        exc.printStackTrace();
    }
}
