package x4;

import java.io.Closeable;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/* JADX INFO: loaded from: classes.dex */
public final class g implements Closeable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final InputStream f34739a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Charset f34740b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public byte[] f34741c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f34742d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f34743e;

    public g(FileInputStream fileInputStream, Charset charset) {
        if (charset == null) {
            throw null;
        }
        if (!charset.equals(h.f34744a)) {
            throw new IllegalArgumentException("Unsupported encoding");
        }
        this.f34739a = fileInputStream;
        this.f34740b = charset;
        this.f34741c = new byte[8192];
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        synchronized (this.f34739a) {
            try {
                if (this.f34741c != null) {
                    this.f34741c = null;
                    this.f34739a.close();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:25:0x0040  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String e() {
        int i10;
        synchronized (this.f34739a) {
            try {
                byte[] bArr = this.f34741c;
                if (bArr == null) {
                    throw new IOException("LineReader is closed");
                }
                if (this.f34742d >= this.f34743e) {
                    int i11 = this.f34739a.read(bArr, 0, bArr.length);
                    if (i11 == -1) {
                        throw new EOFException();
                    }
                    this.f34742d = 0;
                    this.f34743e = i11;
                }
                for (int i12 = this.f34742d; i12 != this.f34743e; i12++) {
                    byte[] bArr2 = this.f34741c;
                    if (bArr2[i12] == 10) {
                        int i13 = this.f34742d;
                        if (i12 != i13) {
                            i10 = i12 - 1;
                            if (bArr2[i10] != 13) {
                                i10 = i12;
                            }
                        }
                        String str = new String(bArr2, i13, i10 - i13, this.f34740b.name());
                        this.f34742d = i12 + 1;
                        return str;
                    }
                }
                f fVar = new f(this, (this.f34743e - this.f34742d) + 80);
                while (true) {
                    byte[] bArr3 = this.f34741c;
                    int i14 = this.f34742d;
                    fVar.write(bArr3, i14, this.f34743e - i14);
                    this.f34743e = -1;
                    byte[] bArr4 = this.f34741c;
                    int i15 = this.f34739a.read(bArr4, 0, bArr4.length);
                    if (i15 == -1) {
                        throw new EOFException();
                    }
                    this.f34742d = 0;
                    this.f34743e = i15;
                    for (int i16 = 0; i16 != this.f34743e; i16++) {
                        byte[] bArr5 = this.f34741c;
                        if (bArr5[i16] == 10) {
                            int i17 = this.f34742d;
                            if (i16 != i17) {
                                fVar.write(bArr5, i17, i16 - i17);
                            }
                            this.f34742d = i16 + 1;
                            return fVar.toString();
                        }
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
