KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > enterprisedt > net > ftp > FTPDataSocket


1 /**
2  *
3  * Java FTP client library.
4  *
5  * Copyright (C) 2000-2003 Enterprise Distributed Technologies Ltd
6  *
7  * www.enterprisedt.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  * Bug fixes, suggestions and comments should be sent to bruce@enterprisedt.com
24  *
25  * Change Log:
26  *
27  * $Log: FTPDataSocket.java,v $
28  * Revision 1.1.1.1 2005/06/23 15:22:58 smontoro
29  * hipergate backend
30  *
31  * Revision 1.1 2004/02/07 03:15:20 hipergate
32  * v2.0 pre-alpha
33  *
34  * Revision 1.3 2003/05/31 14:53:44 bruceb
35  * 1.2.2 changes
36  *
37  * Revision 1.2 2002/11/19 22:01:25 bruceb
38  * changes for 1.2
39  *
40  * Revision 1.1 2001/10/09 20:53:46 bruceb
41  * Active mode changes
42  *
43  * Revision 1.1 2001/10/05 14:42:03 bruceb
44  * moved from old project
45  *
46  */

47
48 package com.enterprisedt.net.ftp;
49
50 import java.io.IOException JavaDoc;
51 import java.io.InputStream JavaDoc;
52 import java.io.OutputStream JavaDoc;
53
54 import java.net.Socket JavaDoc;
55 import java.net.ServerSocket JavaDoc;
56
57
58 /**
59  * Supports client-side FTP DataSocket in Passive and Active Mode.
60  * Wrapper for Socket and ServerSocket. Methods are package access
61  * only - not for public use.
62  *
63  * @author Vladyslav Skarzhevsky
64  * @version $Revision: 1.1.1.1 $
65  */

66
67 public class FTPDataSocket {
68
69     /**
70      * Revision control id
71      */

72     private static String JavaDoc cvsId = "@(#)$Id: FTPDataSocket.java,v 1.1.1.1 2005/06/23 15:22:58 smontoro Exp $";
73
74     /**
75      * The underlying socket for Active connection.
76      */

77     private ServerSocket JavaDoc activeSocket = null;
78
79     /**
80      * The underlying socket for PASV connection or Socket accepted from server.
81      */

82     private Socket JavaDoc passiveSocket = null;
83
84     /**
85      * Create socket wrapper for Active connection.
86      */

87     FTPDataSocket(ServerSocket JavaDoc s) {
88          activeSocket = s;
89     }
90
91     /**
92      * Create socket wrapper for PASV connection.
93      */

94     FTPDataSocket(Socket JavaDoc s) {
95          passiveSocket = s;
96     }
97
98
99     /**
100      * Set the TCP timeout on the underlying control socket.
101      *
102      * If a timeout is set, then any operation which
103      * takes longer than the timeout value will be
104      * killed with a java.io.InterruptedException.
105      *
106      * @param millis The length of the timeout, in milliseconds
107      */

108     void setTimeout(int millis)
109         throws IOException JavaDoc {
110
111         if (passiveSocket != null)
112             passiveSocket.setSoTimeout(millis);
113         else if (activeSocket != null)
114             activeSocket.setSoTimeout(millis);
115     }
116
117
118     /**
119      * If active mode, accepts the FTP server's connection - in PASV,
120      * we are already connected. Then gets the output stream of
121      * the connection
122      *
123      * @return output stream for underlying socket.
124      */

125     OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
126
127         if (passiveSocket != null) {
128             return passiveSocket.getOutputStream();
129         }
130         else {
131             // accept socket from server, in Active mode
132
passiveSocket = activeSocket.accept();
133             // get and return its OutputStream
134
return passiveSocket.getOutputStream ();
135         }
136     }
137
138     /**
139      * If active mode, accepts the FTP server's connection - in PASV,
140      * we are already connected. Then gets the input stream of
141      * the connection
142      *
143      * @return input stream for underlying socket.
144      */

145     InputStream JavaDoc getInputStream() throws IOException JavaDoc {
146
147         if (passiveSocket != null) {
148             return passiveSocket.getInputStream();
149         } else {
150             // accept socket from server, in Active mode
151
passiveSocket = activeSocket.accept();
152             // get and return it's InputStream
153
return passiveSocket.getInputStream ();
154         }
155     }
156
157      /**
158       * Closes underlying sockets.
159       */

160     void close() throws IOException JavaDoc {
161
162         if (passiveSocket != null)
163             passiveSocket.close();
164         if (activeSocket != null)
165             activeSocket.close();
166     }
167 }
168
Popular Tags