package com.ifpdos.autotest.k0.d;

import android.os.SystemProperties;
import android.util.Log;
import com.seewo.libmcuservice.aidl.model.McuEnum;
import d.n;
import d.n0.c.s;
import d.s0.r;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

/* JADX INFO: compiled from: FileHandler.kt */
/* JADX INFO: loaded from: classes.dex */
@n
public final class b {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final b f2026a = new b();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static File f2027b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static Base64.Decoder f2028c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static FileOutputStream f2029d;

    private b() {
    }

    private final void a() throws IOException {
        Log.i("SerialPortServer", "startTranFile");
        File file = new File("data/" + SystemProperties.get("persist.sys.file.name", "3dlut.bin"));
        f2027b = file;
        s.b(file);
        if (file.exists()) {
            File file2 = f2027b;
            s.b(file2);
            file2.delete();
        }
        File file3 = f2027b;
        s.b(file3);
        file3.createNewFile();
        f2029d = new FileOutputStream(f2027b);
        f2028c = Base64.getMimeDecoder();
    }

    private final void c(String str) {
        try {
            if (f2029d == null) {
                a();
            }
            Base64.Decoder decoder = f2028c;
            byte[] bArrDecode = decoder != null ? decoder.decode(str) : null;
            StringBuilder sb = new StringBuilder();
            sb.append("data.size");
            sb.append(bArrDecode != null ? Integer.valueOf(bArrDecode.length) : null);
            Log.i("SerialPortServer", sb.toString());
            FileOutputStream fileOutputStream = f2029d;
            s.b(fileOutputStream);
            Integer numValueOf = bArrDecode != null ? Integer.valueOf(bArrDecode.length) : null;
            s.b(numValueOf);
            fileOutputStream.write(bArrDecode, 0, numValueOf.intValue());
        } catch (Exception e2) {
            Log.i("SerialPortServer", "onProcessor: ", e2);
            e2.printStackTrace();
            throw new RuntimeException(e2);
        }
    }

    public static final boolean e() {
        return SystemProperties.getBoolean("sys.start.tran.file", false);
    }

    public static final void f(boolean z) {
        SystemProperties.set("sys.start.tran.file", String.valueOf(z));
    }

    public final void b(String str) throws IOException {
        s.e(str, McuEnum.KEY_DATA);
        if (r.w(str, "startTranFile", false, 2, null)) {
            a();
        }
    }

    public final void d(String str, com.ifpdos.autotest.lancontrol.serialport.b bVar) throws IOException {
        FileDescriptor fd;
        s.e(str, McuEnum.KEY_DATA);
        if (!r.w(str, "endTranFile", false, 2, null)) {
            c(str);
            return;
        }
        f(false);
        if (bVar != null) {
            bVar.c("{\"paramsJson\":\"true\",\"requestName\":\"com.ifpdos.autotest.api.IWB.endTranFile\",\"responseName\":\"OTHER\",\"status\":\"SUCCESS\"}\n");
        }
        FileOutputStream fileOutputStream = f2029d;
        if (fileOutputStream != null) {
            fileOutputStream.flush();
        }
        FileOutputStream fileOutputStream2 = f2029d;
        if (fileOutputStream2 != null && (fd = fileOutputStream2.getFD()) != null) {
            fd.sync();
        }
        FileOutputStream fileOutputStream3 = f2029d;
        if (fileOutputStream3 != null) {
            fileOutputStream3.close();
        }
    }
}
