44 |
final class PassiveModeDTP implements DTP |
final class PassiveModeDTP implements DTP |
45 |
{ |
{ |
46 |
|
|
47 |
final String address; |
final String address; |
48 |
final int port; |
final int port; |
49 |
Socket socket; |
Socket socket; |
50 |
DTPInputStream in; |
DTPInputStream in; |
51 |
DTPOutputStream out; |
DTPOutputStream out; |
52 |
boolean completed; |
boolean completed; |
53 |
boolean inProgress; |
boolean inProgress; |
54 |
int transferMode; |
int transferMode; |
55 |
|
|
56 |
PassiveModeDTP(String address, int port, InetAddress localhost) |
PassiveModeDTP(String address, int port, InetAddress localhost) |
57 |
throws IOException |
throws IOException |
58 |
{ |
{ |
59 |
this.address = address; |
this.address = address; |
60 |
this.port = port; |
this.port = port; |
61 |
completed = false; |
completed = false; |
62 |
inProgress = false; |
inProgress = false; |
63 |
socket = new Socket(address, port, localhost, port+1); |
socket = new Socket(address, port, localhost, port + 1); |
64 |
} |
} |
65 |
|
|
66 |
/** |
/** |
67 |
* Returns an input stream from which a remote file can be read. |
* Returns an input stream from which a remote file can be read. |
68 |
*/ |
*/ |
69 |
public InputStream getInputStream() |
public InputStream getInputStream() throws IOException |
70 |
throws IOException |
{ |
71 |
{ |
if (inProgress) |
72 |
if (inProgress) |
throw new IOException("Transfer in progress"); |
73 |
throw new IOException("Transfer in progress"); |
switch (transferMode) |
74 |
switch (transferMode) |
{ |
75 |
{ |
case FTPConnection.MODE_STREAM: |
76 |
case FTPConnection.MODE_STREAM: |
in = new StreamInputStream(this, socket.getInputStream()); |
77 |
in = new StreamInputStream(this, socket.getInputStream()); |
break; |
78 |
break; |
case FTPConnection.MODE_BLOCK:in = |
79 |
case FTPConnection.MODE_BLOCK: |
new BlockInputStream(this, socket.getInputStream()); |
80 |
in = new BlockInputStream(this, socket.getInputStream()); |
break; |
81 |
break; |
case FTPConnection.MODE_COMPRESSED:in = |
82 |
case FTPConnection.MODE_COMPRESSED: |
new CompressedInputStream(this, socket.getInputStream()); |
83 |
in = new CompressedInputStream(this, socket.getInputStream()); |
break; |
84 |
break; |
default:throw new IllegalStateException("Invalid transfer mode"); |
85 |
default: |
} |
86 |
throw new IllegalStateException("Invalid transfer mode"); |
in.setTransferComplete(false); |
87 |
} |
return in; |
88 |
in.setTransferComplete(false); |
} |
|
return in; |
|
|
} |
|
89 |
|
|
90 |
/** |
/** |
91 |
* 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 |
92 |
* upload. |
* upload. |
93 |
*/ |
*/ |
94 |
public OutputStream getOutputStream() |
public OutputStream getOutputStream() throws IOException |
95 |
throws IOException |
{ |
96 |
{ |
if (inProgress) |
97 |
if (inProgress) |
throw new IOException("Transfer in progress"); |
98 |
throw new IOException("Transfer in progress"); |
switch (transferMode) |
99 |
switch (transferMode) |
{ |
100 |
{ |
case FTPConnection.MODE_STREAM: |
101 |
case FTPConnection.MODE_STREAM: |
out = new StreamOutputStream(this, socket.getOutputStream()); |
102 |
out = new StreamOutputStream(this, socket.getOutputStream()); |
break; |
103 |
break; |
case FTPConnection.MODE_BLOCK:out = |
104 |
case FTPConnection.MODE_BLOCK: |
new BlockOutputStream(this, socket.getOutputStream()); |
105 |
out = new BlockOutputStream(this, socket.getOutputStream()); |
break; |
106 |
break; |
case FTPConnection.MODE_COMPRESSED:out = |
107 |
case FTPConnection.MODE_COMPRESSED: |
new CompressedOutputStream(this, socket.getOutputStream()); |
108 |
out = new CompressedOutputStream(this, socket.getOutputStream()); |
break; |
109 |
break; |
default:throw new IllegalStateException("Invalid transfer mode"); |
110 |
default: |
} |
111 |
throw new IllegalStateException("Invalid transfer mode"); |
out.setTransferComplete(false); |
112 |
} |
return out; |
113 |
out.setTransferComplete(false); |
} |
114 |
return out; |
|
115 |
} |
public void setTransferMode(int mode) |
116 |
|
{ |
117 |
public void setTransferMode(int mode) |
transferMode = mode; |
118 |
{ |
} |
119 |
transferMode = mode; |
|
120 |
} |
public void complete() |
121 |
|
{ |
122 |
public void complete() |
completed = true; |
123 |
{ |
if (!inProgress) |
124 |
completed = true; |
transferComplete(); |
125 |
if (!inProgress) |
} |
126 |
transferComplete(); |
|
127 |
} |
public boolean abort() |
128 |
|
{ |
129 |
public boolean abort() |
completed = true; |
130 |
{ |
transferComplete(); |
131 |
completed = true; |
return inProgress; |
132 |
transferComplete(); |
} |
133 |
return inProgress; |
|
134 |
} |
/* |
135 |
|
* Called by DTPInputStream or DTPOutputStream when end of |
136 |
/* |
* stream is reached. |
137 |
* Called by DTPInputStream or DTPOutputStream when end of |
*/ |
138 |
* stream is reached. |
public void transferComplete() |
139 |
*/ |
{ |
140 |
public void transferComplete() |
in.setTransferComplete(true); |
141 |
{ |
out.setTransferComplete(true); |
142 |
in.setTransferComplete(true); |
inProgress = false; |
143 |
out.setTransferComplete(true); |
completed = completed || (transferMode == FTPConnection.MODE_STREAM); |
144 |
inProgress = false; |
if (completed) |
145 |
completed = completed || (transferMode==FTPConnection.MODE_STREAM); |
{ |
146 |
if (completed) |
try |
147 |
{ |
{ |
148 |
try |
socket.close(); |
149 |
{ |
} |
150 |
socket.close(); |
catch(IOException e) |
151 |
} |
{ |
152 |
catch (IOException e) |
} |
153 |
{ |
} |
154 |
} |
} |
155 |
} |
|
|
} |
|
|
|
|
156 |
} |
} |