package jxl.read.biff;

import common.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import jxl.WorkbookSettings;
import jxl.biff.BaseCompoundFile;
import jxl.biff.IntegerHelper;
import jxl.biff.Type;

/* JADX INFO: loaded from: classes.dex */
public class File {
    static /* synthetic */ Class class$jxl$read$biff$File;
    private static Logger logger;
    private int arrayGrowSize;
    private CompoundFile compoundFile;
    private byte[] data;
    private int filePos;
    private int initialFileSize;
    private int oldPos;
    private WorkbookSettings workbookSettings;

    static {
        Class clsClass$ = class$jxl$read$biff$File;
        if (clsClass$ == null) {
            clsClass$ = class$("jxl.read.biff.File");
            class$jxl$read$biff$File = clsClass$;
        }
        logger = Logger.getLogger(clsClass$);
    }

    public File(InputStream inputStream, WorkbookSettings workbookSettings) throws BiffException, IOException {
        this.workbookSettings = workbookSettings;
        this.initialFileSize = workbookSettings.getInitialFileSize();
        this.arrayGrowSize = this.workbookSettings.getArrayGrowSize();
        byte[] bArr = new byte[this.initialFileSize];
        int i2 = inputStream.read(bArr);
        if (Thread.currentThread().isInterrupted()) {
            throw new InterruptedIOException();
        }
        int i3 = i2;
        while (i2 != -1) {
            if (i3 >= bArr.length) {
                byte[] bArr2 = new byte[bArr.length + this.arrayGrowSize];
                System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
                bArr = bArr2;
            }
            i2 = inputStream.read(bArr, i3, bArr.length - i3);
            i3 += i2;
            if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedIOException();
            }
        }
        if (i3 + 1 == 0) {
            throw new BiffException(BiffException.excelFileNotFound);
        }
        CompoundFile compoundFile = new CompoundFile(bArr, workbookSettings);
        try {
            this.data = compoundFile.getStream("workbook");
        } catch (BiffException unused) {
            this.data = compoundFile.getStream("book");
        }
        if (!this.workbookSettings.getPropertySetsDisabled() && compoundFile.getNumberOfPropertySets() > BaseCompoundFile.STANDARD_PROPERTY_SETS.length) {
            this.compoundFile = compoundFile;
        }
        if (this.workbookSettings.getGCDisabled()) {
            return;
        }
        System.gc();
    }

    static /* synthetic */ Class class$(String str) {
        try {
            return Class.forName(str);
        } catch (ClassNotFoundException e) {
            throw new NoClassDefFoundError(e.getMessage());
        }
    }

    private void moveToFirstBof() {
        boolean z = false;
        while (!z) {
            byte[] bArr = this.data;
            int i2 = this.filePos;
            if (IntegerHelper.getInt(bArr[i2], bArr[i2 + 1]) == Type.BOF.value) {
                z = true;
            } else {
                skip(128);
            }
        }
    }

    public void clear() {
        this.data = null;
    }

    public void close() {
    }

    CompoundFile getCompoundFile() {
        return this.compoundFile;
    }

    public int getPos() {
        return this.filePos;
    }

    public boolean hasNext() {
        return this.filePos < this.data.length + (-4);
    }

    Record next() {
        return new Record(this.data, this.filePos, this);
    }

    Record peek() {
        int i2 = this.filePos;
        Record record = new Record(this.data, this.filePos, this);
        this.filePos = i2;
        return record;
    }

    public byte[] read(int i2, int i3) {
        byte[] bArr = new byte[i3];
        try {
            System.arraycopy(this.data, i2, bArr, 0, i3);
            return bArr;
        } catch (ArrayIndexOutOfBoundsException e) {
            Logger logger2 = logger;
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("Array index out of bounds at position ");
            stringBuffer.append(i2);
            stringBuffer.append(" record length ");
            stringBuffer.append(i3);
            logger2.error(stringBuffer.toString());
            throw e;
        }
    }

    public void restorePos() {
        this.filePos = this.oldPos;
    }

    public void setPos(int i2) {
        this.oldPos = this.filePos;
        this.filePos = i2;
    }

    public void skip(int i2) {
        this.filePos += i2;
    }

    public File(byte[] bArr) {
        this.data = bArr;
    }
}
