001: /**
002: *
003: * Copyright (C) 2000-2003 Enterprise Distributed Technologies Ltd
004: *
005: * www.enterprisedt.com
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Bug fixes, suggestions and comments should be should posted on
022: * http://www.enterprisedt.com/forums/index.php
023: *
024: * Change Log:
025: *
026: * $Log: FTPActiveDataSocket.java,v $
027: * Revision 1.6 2006/10/17 10:27:15 bruceb
028: * added closeChild()
029: *
030: * Revision 1.5 2005/06/03 11:26:25 bruceb
031: * comment change
032: *
033: * Revision 1.4 2004/07/23 08:27:03 bruceb
034: * made public cvsId
035: *
036: * Revision 1.3 2003/11/15 11:23:55 bruceb
037: * changes required for ssl subclasses
038: *
039: * Revision 1.1 2003/11/02 21:49:52 bruceb
040: * implement FTPDataSocket interface
041: *
042: *
043: */package com.enterprisedt.net.ftp;
044:
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.net.ServerSocket;
049: import java.net.Socket;
050:
051: import com.enterprisedt.util.debug.Logger;
052:
053: /**
054: * Active data socket handling class
055: *
056: * @author Bruce Blackshaw
057: * @version $Revision: 1.6 $
058: */
059: public class FTPActiveDataSocket implements FTPDataSocket {
060:
061: /**
062: * Revision control id
063: */
064: public static String cvsId = "@(#)$Id: FTPActiveDataSocket.java,v 1.6 2006/10/17 10:27:15 bruceb Exp $";
065:
066: /**
067: * Logging object
068: */
069: private static Logger log = Logger.getLogger("FTPActiveDataSocket");
070:
071: /**
072: * The underlying socket for Active connection.
073: */
074: protected ServerSocket sock = null;
075:
076: /**
077: * The socket accepted from server.
078: */
079: protected Socket acceptedSock = null;
080:
081: /**
082: * Constructor
083: *
084: * @param sock
085: * the server socket to use
086: */
087: protected FTPActiveDataSocket(ServerSocket sock) {
088: this .sock = sock;
089: }
090:
091: /**
092: * Set the TCP timeout on the underlying data socket(s).
093: *
094: * If a timeout is set, then any operation which takes longer than the
095: * timeout value will be killed with a java.io.InterruptedException.
096: *
097: * @param millis
098: * The length of the timeout, in milliseconds
099: */
100: public void setTimeout(int millis) throws IOException {
101: sock.setSoTimeout(millis);
102: if (acceptedSock != null)
103: acceptedSock.setSoTimeout(millis);
104: }
105:
106: /**
107: * Returns the local port to which this socket is bound.
108: *
109: * @return the local port number to which this socket is bound
110: */
111: public int getLocalPort() {
112: return sock.getLocalPort();
113: }
114:
115: /**
116: * Waits for a connection from the server and then sets the timeout when the
117: * connection is made.
118: *
119: * @throws IOException
120: * There was an error while waiting for or accepting a
121: * connection from the server.
122: */
123: protected void acceptConnection() throws IOException {
124: log.debug("Calling accept()");
125: acceptedSock = sock.accept();
126: acceptedSock.setSoTimeout(sock.getSoTimeout());
127: log.debug("accept() succeeded");
128: }
129:
130: /**
131: * If active mode, accepts the FTP server's connection - in PASV, we are
132: * already connected. Then gets the output stream of the connection
133: *
134: * @return output stream for underlying socket.
135: */
136: public OutputStream getOutputStream() throws IOException {
137: acceptConnection();
138: return acceptedSock.getOutputStream();
139: }
140:
141: /**
142: * If active mode, accepts the FTP server's connection - in PASV, we are
143: * already connected. Then gets the input stream of the connection
144: *
145: * @return input stream for underlying socket.
146: */
147: public InputStream getInputStream() throws IOException {
148: acceptConnection();
149: return acceptedSock.getInputStream();
150: }
151:
152: /**
153: * Closes underlying sockets
154: */
155: public void close() throws IOException {
156: closeChild();
157: sock.close();
158: log.debug("close() succeeded");
159: }
160:
161: /**
162: * Closes child socket
163: */
164: public void closeChild() throws IOException {
165: if (acceptedSock != null) {
166: acceptedSock.close();
167: acceptedSock = null;
168: log.debug("closeChild() succeeded");
169: }
170: }
171: }
|