DataBufferInputStream.java 2.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.service.DataBuffer
 */
package com.adobe.aemfd.docmanager.internal.io;

import com.adobe.service.DataBuffer;
import java.io.IOException;
import java.io.InputStream;

public class DataBufferInputStream
extends InputStream {
    private DataBuffer dataBuffer;
    private long _pos = 0;
    private long _len;
    private boolean closed = false;

    public DataBufferInputStream(DataBuffer dataBuffer) {
        this.dataBuffer = dataBuffer;
        this._len = dataBuffer.getBufLength();
    }

    private void checkClosed() throws IOException {
        if (this.closed) {
            throw new IOException("The stream is closed!");
        }
    }

    public int available() throws IOException {
        this.checkClosed();
        if (this._len == -1) {
            return super.available();
        }
        if (this._pos >= this._len) {
            return 0;
        }
        long avb = this._len - this._pos;
        return avb > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)avb;
    }

    public int read() throws IOException {
        byte[] buf = new byte[1];
        int rd = this.read(buf);
        return rd == -1 ? -1 : buf[0];
    }

    public int read(byte[] b, int off, int len) throws IOException {
        int toRead;
        this.checkClosed();
        if (b == null) {
            throw new NullPointerException();
        }
        if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        }
        if (len == 0) {
            return 0;
        }
        int n = toRead = this._len == -1 ? len : Math.min(len, this.available());
        if (toRead == 0) {
            return -1;
        }
        byte[] rb = this.dataBuffer.getBytes(this._pos, (long)toRead);
        int actualRead = rb.length;
        if (actualRead == 0) {
            return -1;
        }
        System.arraycopy(rb, 0, b, off, actualRead);
        this._pos += (long)actualRead;
        return actualRead;
    }

    public long skip(long n) throws IOException {
        this.checkClosed();
        if (n <= 0) {
            return 0;
        }
        if (this._len == -1) {
            return 0;
        }
        int avb = this.available();
        if (avb == 0) {
            return 0;
        }
        long toSkip = Math.min(n, (long)avb);
        this._pos += toSkip;
        return toSkip;
    }

    public void close() throws IOException {
        this.dataBuffer = null;
        this.closed = true;
    }
}