package com.jscape.inet.ftp;

import com.jscape.util.IOTools;

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

/**
 * Special InputStream implementation
 *
 * @author Alvaro
 */
public class FtpInputStream
        extends InputStream {
    /**
     * Current Ftp instance
     */
    private Ftp ftp;

    /**
     * If it is true then allows to disconnect the ftp connection
     */
    private boolean close;
    /**
     * It is the internal InputStream
     */
    private InputStream in;

    /**
     * Creates a new FtpInputStream instance and keeps the ftp connection open
     *
     * @param ftp      Current ftp instance
     * @param connect  <code>true</code> to establish a ftp connection,
     *                 <code>false</code> otherwise
     * @param close    <code>true</code> to disconnect the ftp connection,
     *                 <code>false</code> otherwise
     * @param fileName The file name which is going to be used to create
     *                 the inputstream
     * @param pos      The offset
     *
     * @throws FtpException
     * @throws IOException
     */
    public FtpInputStream(Ftp ftp, boolean connect, boolean close,
                          String fileName, long pos)
            throws IOException, FtpException {
        this.ftp = ftp;
        if (connect) {
            this.ftp.connect();
        }
        in = this.ftp.getInputStream(fileName, pos);
        this.close = close;
    }

    /**
     * Method read
     * It invokes to the read method of the internal InputStream instance
     *
     * @see java.io.InputStream#read()
     */
    public int read()
            throws IOException {
        return in.read();
    }

    /**
     * Method read
     * It invokes to the read method of the internal InputStream instance
     *
     * @see java.io.InputStream#read(byte[])
     */
    public int read(byte[] b)
            throws IOException {
        return in.read(b);
    }

    /**
     * Method read
     * It invokes to the read method of the internal InputStream instance
     *
     * @see java.io.InputStream#read(byte[], int, int)
     */
    public int read(byte[] b, int off, int len)
            throws IOException {
        return in.read(b, off, len);
    }

    /**
     * Method close
     * If close is true then it disconnect the ftp connection,
     * and reads the last response from the server
     *
     * @see java.io.OutputStream#close()
     */
    public void close() {
        IOTools.close(this.in);
        if (this.close) {
            this.ftp.disconnect();
        }
    }

    /**
     * Method available
     * It invokes to the available method of the internal InputStream instance
     *
     * @see java.io.InputStream#available()
     */
    public synchronized int available()
            throws IOException {
        return in.available();
    }

    /**
     * Method skip
     * It invokes to the skip method of the internal InputStream instance
     *
     * @see java.io.InputStream#skip(long)
     */
    public synchronized long skip(long n)
            throws IOException {
        return in.skip(n);
    }

}

