package org.snmp4j.agent.cfg;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;

/* JADX INFO: loaded from: classes.dex */
public class EngineBootsCounterFile implements EngineBootsProvider {
    private static final LogAdapter logger = LogFactory.getLogger(EngineBootsCounterFile.class);
    private File bootCounterFile;
    private int boots = -1;

    public EngineBootsCounterFile(File file) {
        this.bootCounterFile = file;
    }

    public File getBootCounterFile() {
        return this.bootCounterFile;
    }

    @Override // org.snmp4j.agent.cfg.EngineBootsProvider
    public int getEngineBoots() {
        if (this.boots < 0) {
            this.boots = getLastEngineBoots();
        }
        return this.boots;
    }

    /* JADX WARN: Removed duplicated region for block: B:49:0x009c A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    protected int getLastEngineBoots() throws java.lang.Throwable {
        /*
            r6 = this;
            r0 = 0
            java.io.FileInputStream r1 = new java.io.FileInputStream     // Catch: java.lang.Throwable -> L3e java.io.IOException -> L43 java.io.FileNotFoundException -> L74
            java.io.File r2 = r6.bootCounterFile     // Catch: java.lang.Throwable -> L3e java.io.IOException -> L43 java.io.FileNotFoundException -> L74
            r1.<init>(r2)     // Catch: java.lang.Throwable -> L3e java.io.IOException -> L43 java.io.FileNotFoundException -> L74
            java.io.ObjectInputStream r0 = new java.io.ObjectInputStream     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            r0.<init>(r1)     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            int r0 = r0.readInt()     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            org.snmp4j.log.LogAdapter r2 = org.snmp4j.agent.cfg.EngineBootsCounterFile.logger     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            boolean r2 = r2.isInfoEnabled()     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            if (r2 == 0) goto L2f
            org.snmp4j.log.LogAdapter r2 = org.snmp4j.agent.cfg.EngineBootsCounterFile.logger     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            java.lang.StringBuilder r3 = new java.lang.StringBuilder     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            r3.<init>()     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            java.lang.String r4 = "Engine boots is: "
            r3.append(r4)     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            r3.append(r0)     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            java.lang.String r3 = r3.toString()     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
            r2.info(r3)     // Catch: java.io.IOException -> L3a java.io.FileNotFoundException -> L3c java.lang.Throwable -> L72
        L2f:
            r1.close()     // Catch: java.io.IOException -> L33
            goto L39
        L33:
            r1 = move-exception
            org.snmp4j.log.LogAdapter r2 = org.snmp4j.agent.cfg.EngineBootsCounterFile.logger
            r2.warn(r1)
        L39:
            return r0
        L3a:
            r0 = move-exception
            goto L47
        L3c:
            r0 = r1
            goto L74
        L3e:
            r1 = move-exception
            r5 = r1
            r1 = r0
            r0 = r5
            goto L9a
        L43:
            r1 = move-exception
            r5 = r1
            r1 = r0
            r0 = r5
        L47:
            org.snmp4j.log.LogAdapter r2 = org.snmp4j.agent.cfg.EngineBootsCounterFile.logger     // Catch: java.lang.Throwable -> L72
            boolean r2 = r2.isDebugEnabled()     // Catch: java.lang.Throwable -> L72
            if (r2 == 0) goto L52
            r0.printStackTrace()     // Catch: java.lang.Throwable -> L72
        L52:
            org.snmp4j.log.LogAdapter r2 = org.snmp4j.agent.cfg.EngineBootsCounterFile.logger     // Catch: java.lang.Throwable -> L72
            java.lang.StringBuilder r3 = new java.lang.StringBuilder     // Catch: java.lang.Throwable -> L72
            r3.<init>()     // Catch: java.lang.Throwable -> L72
            java.lang.String r4 = "Failed to read boot counter: "
            r3.append(r4)     // Catch: java.lang.Throwable -> L72
            java.lang.String r0 = r0.getMessage()     // Catch: java.lang.Throwable -> L72
            r3.append(r0)     // Catch: java.lang.Throwable -> L72
            java.lang.String r0 = r3.toString()     // Catch: java.lang.Throwable -> L72
            r2.error(r0)     // Catch: java.lang.Throwable -> L72
            if (r1 == 0) goto L98
            r1.close()     // Catch: java.io.IOException -> L92
            goto L98
        L72:
            r0 = move-exception
            goto L9a
        L74:
            org.snmp4j.log.LogAdapter r1 = org.snmp4j.agent.cfg.EngineBootsCounterFile.logger     // Catch: java.lang.Throwable -> L3e
            java.lang.StringBuilder r2 = new java.lang.StringBuilder     // Catch: java.lang.Throwable -> L3e
            r2.<init>()     // Catch: java.lang.Throwable -> L3e
            java.lang.String r3 = "Could not find boot counter file: "
            r2.append(r3)     // Catch: java.lang.Throwable -> L3e
            java.io.File r3 = r6.bootCounterFile     // Catch: java.lang.Throwable -> L3e
            r2.append(r3)     // Catch: java.lang.Throwable -> L3e
            java.lang.String r2 = r2.toString()     // Catch: java.lang.Throwable -> L3e
            r1.warn(r2)     // Catch: java.lang.Throwable -> L3e
            if (r0 == 0) goto L98
            r0.close()     // Catch: java.io.IOException -> L92
            goto L98
        L92:
            r0 = move-exception
            org.snmp4j.log.LogAdapter r1 = org.snmp4j.agent.cfg.EngineBootsCounterFile.logger
            r1.warn(r0)
        L98:
            r0 = 0
            return r0
        L9a:
            if (r1 == 0) goto La6
            r1.close()     // Catch: java.io.IOException -> La0
            goto La6
        La0:
            r1 = move-exception
            org.snmp4j.log.LogAdapter r2 = org.snmp4j.agent.cfg.EngineBootsCounterFile.logger
            r2.warn(r1)
        La6:
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: org.snmp4j.agent.cfg.EngineBootsCounterFile.getLastEngineBoots():int");
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v0 */
    /* JADX WARN: Type inference failed for: r0v1 */
    /* JADX WARN: Type inference failed for: r0v10 */
    /* JADX WARN: Type inference failed for: r0v11 */
    /* JADX WARN: Type inference failed for: r0v15 */
    /* JADX WARN: Type inference failed for: r0v17 */
    /* JADX WARN: Type inference failed for: r0v18 */
    /* JADX WARN: Type inference failed for: r0v19 */
    /* JADX WARN: Type inference failed for: r0v2, types: [java.io.FileOutputStream] */
    /* JADX WARN: Type inference failed for: r0v20 */
    /* JADX WARN: Type inference failed for: r0v21 */
    /* JADX WARN: Type inference failed for: r0v22 */
    /* JADX WARN: Type inference failed for: r0v23 */
    /* JADX WARN: Type inference failed for: r0v24 */
    /* JADX WARN: Type inference failed for: r0v5, types: [java.io.FileOutputStream] */
    /* JADX WARN: Type inference failed for: r0v6, types: [java.io.FileOutputStream] */
    /* JADX WARN: Type inference failed for: r0v7 */
    /* JADX WARN: Type inference failed for: r0v8 */
    /* JADX WARN: Type inference failed for: r0v9 */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:10:0x0036 -> B:35:0x0088). Please report as a decompilation issue!!! */
    protected void setEngineBoots(int i2) throws Throwable {
        FileOutputStream fileOutputStream;
        ?? r0 = 0;
        r0 = 0;
        r0 = 0;
        try {
            try {
                try {
                    fileOutputStream = new FileOutputStream(this.bootCounterFile);
                } catch (IOException e2) {
                    LogAdapter logAdapter = logger;
                    logAdapter.warn(e2);
                    r0 = logAdapter;
                }
            } catch (FileNotFoundException e3) {
                e = e3;
            } catch (IOException e4) {
                e = e4;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeInt(i2);
            objectOutputStream.close();
            boolean zIsInfoEnabled = logger.isInfoEnabled();
            ?? r02 = zIsInfoEnabled;
            if (zIsInfoEnabled) {
                LogAdapter logAdapter2 = logger;
                logAdapter2.info("Wrote boot counter: " + i2);
                r02 = logAdapter2;
            }
            fileOutputStream.close();
            r0 = r02;
        } catch (FileNotFoundException e5) {
            e = e5;
            r0 = fileOutputStream;
            logger.error("Boot counter configuration file not found: " + e.getMessage());
            if (r0 != 0) {
                r0.close();
                r0 = r0;
            }
        } catch (IOException e6) {
            e = e6;
            r0 = fileOutputStream;
            logger.error("Failed to write boot counter: " + e.getMessage());
            if (r0 != 0) {
                r0.close();
                r0 = r0;
            }
        } catch (Throwable th2) {
            th = th2;
            r0 = fileOutputStream;
            if (r0 != 0) {
                try {
                    r0.close();
                } catch (IOException e7) {
                    logger.warn(e7);
                }
            }
            throw th;
        }
    }

    @Override // org.snmp4j.agent.cfg.EngineBootsProvider
    public int updateEngineBoots() throws Throwable {
        int lastEngineBoots = getLastEngineBoots();
        this.boots = lastEngineBoots;
        int i2 = lastEngineBoots + 1;
        this.boots = i2;
        if (i2 <= 0) {
            this.boots = 1;
        }
        setEngineBoots(this.boots);
        return this.boots;
    }
}
