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 SSTRecord extends WritableRecordData {
    private static int maxBytes = 8216;
    private int byteCount;
    private byte[] data;
    private int numReferences;
    private int numStrings;
    private ArrayList stringLengths;
    private ArrayList strings;

    public SSTRecord(int i2, int i3) {
        super(Type.SST);
        this.numReferences = i2;
        this.numStrings = i3;
        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) {
            if (str.length() > 0) {
                return str.length();
            }
            return -1;
        }
        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 length = 8;
        byte[] bArr = new byte[this.byteCount + 8];
        this.data = bArr;
        int i2 = 0;
        IntegerHelper.getFourBytes(this.numReferences, bArr, 0);
        IntegerHelper.getFourBytes(this.numStrings, this.data, 4);
        for (String str : this.strings) {
            IntegerHelper.getTwoBytes(((Integer) this.stringLengths.get(i2)).intValue(), this.data, length);
            byte[] bArr2 = this.data;
            bArr2[length + 2] = 1;
            StringHelper.getUnicodeBytes(str, bArr2, length + 3);
            length += (str.length() * 2) + 3;
            i2++;
        }
        return this.data;
    }

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