Initialization
[xylftp.git] / client / gui / src / XylFTPConnection.java
blob4fe58223debb4699bbc7b1b0a077cc3a683d9d33
1 //////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Zhou Xiao-wei, Apr. 2007.
3 // Copyright (C) Wang Cong, Apr. 2007.
4 // See AUTHORS and CREDITS to learn more.
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation.
8 // See COPYING to learn more.
9 //////////////////////////////////////////////////////////////////////////
11 import java.awt.Container;
12 import java.net.*;
13 import java.io.*;
15 /**
16 * This class is for FTP connection.
17 * Extends XylTelnetConnection.
18 * @author WANG Cong, Zhou Xiao-Wei
19 * @version 1.14
21 public class XylFTPConnection extends XylTelnetConnection{
23 /**
24 *The size of reader's buffer.
26 private final int RDBUF_SIZE = 1024;
28 /**
29 *The size of writer's buffer.
31 private final int WRBUF_SIZE = 1024;
33 /**
34 *The status of FTP connection.
36 private int Status;
37 // 0 for not connected
38 // 1 for connected but not login
39 // 2 for login and no data transfer
40 // 3 for login and getting data down
41 // 4 for login and putting data up
43 /**
44 *Presents the mode (active or passive) of FTP connection.
46 private int Mode;
47 //1 for passive, 0 for active, default to 1. ;)
49 /**
50 *Constructs a LocalFile for data storage.
52 private String LocalFile;
53 // Empty string for this means stdout. ;)
55 /**
56 *The data connection socket.
58 private Socket DataSocket;
60 /**
61 *The sever data socket, used by active mode.
63 private ServerSocket DataServerSocket;
65 /**
66 *The input stream.
68 private BufferedInputStream FTPInStream;
70 /**
71 *The output stream.
73 private BufferedOutputStream FTPOutStream;
75 /**
76 *Initializes some values.
78 XylFTPConnection(){
79 super();
80 Status = 0;
81 Mode = 1;
82 LocalFile = null;
83 DataSocket = null;
84 DataServerSocket = null;
87 /**
88 *Gets status of current FTP connection.
89 *@return the status of connection
91 public int GetStatus(){
92 return Status;
95 /**
96 *Sets status of the connection.
97 *@param NewStatus
99 public void SetStatus(int NewStatus){
100 Status = NewStatus;
104 *Sets the LocalFile name.
105 *@param FileName
107 public void SetLocalFile(String FileName){
108 LocalFile = FileName;
112 *Tests whether the mode is passive
114 public boolean IsPassive(){
115 if (Mode==1)
116 return true;
117 else
118 return false;
121 *Sets the passive mode.
123 public void SetPassive(){
124 Mode = 1;
128 *Sets the active mode.
130 public void SetActive(){
131 Mode = 0;
135 *Opens the data connection in active mode.
136 *@see Exception
137 *@throws Exception on opening dataconnection error
139 public void OpenDataConnection() throws Exception{
140 try {
141 DataSocket = DataServerSocket.accept();
142 } catch(Exception e) {
143 SetStatus(2);
144 throw e;
148 public void ListenForDataConnection() throws Exception{
149 try {
150 if(-1 == ReturnSelfPort())
151 throw new XylFTPException("Port is unavailable.");
152 DataServerSocket = new ServerSocket(ReturnSelfPort());
153 DataServerSocket.setSoTimeout(Timeout);
154 } catch(Exception e) {
155 SetStatus(2);
156 throw e;
161 *Opens the data connection in passive mode.
162 *@param Port the port of server
163 *@param Host the IP of server
164 *@see Exception
165 *@throws Exception on opening DataConnection error
167 public void OpenDataConnection(String Host, int Port) throws Exception{
168 try {
169 DataSocket = new Socket(Host, Port);
170 DataSocket.setSoTimeout(Timeout);
171 } catch (Exception e) {
172 SetStatus(2);
173 throw e;
178 *Closes the data connection.
179 *@see XylFTPException
180 *@throws XylFTPException on closing DataConnection error
182 public void CloseDataConnection() throws Exception{
183 if(DataSocket != null) {
184 DataSocket.close();
185 DataSocket = null;
187 if(DataServerSocket != null) {
188 DataServerSocket.close();
189 DataServerSocket = null;
194 *Uploads a file in active mode.
195 *@throws XylFTPException on sending file error
196 *@see XylFTPException
198 public void SendFileActive() throws Exception{
199 int bytesRead;
200 byte [] buf = new byte[WRBUF_SIZE];
201 if (Mode!=0) {
202 SetStatus(2);
203 throw new XylFTPException("You are not in active mode!");
205 if (LocalFile.equals("")) {
206 SetStatus(2);
207 throw new XylFTPException("Can't send unnamed file!");
208 } else {
209 FTPInStream = new BufferedInputStream(new FileInputStream(LocalFile));
211 FTPOutStream = new BufferedOutputStream(DataSocket.getOutputStream());
212 while ((bytesRead = FTPInStream.read(buf, 0, WRBUF_SIZE)) > 0) {
213 FTPOutStream.write(buf, 0, bytesRead);
215 FTPOutStream.flush();
216 FTPOutStream.close();
217 FTPInStream.close();
220 *Uploads a file in passive mode.
221 *@throws XylFTPException on sending file error
222 *@see XylFTPException
224 public void SendFilePassive() throws Exception{
225 int bytesRead;
226 byte [] buf = new byte[WRBUF_SIZE];
227 if (Mode!=1) {
228 SetStatus(2);
229 throw new XylFTPException("You are not in passive mode!");
231 if (LocalFile.equals("")) {
232 SetStatus(2);
233 throw new XylFTPException("Can't send unnamed file!");
234 } else {
235 FTPInStream = new BufferedInputStream(new FileInputStream(LocalFile));
237 FTPOutStream = new BufferedOutputStream(DataSocket.getOutputStream());
238 while ((bytesRead = FTPInStream.read(buf, 0, WRBUF_SIZE)) > 0) {
239 FTPOutStream.write(buf, 0, bytesRead);
241 FTPOutStream.flush();
242 FTPOutStream.close();
243 FTPInStream.close();
247 *Downloads a file in active mode.
248 *@throws XylFTPException on getting file error
249 *@see XylFTPException
251 public void GetFileActive() throws Exception{
252 int bytesRead;
253 byte [] buf = new byte[RDBUF_SIZE];
254 if (Mode!=0) {
255 SetStatus(2);
256 throw new XylFTPException("You are not in active mode!");
258 if (LocalFile.equals("")) {
259 FTPOutStream = new BufferedOutputStream((OutputStream)System.out);
260 } else {
261 FTPOutStream = new BufferedOutputStream(new FileOutputStream(LocalFile));
263 FTPInStream = new BufferedInputStream(DataSocket.getInputStream());
264 while ((bytesRead = FTPInStream.read(buf, 0, RDBUF_SIZE)) > 0) {
265 FTPOutStream.write(buf, 0, bytesRead);
267 FTPOutStream.flush();
268 if (!LocalFile.equals("")) //We can NOT close the stdout.
269 FTPOutStream.close();
270 FTPInStream.close();
274 *Downloads a file in passive mode.
275 *@throws XylFTPException on getting file error
276 *@see XylFTPException
278 public void GetFilePassive() throws Exception{
279 int bytesRead;
280 byte [] buf = new byte[RDBUF_SIZE];
281 if (Mode!=1) {
282 SetStatus(2);
283 throw new XylFTPException("You are not in passive mode!");
285 if (LocalFile.equals("")) {
286 FTPOutStream = new BufferedOutputStream((OutputStream)System.out);
287 } else {
288 FTPOutStream = new BufferedOutputStream(new FileOutputStream(LocalFile));
290 FTPInStream = new BufferedInputStream(DataSocket.getInputStream());
291 while ((bytesRead = FTPInStream.read(buf, 0, RDBUF_SIZE)) > 0) {
292 FTPOutStream.write(buf, 0, bytesRead);
294 FTPOutStream.flush();
295 if (!LocalFile.equals("")) //We can NOT close the stdout.
296 FTPOutStream.close();
297 FTPInStream.close();
300 public String GetListActive() throws Exception{
301 int bytesRead;
302 byte [] buf = new byte[RDBUF_SIZE];
303 if (Mode!=0) {
304 SetStatus(2);
305 throw new XylFTPException("You are not in active mode!");
308 ByteArrayOutputStream bArrayStream = new ByteArrayOutputStream();
309 FTPOutStream = new BufferedOutputStream(bArrayStream);
311 FTPInStream = new BufferedInputStream(DataSocket.getInputStream());
312 while ((bytesRead = FTPInStream.read(buf, 0, RDBUF_SIZE)) > 0) {
313 FTPOutStream.write(buf, 0, bytesRead);
315 FTPOutStream.flush();
316 FTPOutStream.close();
317 FTPInStream.close();
319 return bArrayStream.toString();
322 public String GetListPassive() throws Exception{
323 int bytesRead;
324 byte [] buf = new byte[RDBUF_SIZE];
325 if (Mode!=1) {
326 SetStatus(2);
327 throw new XylFTPException("You are not in passive mode!");
330 ByteArrayOutputStream bArrayStream = new ByteArrayOutputStream();
331 FTPOutStream = new BufferedOutputStream(bArrayStream);
333 FTPInStream = new BufferedInputStream(DataSocket.getInputStream());
334 while ((bytesRead = FTPInStream.read(buf, 0, RDBUF_SIZE)) > 0) {
335 FTPOutStream.write(buf, 0, bytesRead);
337 FTPOutStream.flush();
338 FTPOutStream.close();
339 FTPInStream.close();
341 return bArrayStream.toString();