package p138.p145.p146.p151.p160.p161;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import p138.p145.p146.p151.p154.p155.InterfaceC2940;

/* JADX INFO: renamed from: ދ.ؠ.֏.ތ.ތ.ހ.ޓ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C3110 extends FilterInputStream {

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public volatile byte[] f9336;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public int f9337;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public int f9338;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public int f9339;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public int f9340;

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public final InterfaceC2940 f9341;

    /* JADX INFO: renamed from: ދ.ؠ.֏.ތ.ތ.ހ.ޓ$֏, reason: contains not printable characters */
    public static class C3111 extends IOException {
        public C3111(String str) {
            super(str);
        }
    }

    public C3110(InputStream inputStream, InterfaceC2940 interfaceC2940) {
        super(inputStream);
        this.f9339 = -1;
        this.f9341 = interfaceC2940;
        this.f9336 = (byte[]) interfaceC2940.mo5081(65536, byte[].class);
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public static IOException m5193() throws IOException {
        throw new IOException("BufferedInputStream is closed");
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public synchronized int available() {
        InputStream inputStream;
        inputStream = ((FilterInputStream) this).in;
        if (this.f9336 == null || inputStream == null) {
            m5193();
            throw null;
        }
        return (this.f9337 - this.f9340) + inputStream.available();
    }

    @Override // java.io.FilterInputStream, java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f9336 != null) {
            this.f9341.mo5080(this.f9336);
            this.f9336 = null;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        ((FilterInputStream) this).in = null;
        if (inputStream != null) {
            inputStream.close();
        }
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public synchronized void mark(int i) {
        this.f9338 = Math.max(this.f9338, i);
        this.f9339 = this.f9340;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public boolean markSupported() {
        return true;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public synchronized int read() {
        byte[] bArr = this.f9336;
        InputStream inputStream = ((FilterInputStream) this).in;
        if (bArr == null || inputStream == null) {
            m5193();
            throw null;
        }
        if (this.f9340 >= this.f9337 && m5194(inputStream, bArr) == -1) {
            return -1;
        }
        if (bArr != this.f9336 && (bArr = this.f9336) == null) {
            m5193();
            throw null;
        }
        if (this.f9337 - this.f9340 <= 0) {
            return -1;
        }
        int i = this.f9340;
        this.f9340 = i + 1;
        return bArr[i] & 255;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public synchronized int read(byte[] bArr, int i, int i2) {
        int i3;
        int i4;
        byte[] bArr2 = this.f9336;
        if (bArr2 == null) {
            m5193();
            throw null;
        }
        if (i2 == 0) {
            return 0;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        if (inputStream == null) {
            m5193();
            throw null;
        }
        if (this.f9340 < this.f9337) {
            int i5 = this.f9337 - this.f9340 >= i2 ? i2 : this.f9337 - this.f9340;
            System.arraycopy(bArr2, this.f9340, bArr, i, i5);
            this.f9340 += i5;
            if (i5 == i2 || inputStream.available() == 0) {
                return i5;
            }
            i += i5;
            i3 = i2 - i5;
        } else {
            i3 = i2;
        }
        while (true) {
            if (this.f9339 == -1 && i3 >= bArr2.length) {
                i4 = inputStream.read(bArr, i, i3);
                if (i4 == -1) {
                    return i3 != i2 ? i2 - i3 : -1;
                }
            } else {
                if (m5194(inputStream, bArr2) == -1) {
                    return i3 != i2 ? i2 - i3 : -1;
                }
                if (bArr2 != this.f9336 && (bArr2 = this.f9336) == null) {
                    m5193();
                    throw null;
                }
                i4 = this.f9337 - this.f9340 >= i3 ? i3 : this.f9337 - this.f9340;
                System.arraycopy(bArr2, this.f9340, bArr, i, i4);
                this.f9340 += i4;
            }
            i3 -= i4;
            if (i3 == 0) {
                return i2;
            }
            if (inputStream.available() == 0) {
                return i2 - i3;
            }
            i += i4;
        }
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public synchronized void reset() {
        if (this.f9336 == null) {
            throw new IOException("Stream is closed");
        }
        if (-1 == this.f9339) {
            throw new C3111("Mark has been invalidated, pos: " + this.f9340 + " markLimit: " + this.f9338);
        }
        this.f9340 = this.f9339;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public synchronized long skip(long j) {
        if (j < 1) {
            return 0L;
        }
        byte[] bArr = this.f9336;
        if (bArr == null) {
            m5193();
            throw null;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        if (inputStream == null) {
            m5193();
            throw null;
        }
        if (this.f9337 - this.f9340 >= j) {
            this.f9340 = (int) (((long) this.f9340) + j);
            return j;
        }
        long j2 = ((long) this.f9337) - ((long) this.f9340);
        this.f9340 = this.f9337;
        if (this.f9339 == -1 || j > this.f9338) {
            return j2 + inputStream.skip(j - j2);
        }
        if (m5194(inputStream, bArr) == -1) {
            return j2;
        }
        if (this.f9337 - this.f9340 >= j - j2) {
            this.f9340 = (int) ((((long) this.f9340) + j) - j2);
            return j;
        }
        long j3 = (j2 + ((long) this.f9337)) - ((long) this.f9340);
        this.f9340 = this.f9337;
        return j3;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final int m5194(InputStream inputStream, byte[] bArr) throws IOException {
        int i = this.f9339;
        if (i != -1) {
            int i2 = this.f9340 - i;
            int i3 = this.f9338;
            if (i2 < i3) {
                if (i == 0 && i3 > bArr.length && this.f9337 == bArr.length) {
                    int length = bArr.length * 2;
                    if (length <= i3) {
                        i3 = length;
                    }
                    byte[] bArr2 = (byte[]) this.f9341.mo5081(i3, byte[].class);
                    System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
                    this.f9336 = bArr2;
                    this.f9341.mo5080(bArr);
                    bArr = bArr2;
                } else {
                    int i4 = this.f9339;
                    if (i4 > 0) {
                        System.arraycopy(bArr, i4, bArr, 0, bArr.length - i4);
                    }
                }
                int i5 = this.f9340 - this.f9339;
                this.f9340 = i5;
                this.f9339 = 0;
                this.f9337 = 0;
                int i6 = inputStream.read(bArr, i5, bArr.length - i5);
                int i7 = this.f9340;
                if (i6 > 0) {
                    i7 += i6;
                }
                this.f9337 = i7;
                return i6;
            }
        }
        int i8 = inputStream.read(bArr);
        if (i8 > 0) {
            this.f9339 = -1;
            this.f9340 = 0;
            this.f9337 = i8;
        }
        return i8;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public synchronized void m5195() {
        this.f9338 = this.f9336.length;
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public synchronized void m5196() {
        if (this.f9336 != null) {
            this.f9341.mo5080(this.f9336);
            this.f9336 = null;
        }
    }
}
