package o;

import java.io.IOException;
import java.net.ProtocolException;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.Uw, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C0541Uw extends KB {
    public final long m;
    public boolean n;

    /* renamed from: o, reason: collision with root package name */
    public long f365o;
    public boolean p;
    public final /* synthetic */ C2398s4 q;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public C0541Uw(C2398s4 c2398s4, R80 r80, long j) {
        super(r80);
        this.q = c2398s4;
        this.m = j;
    }

    public final IOException b(IOException iOException) {
        if (this.n) {
            return iOException;
        }
        this.n = true;
        return this.q.c(false, true, iOException);
    }

    @Override // o.KB, o.R80, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        if (this.p) {
            return;
        }
        this.p = true;
        long j = this.m;
        if (j != -1 && this.f365o != j) {
            throw new ProtocolException("unexpected end of stream");
        }
        try {
            super.close();
            b(null);
        } catch (IOException e) {
            throw b(e);
        }
    }

    @Override // o.KB, o.R80
    public final void e(long j, C0732af c0732af) throws IOException {
        if (this.p) {
            throw new IllegalStateException("closed");
        }
        long j2 = this.m;
        if (j2 == -1 || this.f365o + j <= j2) {
            try {
                super.e(j, c0732af);
                this.f365o += j;
                return;
            } catch (IOException e) {
                throw b(e);
            }
        }
        throw new ProtocolException("expected " + j2 + " bytes but received " + (this.f365o + j));
    }

    @Override // o.KB, o.R80, java.io.Flushable
    public final void flush() throws IOException {
        try {
            super.flush();
        } catch (IOException e) {
            throw b(e);
        }
    }
}
