package org.jacoco.core.data;

import java.io.IOException;
import java.io.InputStream;
import org.jacoco.core.internal.data.CompactDataInput;
/* loaded from: classes6.dex */
public class ExecutionDataReader {
    protected final CompactDataInput in;
    private ISessionInfoVisitor sessionInfoVisitor = null;
    private IExecutionDataVisitor executionDataVisitor = null;
    private boolean firstBlock = true;

    public void setExecutionDataVisitor(IExecutionDataVisitor iExecutionDataVisitor) {
        this.executionDataVisitor = iExecutionDataVisitor;
    }

    public void setSessionInfoVisitor(ISessionInfoVisitor iSessionInfoVisitor) {
        this.sessionInfoVisitor = iSessionInfoVisitor;
    }

    public ExecutionDataReader(InputStream inputStream) {
        this.in = new CompactDataInput(inputStream);
    }

    public boolean read() throws IOException, IncompatibleExecDataVersionException {
        byte b;
        do {
            int read = this.in.read();
            if (read == -1) {
                return false;
            }
            b = (byte) read;
            if (this.firstBlock && b != 1) {
                throw new IOException("Invalid execution data file.");
            }
            this.firstBlock = false;
        } while (readBlock(b));
        return true;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public boolean readBlock(byte b) throws IOException {
        if (b == 1) {
            readHeader();
            return true;
        } else if (b == 16) {
            readSessionInfo();
            return true;
        } else if (b == 17) {
            readExecutionData();
            return true;
        } else {
            throw new IOException(String.format("Unknown block type %x.", Byte.valueOf(b)));
        }
    }

    private void readHeader() throws IOException {
        if (this.in.readChar() != 49344) {
            throw new IOException("Invalid execution data file.");
        }
        char readChar = this.in.readChar();
        if (readChar != ExecutionDataWriter.FORMAT_VERSION) {
            throw new IncompatibleExecDataVersionException(readChar);
        }
    }

    private void readSessionInfo() throws IOException {
        if (this.sessionInfoVisitor == null) {
            throw new IOException("No session info visitor.");
        }
        this.sessionInfoVisitor.visitSessionInfo(new SessionInfo(this.in.readUTF(), this.in.readLong(), this.in.readLong()));
    }

    private void readExecutionData() throws IOException {
        if (this.executionDataVisitor == null) {
            throw new IOException("No execution data visitor.");
        }
        this.executionDataVisitor.visitClassExecution(new ExecutionData(this.in.readLong(), this.in.readUTF(), this.in.readBooleanArray()));
    }
}
