package jxl.write.biff;

import java.util.ArrayList;
import jxl.biff.IntegerHelper;
import jxl.biff.StringHelper;
import jxl.biff.Type;
import jxl.biff.WritableRecordData;

/* JADX INFO: loaded from: classes.dex */
class SSTContinueRecord extends WritableRecordData {
    private static int maxBytes = 8224;
    private int byteCount;
    private byte[] data;
    private String firstString;
    private int firstStringLength;
    private boolean includeLength;
    private ArrayList stringLengths;
    private ArrayList strings;

    public SSTContinueRecord() {
        super(Type.CONTINUE);
        this.byteCount = 0;
        this.strings = new ArrayList(50);
        this.stringLengths = new ArrayList(50);
    }

    public int add(String str) {
        int length = (str.length() * 2) + 3;
        if (this.byteCount >= maxBytes - 5) {
            return str.length();
        }
        this.stringLengths.add(new Integer(str.length()));
        int i2 = this.byteCount;
        int i3 = length + i2;
        int i4 = maxBytes;
        if (i3 < i4) {
            this.strings.add(str);
            this.byteCount += length;
            return 0;
        }
        int i5 = (i4 - 3) - i2;
        if (i5 % 2 != 0) {
            i5--;
        }
        int i6 = i5 / 2;
        this.strings.add(str.substring(0, i6));
        this.byteCount += (i6 * 2) + 3;
        return str.length() - i6;
    }

    @Override // jxl.biff.WritableRecordData
    public byte[] getData() {
        int i2;
        byte[] bArr = new byte[this.byteCount];
        this.data = bArr;
        int i3 = 0;
        if (this.includeLength) {
            IntegerHelper.getTwoBytes(this.firstStringLength, bArr, 0);
            this.data[2] = 1;
            i2 = 3;
        } else {
            bArr[0] = 1;
            i2 = 1;
        }
        StringHelper.getUnicodeBytes(this.firstString, this.data, i2);
        int length = i2 + (this.firstString.length() * 2);
        for (String str : this.strings) {
            IntegerHelper.getTwoBytes(((Integer) this.stringLengths.get(i3)).intValue(), this.data, length);
            byte[] bArr2 = this.data;
            bArr2[length + 2] = 1;
            StringHelper.getUnicodeBytes(str, bArr2, length + 3);
            length += (str.length() * 2) + 3;
            i3++;
        }
        return this.data;
    }

    public int getOffset() {
        return this.byteCount;
    }

    public int setFirstString(String str, boolean z) {
        this.includeLength = z;
        this.firstStringLength = str.length();
        int length = !this.includeLength ? (str.length() * 2) + 1 : (str.length() * 2) + 3;
        int i2 = maxBytes;
        if (length <= i2) {
            this.firstString = str;
            this.byteCount += length;
            return 0;
        }
        int i3 = (this.includeLength ? i2 - 4 : i2 - 2) / 2;
        this.firstString = str.substring(0, i3);
        this.byteCount = maxBytes - 1;
        return str.length() - i3;
    }
}
