/[classpath]/inetlib/source/gnu/inet/ftp/PassiveModeDTP.java
ViewVC logotype

Diff of /inetlib/source/gnu/inet/ftp/PassiveModeDTP.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by dog, Thu Oct 21 15:21:54 2004 UTC revision 1.7 by dog, Thu Nov 25 22:15:05 2004 UTC
# Line 1  Line 1 
1  /*  /*
2   * $Id$   * PassiveModeDTP.java
3   * Copyright (C) 2003 The Free Software Foundation   * Copyright (C) 2003 The Free Software Foundation
4   *   *
5   * This file is part of GNU inetlib, a library.   * This file is part of GNU inetlib, a library.
# Line 51  import java.net.Socket; Line 51  import java.net.Socket;
51   * input and output streams.   * input and output streams.
52   *   *
53   * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>   * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  * @version $Revision$ $Date$  
54   */   */
55  final class PassiveModeDTP implements DTP  final class PassiveModeDTP
56      implements DTP
57  {  {
58    
59    final String address;    final String address;
# Line 65  final class PassiveModeDTP implements DT Line 65  final class PassiveModeDTP implements DT
65    boolean inProgress;    boolean inProgress;
66    int transferMode;    int transferMode;
67    
68    PassiveModeDTP (String address, int port, InetAddress localhost,    PassiveModeDTP(String address, int port, InetAddress localhost,
69                    int connectionTimeout, int timeout)                   int connectionTimeout, int timeout)
70      throws IOException      throws IOException
71    {    {
72      this.address = address;      this.address = address;
73      this.port = port;      this.port = port;
74      completed = false;      completed = false;
75      inProgress = false;      inProgress = false;
76      socket = new Socket ();      socket = new Socket();
77      InetSocketAddress remote = new InetSocketAddress (address, port);      InetSocketAddress remote = new InetSocketAddress(address, port);
78      InetSocketAddress local = new InetSocketAddress (localhost, port + 1);      InetSocketAddress local = new InetSocketAddress(localhost, port + 1);
79      socket.bind (local);      socket.bind(local);
80      if (connectionTimeout > 0)      if (connectionTimeout > 0)
81        {        {
82          socket.connect (remote, connectionTimeout);          socket.connect(remote, connectionTimeout);
83        }        }
84      else      else
85        {        {
86          socket.connect (remote);          socket.connect(remote);
87        }        }
88      if (timeout > 0)      if (timeout > 0)
89        {        {
90          socket.setSoTimeout (timeout);          socket.setSoTimeout(timeout);
91        }        }
92    }    }
93    
94    /**    /**
95     * Returns an input stream from which a remote file can be read.     * Returns an input stream from which a remote file can be read.
96     */     */
97    public InputStream getInputStream () throws IOException    public InputStream getInputStream()
98        throws IOException
99    {    {
100      if (inProgress)      if (inProgress)
101        {        {
102          throw new IOException ("Transfer in progress");          throw new IOException("Transfer in progress");
103        }        }
104      switch (transferMode)      switch (transferMode)
105        {        {
106        case FTPConnection.MODE_STREAM:        case FTPConnection.MODE_STREAM:
107          in = new StreamInputStream (this, socket.getInputStream ());          in = new StreamInputStream(this, socket.getInputStream());
108          break;          break;
109        case FTPConnection.MODE_BLOCK:        case FTPConnection.MODE_BLOCK:
110          in = new BlockInputStream (this, socket.getInputStream ());          in = new BlockInputStream(this, socket.getInputStream());
111          break;          break;
112        case FTPConnection.MODE_COMPRESSED:        case FTPConnection.MODE_COMPRESSED:
113          in = new CompressedInputStream (this, socket.getInputStream ());          in = new CompressedInputStream(this, socket.getInputStream());
114          break;          break;
115        default:        default:
116          throw new IllegalStateException ("Invalid transfer mode");          throw new IllegalStateException("Invalid transfer mode");
117        }        }
118      in.setTransferComplete (false);      in.setTransferComplete(false);
119      return in;      return in;
120    }    }
121        
# Line 122  final class PassiveModeDTP implements DT Line 123  final class PassiveModeDTP implements DT
123     * Returns an output stream to which a local file can be written for     * Returns an output stream to which a local file can be written for
124     * upload.     * upload.
125     */     */
126    public OutputStream getOutputStream () throws IOException    public OutputStream getOutputStream()
127        throws IOException
128    {    {
129      if (inProgress)      if (inProgress)
130        {        {
131          throw new IOException ("Transfer in progress");          throw new IOException("Transfer in progress");
132        }        }
133      switch (transferMode)      switch (transferMode)
134        {        {
135        case FTPConnection.MODE_STREAM:        case FTPConnection.MODE_STREAM:
136          out = new StreamOutputStream (this, socket.getOutputStream ());          out = new StreamOutputStream(this, socket.getOutputStream());
137          break;          break;
138        case FTPConnection.MODE_BLOCK:        case FTPConnection.MODE_BLOCK:
139          out = new BlockOutputStream (this, socket.getOutputStream ());          out = new BlockOutputStream(this, socket.getOutputStream());
140          break;          break;
141        case FTPConnection.MODE_COMPRESSED:        case FTPConnection.MODE_COMPRESSED:
142          out = new CompressedOutputStream (this, socket.getOutputStream ());          out = new CompressedOutputStream(this, socket.getOutputStream());
143          break;          break;
144        default:        default:
145          throw new IllegalStateException("Invalid transfer mode");          throw new IllegalStateException("Invalid transfer mode");
146        }        }
147      out.setTransferComplete (false);      out.setTransferComplete(false);
148      return out;      return out;
149    }    }
150        
151    public void setTransferMode (int mode)    public void setTransferMode(int mode)
152    {    {
153      transferMode = mode;      transferMode = mode;
154    }    }
155        
156    public void complete ()    public void complete()
157    {    {
158      completed = true;      completed = true;
159      if (!inProgress)      if (!inProgress)
160        {        {
161          transferComplete ();          transferComplete();
162        }        }
163    }    }
164    
165    public boolean abort ()    public boolean abort()
166    {    {
167      completed = true;      completed = true;
168      transferComplete ();      transferComplete();
169      return inProgress;      return inProgress;
170    }    }
171    
# Line 171  final class PassiveModeDTP implements DT Line 173  final class PassiveModeDTP implements DT
173     * Called by DTPInputStream or DTPOutputStream when end of     * Called by DTPInputStream or DTPOutputStream when end of
174     * stream is reached.     * stream is reached.
175     */     */
176    public void transferComplete ()    public void transferComplete()
177    {    {
178      if (in != null)      if (in != null)
179        {        {
180          in.setTransferComplete (true);          in.setTransferComplete(true);
181        }        }
182      if (out != null)      if (out != null)
183        {        {
184          out.setTransferComplete (true);          out.setTransferComplete(true);
185        }        }
186      inProgress = false;      inProgress = false;
187      completed = completed || (transferMode == FTPConnection.MODE_STREAM);      completed = completed ||(transferMode == FTPConnection.MODE_STREAM);
188      if (completed && socket != null)      if (completed && socket != null)
189        {        {
190          try          try
191            {            {
192              socket.close ();              socket.close();
193            }            }
194          catch (IOException e)          catch (IOException e)
195            {            {
# Line 196  final class PassiveModeDTP implements DT Line 198  final class PassiveModeDTP implements DT
198    }    }
199    
200  }  }
201    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26