⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpinputstream.java

📁 用java实现的摄像头编程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Version 0.70 01/04/2002
 *
 * Visit my url for update: http://www.geocities.com/beapetrovicova/
 * 
 * jFtp was developed by Bea Petrovicova <beapetrovicova@yahoo.com>.
 * The design and implementation of jFtp are available for royalty-free 
 * adoption and use. This software is provided 'as is' without any 
 * guarantees. Copyright is retained by Bea Petrovicova. Redistribution 
 * of any part of jFtp or any derivative works must include this notice.
 * 
 */
package cz.dhl.ftp;

import cz.dhl.ui.CoConsole;
import java.io.InputStream;
import java.io.IOException;

/**
 * Allows reading from FTP file.
 * 
 * @Version 0.70 01/04/2002
 * @author Bea Petrovicova <beapetrovicova@yahoo.com>
 * @see Ftp
 * @see FtpFile
 */
public class FtpInputStream extends InputStream 
{  Ftp client;
   FtpDataSocket data;
   InputStream stream;

   FtpInputStream() {}

   /** Open 'retreive' InputStream for given filename.
    * <P><B>RETR</B> - retrieve</P>
    * <P>This command causes the server-DTP to transfer a copy of 
    * the file, specified in the pathname, to the server or 
    * user-DTP at the other end of the data connection. 
    * The status and contents of the file at the server site 
    * shall be unaffected.</P>
    * @param file the file to be opened for reading
    * @exception IOException socket error
    * @exception FileNotFoundException Server file not found. */
   public FtpInputStream(FtpFile file) 
      throws IOException
   {  client=null;
      data=new FtpDataSocket(file.client); 
      stream=data.getInputStream("RETR " + file,file.getDataType()); }

   /** Open 'retreive' concurent InputStream for given filename. 
    * <P>Single ftp connection cannot handle <B>multiple concurent 
    * data transfers</B>. This limitation can be eliminated by 
    * creating new ftp connection for each concurent data 
    * transfer. This constructor creates separate Ftp instance.</P>
    * <P>Note (1) supplying same CoConsole instance for 
    * multiple sessions will produce messed output.</P>
    * <P>Note (2) This code may need to be run in separate 
    * thread to process multiple files concurently.</P>
    * <P><B>RETR</B> - retrieve</P>
    * This command causes the server-DTP to transfer a copy of 
    * the file, specified in the pathname, to the server or 
    * user-DTP at the other end of the data connection. 
    * The status and contents of the file at the server site 
    * shall be unaffected.</P>
    * @param file the file to be opened for reading
    * @param connect login details
    * @param console message output
    * @exception IOException socket error
    * @see #FtpInputStream(FtpFile) */
   public FtpInputStream(FtpFile file,FtpConnect connect,CoConsole console) 
      throws IOException
   {  client = new Ftp(); 
      if(client.connect(connect))
      {  if(console!=null)
	    client.getContext().setConsole(console);
         file = new FtpFile(file.toString(),client);
         data=new FtpDataSocket(file.client); 
         stream=data.getInputStream("RETR " + file,file.getDataType()); 
      } else throw new IOException("Connect failed.");
   } 

   /** Close current data transfer and close data connection.
    * <P>If no reply <B>ABOR</B> - abort</P>
    * <P>This command tells the server to abort the previous FTP 
    * service command and any associated transfer of data. No 
    * action is to be taken if the previous command has been 
    * completed (including data transfer). The control connection 
    * is not to be closed by the server, but the data connection 
    * must be closed. There are two cases for the server upon 
    * receipt of this command:</P>
    * <P>(1) the FTP service command was already completed. The 
    * server closes the data connection (if it is open) and 
    * responds with a 226 reply, indicating that the abort 
    * command was successfully processed.</P>
    * <P>(2) the FTP service command is still in progress. The 
    * server aborts the FTP service in progress and closes the 
    * data connection, returning a 426 reply to indicate that 
    * the service request terminated abnormally. The server then 
    * sends a 226 reply, indicating that the abort command was 
    * successfully processed.</P>
    * @exception IOException socket error */
   public void close() throws IOException 
   {  IOException x = null;
      while (stream!=null || data!=null || client!=null)
	 try 
	 {  InputStream i; FtpDataSocket d; Ftp c;
	    if(stream!=null) { i=stream; stream = null; i.close(); }
	    if(data!=null) { d=data; data = null; d.close(); }
	    if(client!=null) { c=client; client = null; c.disconnect(); }
	 } catch(IOException e) { x=e; }
      if(x!=null) throw x;
   }
   
   /**
    * Reads the next byte of data from the input stream. The value byte is
    * returned as an <code>int</code> in the range <code>0</code> to
    * <code>255</code>. If no byte is available because the end of the stream
    * has been reached, the value <code>-1</code> is returned. This method
    * blocks until input data is available, the end of the stream is detected,
    * or an exception is thrown.
    *
    * @return     the next byte of data, or <code>-1</code> if the end of the
    *             stream is reached.
    * @exception  IOException  if an I/O error occurs.
    */
   public int read() throws IOException { return stream.read(); }

   /**
    * Reads some number of bytes from the input stream and stores them into
    * the buffer array <code>b</code>. The number of bytes actually read is
    * returned as an integer.  This method blocks until input data is
    * available, end of file is detected, or an exception is thrown.
    *
    * <p> If <code>b</code> is <code>null</code>, a
    * <code>NullPointerException</code> is thrown.  If the length of
    * <code>b</code> is zero, then no bytes are read and <code>0</code> is
    * returned; otherwise, there is an attempt to read at least one byte. If
    * no byte is available because the stream is at end of file, the value
    * <code>-1</code> is returned; otherwise, at least one byte is read and
    * stored into <code>b</code>.
    *
    * <p> The first byte read is stored into element <code>b[0]</code>, the
    * next one into <code>b[1]</code>, and so on. The number of bytes read is,
    * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
    * number of bytes actually read; these bytes will be stored in elements
    * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
    * leaving elements <code>b[</code><i>k</i><code>]</code> through
    * <code>b[b.length-1]</code> unaffected.
    *
    * <p> If the first byte cannot be read for any reason other than end of
    * file, then an <code>IOException</code> is thrown. In particular, an
    * <code>IOException</code> is thrown if the input stream has been closed.
    *
    * <p> The <code>read(b)</code> method for class <code>InputStream</code>
    * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
    *
    * @param      b   the buffer into which the data is read.
    * @return     the total number of bytes read into the buffer, or
    *             <code>-1</code> is there is no more data because the end of
    *             the stream has been reached.
    * @exception  IOException  if an I/O error occurs.
    * @see        cz.dhl.ftp.FtpInputStream#read(byte[], int, int)
    */
   public int read(byte b[]) throws IOException { return stream.read(b); }

   /**
    * Reads up to <code>len</code> bytes of data from the input stream into
    * an array of bytes.  An attempt is made to read as many as
    * <code>len</code> bytes, but a smaller number may be read, possibly

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -