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. |
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; |
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 |
|
|
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 |
|
|
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 |
{ |
{ |
198 |
} |
} |
199 |
|
|
200 |
} |
} |
201 |
|
|