// 
// Decompiled by Procyon v0.6.0
// 

package org.jline.utils;

import java.io.IOException;
import java.io.InputStream;

public abstract class NonBlockingInputStream extends InputStream
{
    public static final int EOF = -1;
    public static final int READ_EXPIRED = -2;
    
    @Override
    public int read() throws IOException {
        return this.read(0L, false);
    }
    
    public int peek(final long timeout) throws IOException {
        return this.read(timeout, true);
    }
    
    public int read(final long timeout) throws IOException {
        return this.read(timeout, false);
    }
    
    @Override
    public int read(final byte[] b, final int off, final int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        }
        if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        }
        if (len == 0) {
            return 0;
        }
        final int c = this.read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;
        return 1;
    }
    
    public int readBuffered(final byte[] b) throws IOException {
        return this.readBuffered(b, 0L);
    }
    
    public int readBuffered(final byte[] b, final long timeout) throws IOException {
        return this.readBuffered(b, 0, b.length, timeout);
    }
    
    public int readBuffered(final byte[] b, final int off, final int len, final long timeout) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        }
        if (off < 0 || len < 0 || off + len < b.length) {
            throw new IllegalArgumentException();
        }
        if (len == 0) {
            return 0;
        }
        final Timeout t = new Timeout(timeout);
        int nb = 0;
        while (!t.elapsed()) {
            final int r = this.read((nb > 0) ? 1L : t.timeout());
            if (r < 0) {
                return (nb > 0) ? nb : r;
            }
            b[off + nb++] = (byte)r;
            if (nb >= len) {
                break;
            }
            if (t.isInfinite()) {
                break;
            }
        }
        return nb;
    }
    
    public void shutdown() {
    }
    
    public abstract int read(final long p0, final boolean p1) throws IOException;
}
