Source code: com/enterprisedt/net/ftp/FTPDataSocket.java
1 /**
2 *
3 * Java FTP client library.
4 *
5 * Copyright (C) 2000-2001 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:
24 *
25 * bruceb@cryptsoft.com
26 *
27 * or by snail mail to:
28 *
29 * Bruce P. Blackshaw
30 * 53 Wakehurst Road
31 * London SW11 6DB
32 * United Kingdom
33 *
34 * Change Log:
35 *
36 * $Log: FTPDataSocket.java,v $
37 * Revision 1.1 2001/10/09 20:53:46 bruceb
38 * Active mode changes
39 *
40 * Revision 1.1 2001/10/05 14:42:03 bruceb
41 * moved from old project
42 *
43 */
44
45 package com.enterprisedt.net.ftp;
46
47 import java.io.*;
48 import java.net.*;
49 import java.util.*;
50
51 /**
52 * Supports client-side FTP DataSocket in Passive and Active Mode.
53 * Wrapper for Socket and ServerSocket. Methods are package access
54 * only - not for public use.
55 *
56 * @author Vladyslav Skarzhevskyy
57 * @version $Revision: 1.1 $
58 *
59 */
60
61 public class FTPDataSocket {
62
63 /**
64 * The underlying socket for Active connection.
65 */
66 private ServerSocket activeSocket = null;
67
68 /**
69 * The underlying socket for PASV connection or Socket acepted from server.
70 */
71 private Socket passiveSocket = null;
72
73 /**
74 * Create socket wrapper for Active connection.
75 */
76 FTPDataSocket(ServerSocket s) {
77 activeSocket = s;
78 }
79
80 /**
81 * Create socket pper for PASV connection.
82 */
83 FTPDataSocket(Socket s) {
84 passiveSocket = s;
85 }
86
87
88 /**
89 * Set the TCP timeout on the underlying control socket.
90 *
91 * If a timeout is set, then any operation which
92 * takes longer than the timeout value will be
93 * killed with a java.io.InterruptedException.
94 *
95 * @param millis The length of the timeout, in milliseconds
96 */
97 void setTimeout(int millis)
98 throws IOException {
99
100 if (passiveSocket != null)
101 passiveSocket.setSoTimeout(millis);
102 else if (activeSocket != null)
103 activeSocket.setSoTimeout(millis);
104 }
105
106
107 /**
108 * If active mode, accepts the FTP server's connection - in PASV,
109 * we are already connected. Then gets the output stream of
110 * the connection
111 *
112 * @return output stream for underlying socket.
113 */
114 OutputStream getOutputStream() throws IOException {
115
116 if (passiveSocket != null) {
117 return passiveSocket.getOutputStream();
118 }
119 else {
120 // accept socket from server, in Active mode
121 passiveSocket = activeSocket.accept();
122 // get and return its OutputStream
123 return passiveSocket.getOutputStream ();
124 }
125 }
126
127 /**
128 * If active mode, accepts the FTP server's connection - in PASV,
129 * we are already connected. Then gets the input stream of
130 * the connection
131 *
132 * @return input stream for underlying socket.
133 */
134 InputStream getInputStream() throws IOException {
135
136 if (passiveSocket != null) {
137 return passiveSocket.getInputStream();
138 } else {
139 // accept socket from server, in Active mode
140 passiveSocket = activeSocket.accept();
141 // get and return it's InputStream
142 return passiveSocket.getInputStream ();
143 }
144 }
145
146 /**
147 * Closes underlying sockets.
148 */
149 void close() throws IOException {
150
151 if (passiveSocket != null)
152 passiveSocket.close();
153 if (activeSocket != null)
154 activeSocket.close();
155 }
156 }