Source code: org/finj/FTPSocket.java
1 package org.finj;
2
3 import java.io.InputStream;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.net.InetAddress;
7 import java.net.Socket;
8 import java.net.ServerSocket;
9
10 /**
11 * This class wraps <code>java.net.Socket</code> and
12 * <code>java.net.ServerSocket</code> behind a common
13 * (limited) interface that mimics theirs.
14 *
15 * It only exsists to circumvent the absence of a common
16 * ancestor in their hierarchy, and to provide a
17 * transparent programming for both passive and
18 * non-passive data transfers, that require
19 * <code>Socket</code> and <code>ServerSocket</code>
20 * manipulation respectively.
21 *
22 * Copyright (C)
23 *
24 * This library is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.1 of the License, or (at your option) any later version.
28 *
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public
35 * License along with this library; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 *
38 * @author Javier Iglesias -- jiglesias@users.sourceforge.net
39 * @version $Id: FTPSocket.java,v 1.3 2003/10/22 08:26:25 jiglesia Exp $
40 * @since v1.0.2
41 */
42 public class FTPSocket extends Object {
43
44 /**
45 * Wrapped socket, either a <code>java.net.Socket</code>,
46 * or a <code>java.net.ServerSocket</code>
47 */
48 private Object socket = null;
49
50 /**
51 * Constructs a new instance of this class that will wrap
52 * the <code>socket</code>.
53 *
54 * @param socket the one to wrap.
55 * @since v1.0.2
56 */
57 public FTPSocket ( Socket socket ) {
58 this.socket = socket;
59 }
60
61 /**
62 * Constructs a new instance of this class that will wrap
63 * the <code>socket</code>.
64 *
65 * @param socket the one to wrap.
66 * @since v1.0.2
67 */
68 public FTPSocket ( ServerSocket socket ) {
69 this.socket = socket;
70 }
71
72 /**
73 * Returns info on the kind of socket wrapped.
74 *
75 * @return <code>true</code> if wrapped socket is an instance of
76 * <code>java.net.Socket</code>, false if it's an instance of
77 * <code>java.net.ServerSocket</code>.
78 * @since v1.0.2
79 */
80 public boolean isSocket ( ) {
81 return (socket instanceof Socket);
82 }
83
84 /**
85 * Returns info on the kind of socket wrapped.
86 *
87 * @return <code>true</code> if wrapped socket is an instance of
88 * <code>java.net.ServerSocket</code>, false if it's an instance of
89 * <code>java.net.Socket</code>.
90 * @since v1.0.2
91 */
92 public boolean isServerSocket ( ) {
93 return (socket instanceof ServerSocket);
94 }
95
96
97
98 /**
99 * Returns the value of this socket's localport field.
100 *
101 * @return the value of this socket's localport field.
102 * @since v1.0.2
103 */
104 public int getLocalPort ( ) {
105 if ( isSocket() ) {
106 return ((Socket)socket).getLocalPort();
107 } else {
108 return ((ServerSocket)socket).getLocalPort();
109 }
110 }
111
112 /**
113 * Returns the value of the wrapped socket's address field.
114 *
115 * @return the value of the wrapped socket's address field.
116 * @since v1.0.2
117 */
118 public InetAddress getInetAddress ( ) {
119 if ( isSocket() ) {
120 return ((Socket)socket).getInetAddress();
121 } else {
122 return ((ServerSocket)socket).getInetAddress();
123 }
124 }
125
126 /**
127 * Closes the wrapped socket.
128 *
129 * @exception IOException something goes wrong with
130 * the sockets, streams, ...
131 * @since v1.0.2
132 */
133 public void close ( ) throws IOException {
134 if ( isSocket() ) {
135 ((Socket)socket).close();
136 } else {
137 ((ServerSocket)socket).close();
138 }
139 }
140
141 /**
142 * Returns a string representation of the wrapped socket.
143 *
144 * @return <code>String</code> representation.
145 * @since v1.0.2
146 */
147 public String toString ( ) {
148 return socket.toString();
149 }
150
151 /**
152 * Returns an input stream for the wrapped socket.
153 *
154 * @return stream from where to read.
155 * @exception IOException something goes wrong with
156 * the sockets, streams, ...
157 * @since v1.0.2
158 */
159 public InputStream getInputStream ( ) throws IOException {
160 if ( isSocket() ) {
161 return ((Socket)socket).getInputStream();
162 } else {
163 // accept socket from server,
164 // get and return it's InputStream
165 return ((ServerSocket)socket).accept().getInputStream();
166 }
167 }
168
169 /**
170 * Returns an output stream for the wrapped socket.
171 *
172 * @return stream where to write.
173 * @exception IOException something goes wrong with
174 * the sockets, streams, ...
175 * @since v1.0.2
176 */
177 public OutputStream getOutputStream ( ) throws IOException {
178 if ( isSocket() ) {
179 return ((Socket)socket).getOutputStream();
180 } else {
181 // accept socket from server,
182 // get and return it's OutputStream
183 return ((ServerSocket)socket).accept().getOutputStream();
184 }
185 }
186 }