package com.bytedance.apm.agent.instrumentation.io;

import X.0HH;
import com.bytedance.apm.agent.logging.AgentLog;
import com.bytedance.apm.agent.logging.AgentLogManager;
import com.bytedance.covode.number.Covode;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public final class CountingInputStream extends InputStream implements StreamCompleteListenerSource {
    public static final AgentLog log = AgentLogManager.getAgentLog();
    public final ByteBuffer buffer;
    public long count;
    public boolean enableBuffering = false;
    public final InputStream impl;
    public final StreamCompleteListenerManager listenerManager = new StreamCompleteListenerManager();

    public final boolean markSupported() {
        return this.impl.markSupported();
    }

    static {
        Covode.recordClassIndex(23056);
    }

    private boolean bufferEmpty() {
        if (this.buffer.hasRemaining()) {
            return false;
        }
        return true;
    }

    private int readBuffer() {
        if (bufferEmpty()) {
            return -1;
        }
        return this.buffer.get();
    }

    public final void close() {
        try {
            this.impl.close();
            notifyStreamComplete();
        } catch (IOException e) {
            notifyStreamError(e);
            throw e;
        }
    }

    private void notifyStreamComplete() {
        if (!this.listenerManager.isComplete()) {
            this.listenerManager.notifyStreamComplete(new StreamCompleteEvent(this, this.count));
        }
    }

    public final int available() {
        int i;
        if (this.enableBuffering) {
            i = this.buffer.remaining();
        } else {
            i = 0;
        }
        try {
            return i + this.impl.available();
        } catch (IOException e) {
            notifyStreamError(e);
            throw e;
        }
    }

    public final void reset() {
        if (markSupported()) {
            try {
                this.impl.reset();
            } catch (IOException e) {
                notifyStreamError(e);
                throw e;
            }
        }
    }

    public final String getBufferAsString() {
        String str;
        ByteBuffer byteBuffer = this.buffer;
        if (byteBuffer == null) {
            return "";
        }
        synchronized (byteBuffer) {
            byte[] bArr = new byte[this.buffer.limit()];
            for (int i = 0; i < this.buffer.limit(); i++) {
                bArr[i] = this.buffer.get(i);
            }
            str = new String(bArr);
        }
        return str;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:11:0x001b, code lost:
        return r2;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final int read() {
        /*
            r6 = this;
            boolean r0 = r6.enableBuffering
            r3 = 1
            if (r0 == 0) goto L_0x0021
            java.nio.ByteBuffer r5 = r6.buffer
            monitor-enter(r5)
            boolean r0 = r6.bufferHasBytes(r3)     // Catch:{ all -> 0x001e }
            if (r0 == 0) goto L_0x001c
            int r2 = r6.readBuffer()     // Catch:{ all -> 0x001e }
            if (r2 < 0) goto L_0x001a
            long r0 = r6.count     // Catch:{ all -> 0x001e }
            long r0 = r0 + r3
            r6.count = r0     // Catch:{ all -> 0x001e }
        L_0x001a:
            monitor-exit(r5)     // Catch:{ all -> 0x001e }
            return r2
        L_0x001c:
            monitor-exit(r5)
            goto L_0x0021
        L_0x001e:
            r0 = move-exception
            monitor-exit(r5)
            throw r0
        L_0x0021:
            java.io.InputStream r0 = r6.impl     // Catch:{ IOException -> 0x0033 }
            int r2 = r0.read()     // Catch:{ IOException -> 0x0033 }
            if (r2 < 0) goto L_0x002f
            long r0 = r6.count     // Catch:{ IOException -> 0x0033 }
            long r0 = r0 + r3
            r6.count = r0     // Catch:{ IOException -> 0x0033 }
            goto L_0x0032
        L_0x002f:
            r6.notifyStreamComplete()     // Catch:{ IOException -> 0x0033 }
        L_0x0032:
            return r2
        L_0x0033:
            r0 = move-exception
            r6.notifyStreamError(r0)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.apm.agent.instrumentation.io.CountingInputStream.read():int");
    }

    public final void fillBuffer() {
        ByteBuffer byteBuffer = this.buffer;
        if (byteBuffer != null && byteBuffer.hasArray()) {
            synchronized (this.buffer) {
                try {
                    int read = this.impl.read(this.buffer.array(), 0, this.buffer.capacity());
                    if (read <= 0) {
                        this.buffer.limit(0);
                    } else if (read < this.buffer.capacity()) {
                        this.buffer.limit(read);
                    }
                } catch (IOException e) {
                    log.error(e.toString());
                }
            }
        }
    }

    public final void setBufferingEnabled(boolean z) {
        this.enableBuffering = z;
    }

    private int readBufferBytes(byte[] bArr) {
        return readBufferBytes(bArr, 0, bArr.length);
    }

    public final void addStreamCompleteListener(StreamCompleteListener streamCompleteListener) {
        this.listenerManager.addStreamCompleteListener(streamCompleteListener);
    }

    public final void removeStreamCompleteListener(StreamCompleteListener streamCompleteListener) {
        this.listenerManager.removeStreamCompleteListener(streamCompleteListener);
    }

    private boolean bufferHasBytes(long j) {
        if (((long) this.buffer.remaining()) >= j) {
            return true;
        }
        return false;
    }

    public final void mark(int i) {
        if (markSupported()) {
            this.impl.mark(i);
        }
    }

    public CountingInputStream(InputStream inputStream) {
        this.impl = inputStream;
        this.buffer = null;
    }

    private void notifyStreamError(Exception exc) {
        if (!this.listenerManager.isComplete()) {
            this.listenerManager.notifyStreamError(new StreamCompleteEvent(this, this.count, exc));
        }
    }

    public final int read(byte[] bArr) {
        int length = bArr.length;
        int i = 0;
        if (this.enableBuffering) {
            synchronized (this.buffer) {
                if (bufferHasBytes((long) length)) {
                    int readBufferBytes = readBufferBytes(bArr);
                    if (readBufferBytes >= 0) {
                        this.count += (long) readBufferBytes;
                        return readBufferBytes;
                    }
                    throw new IOException("readBufferBytes failed");
                }
                int remaining = this.buffer.remaining();
                if (remaining > 0) {
                    i = readBufferBytes(bArr, 0, remaining);
                    if (i >= 0) {
                        length -= i;
                        this.count += (long) i;
                    } else {
                        throw new IOException("partial read from buffer failed");
                    }
                }
            }
        }
        try {
            int read = this.impl.read(bArr, i, length);
            if (read >= 0) {
                this.count += (long) read;
                return read + i;
            } else if (i > 0) {
                return i;
            } else {
                notifyStreamComplete();
                return read;
            }
        } catch (IOException e) {
            log.error(e.toString());
            System.out.println("NOTIFY STREAM ERROR: ".concat(String.valueOf(e)));
            0HH.LIZ(e);
            notifyStreamError(e);
            throw e;
        }
    }

    public final long skip(long j) {
        if (this.enableBuffering) {
            synchronized (this.buffer) {
                if (bufferHasBytes(j)) {
                    this.buffer.position((int) j);
                    this.count += j;
                    return j;
                }
                j -= (long) this.buffer.remaining();
                if (j > 0) {
                    ByteBuffer byteBuffer = this.buffer;
                    byteBuffer.position(byteBuffer.remaining());
                } else {
                    throw new IOException("partial read from buffer (skip) failed");
                }
            }
        }
        try {
            long skip = this.impl.skip(j);
            this.count += skip;
            return skip;
        } catch (IOException e) {
            notifyStreamError(e);
            throw e;
        }
    }

    public CountingInputStream(InputStream inputStream, boolean z) {
        this.impl = inputStream;
        this.enableBuffering = z;
        if (z) {
            this.buffer = ByteBuffer.allocate(2048);
            fillBuffer();
            return;
        }
        this.buffer = null;
    }

    private int readBufferBytes(byte[] bArr, int i, int i2) {
        if (bufferEmpty()) {
            return -1;
        }
        int remaining = this.buffer.remaining();
        this.buffer.get(bArr, i, i2);
        return remaining - this.buffer.remaining();
    }

    public final int read(byte[] bArr, int i, int i2) {
        int i3 = 0;
        if (this.enableBuffering) {
            synchronized (this.buffer) {
                if (bufferHasBytes((long) i2)) {
                    int readBufferBytes = readBufferBytes(bArr, i, i2);
                    if (readBufferBytes >= 0) {
                        this.count += (long) readBufferBytes;
                        return readBufferBytes;
                    }
                    throw new IOException("readBufferBytes failed");
                }
                int remaining = this.buffer.remaining();
                if (remaining > 0) {
                    i3 = readBufferBytes(bArr, i, remaining);
                    if (i3 >= 0) {
                        i2 -= i3;
                        this.count += (long) i3;
                    } else {
                        throw new IOException("partial read from buffer failed");
                    }
                }
            }
        }
        try {
            int read = this.impl.read(bArr, i + i3, i2);
            if (read >= 0) {
                this.count += (long) read;
                return read + i3;
            } else if (i3 > 0) {
                return i3;
            } else {
                notifyStreamComplete();
                return read;
            }
        } catch (IOException e) {
            notifyStreamError(e);
            throw e;
        }
    }
}
