1 /************************************************************
2 * Copyright *
3 * Portions of this software are Copyright (c) 1993 - 2002, *
4 * Chad Z. Hower (Kudzu) and the Indy Pit Crew *
5 * - http://www.nevrona.com/Indy/ *
6 ************************************************************/
7 package org.indy.io;
8
9 import java.util.Map;
10
11 import javax.swing.event.EventListenerList;
12
13 import org.indy.Status;
14
15 import org.indy.util.IndyUtilities;
16
17
18 /***
19 * A skeleton implementation of {@link IOHandler}. This provides no-ops for
20 * the non base IO methods of <code>IOHandler</code>, property accessors and mutators and
21 * listener management for concrete subclasses.
22 *
23 *@author Owen Green
24 */
25 public abstract class AbstractIOHandler implements IOHandler {
26 /***
27 * The timeout to be used for read operations on this <code>IOHandler</code>.
28 * Initially set to the Indy default value
29 *
30 * @see org.indy.util.IndyUtilities#getDefaultTimeout()
31 */
32 protected int readTimeOut = IndyUtilities.getDefaultTimeout();
33
34 /***
35 * The maximum line length to be used for read line operations
36 * Initially set to the Indy default value
37 *
38 * @see org.indy.util.IndyUtilities#getDefaultMaxLineLength()
39 */
40 protected int maximumLineLength = IndyUtilities.getDefaultMaxLineLength();
41
42 /***
43 * Holds a list of interested listeners
44 */
45 protected EventListenerList listenerList = new EventListenerList();
46
47 /***
48 * Provides a no-op implementation of <code>AfterAccept()</code>
49 *
50 * @see IOHandler#afterAccept
51 */
52 public void afterAccept() {
53 }
54
55 /***
56 * Provides a a no-op implementation of <code>close()</code>
57 *
58 * @see IOHandler#close()
59 */
60 public void close() {
61 }
62
63 /***
64 * A no-op implementation of {@link IOHandler#connectClient(Map,int)}.
65 *
66 * @param parameters The parameters to use for connection
67 * @param timeout The timeout to use for connection
68 * @throws IndyIOException If an IO error ocurrs
69 * @see IOHandler#connectClient(Map,int)
70 */
71 public void connectClient(Map parameters, int timeout)
72 throws IndyIOException {
73 }
74
75 /***
76 * Provides a no-op imlpementaion of <code>open()</code>
77 *
78 * @see IOHandler#open
79 */
80 public void open() {
81 }
82
83 /***
84 * Reads a sequence of bytes from the socket, using the
85 * readTimeOut property, if set, or the Indy default timeout
86 * otherwise.
87 *
88 *@param b An array of bytes to read the data into
89 *@return The number of bytes actually read
90 *@throws IndyIOException If an IO error occurs.
91 *@throws PeerDisconnectedException if the remote machine has disconnected
92 *@throws ReadTimedOutException if the read opertaion timed out
93 *@throws NotConnectedException if the socket is not connected
94 *@see read(byte[],int,int,int)
95 */
96 public int read(byte[] b) throws IndyIOException {
97 if (b == null) {
98 throw new NullPointerException();
99 }
100
101 return read(b, b.length);
102 }
103
104 /***
105 * Reads a sequence of bytes from the socket, using the
106 * readTimeOut property, if set, or the Indy default timeout
107 * otherwise.
108 *
109 *@param b An array of bytes to read the data into
110 *@param len The amount of data to read
111 *@return The number of bytes actually read
112 *@throws IndyIOException If an IO error occurs.
113 *@throws PeerDisconnectedException if the remote machine has disconnected
114 *@throws ReadTimedOutException if the read opertaion timed out
115 *@throws NotConnectedException if the socket is not connected
116 *@see read(byte[],int,int,int)
117 */
118 public int read(byte[] b, int len) throws IndyIOException,
119 ReadTimedOutException {
120 return read(b, 0, len);
121 }
122
123 /***
124 * Reads a sequence of bytes from the socket, using the
125 * readTimeOut property, if set, or the Indy default timeout
126 * otherwise.
127 *
128 *@param b An array of bytes to read the data into
129 *@param off The offset at which to start writing to <code>b</code>
130 *@param len The amount of data to read
131 *@return The number of bytes actually read
132 *@throws IndyIOException If an IO error occurs.
133 *@throws PeerDisconnectedException if the remote machine has disconnected
134 *@throws ReadTimedOutException if the read opertaion timed out
135 *@throws NotConnectedException if the socket is not connected
136 *@see read(byte[],int,int,int)
137 */
138 public int read(byte[] b, int off, int len) throws IndyIOException,
139 ReadTimedOutException {
140 return read(b, off, len, readTimeOut);
141 }
142
143 /***
144 * Reads a line of text from the socket using the
145 * <code>maximumLineLength</code> and <code>readTimeOut</code>
146 * properties.
147 *
148 * @return A line of data from the socket
149 * @throws IndyIOException if an IO error occurs
150 * @throws PeerDisconnectedException if the remote machine disconnects normally
151 * @throws ReadTimedOutException if the operation times out
152 * @throws MaxLineLengthExceededException if the maximum line length is exceeded before a line break is found
153 * @see getReadTimeOut()
154 * @see setReadTimeOut(int,int)
155 * @see getMaximumLineLength()
156 * @see setMaximumLineLength(int)
157 */
158 public String readLine() throws IndyIOException {
159 return readLine(readTimeOut, maximumLineLength);
160 }
161
162 /***
163 * Reads a line of data from the socket
164 *
165 * @param timeout The timeout to use for this operation
166 * @param maxLineLength The maxiumum line length to accept
167 * @return A line of data from the socket
168 * @throws IndyIOException if an IO error occurs
169 * @throws PeerDisconnectedException if the remote machine disconnects normally
170 * @throws ReadTimedOutException if the operation times out
171 * @throws MaxLineLengthExceededException if the maximum line length is exceeded before a line break is found
172 * @see readLine(int,int,String)
173 */
174 public String readLine(int timeout, int maxLineLength)
175 throws IndyIOException, PeerDisconnectedException,
176 ReadTimedOutException, MaxLineLengthExceededException {
177 return readLine(timeout, maxLineLength, null);
178 }
179
180 /***
181 * Returns the current timeout for read operations on this <code>IOHandler</code>
182 *
183 * @return The read timeout for this
184 */
185 public int getReadTimeOut() throws IndyIOException {
186 return readTimeOut;
187 }
188
189 /***
190 * Set the timeout for read operations on this <code>IOHandler</code>
191 *
192 * @param newTimeout The new timeout value to use
193 * @throws IllegalArgumentException If the <code>newTimeout</code> is < 0
194 */
195 public void setReadTimeOut(int newTimeout) throws IndyIOException {
196 if (newTimeout < 0) {
197 /*** @todo Resource message */
198 throw new IllegalArgumentException();
199 }
200
201 readTimeOut = newTimeout;
202 }
203
204 /***
205 * Gets the maximum line length to be used for read line operations
206 *
207 * @return The maximum line length accepted by this <code>IOHandler</code
208 */
209 public int getMaximumLineLength() {
210 return maximumLineLength;
211 }
212
213 /***
214 * Sets the maximum line length to be acceped by this <code>IOHandler</code>
215 *
216 * @param newMaximumLineLength The new maximum line length
217 * @throws IllegalArgumentException if <code>newMaximumLineLength</code> is < 0
218 */
219 public void setMaximumLineLength(int newMaximumLineLength) {
220 if (newMaximumLineLength < 0) {
221 throw new IllegalArgumentException();
222 }
223
224 maximumLineLength = newMaximumLineLength;
225 }
226
227 /***
228 * Add a {@link IOHandlerListener} to receive events from this <code>IOHandler</code>
229 *
230 * @param l The <code>IOHandlerListener</code>
231 * @see removeIOHandlerListener(IOHandlerListener)
232 */
233 public void addIOHandlerListener(IOHandlerListener l) {
234 listenerList.add(IOHandlerListener.class, l);
235 }
236
237 /***
238 * Remove a {@link IOHandlerListener} from the listener list.
239 *
240 * @param l The <code>IOHandlerListener</code>
241 * @see addIOHandlerListener(IOHandlerListener)
242 */
243 public void removeIOHandlerListener(IOHandlerListener l) {
244 listenerList.remove(IOHandlerListener.class, l);
245 }
246
247 /***
248 * Fires an onDisconnect event to all registered listeners
249 */
250 protected void doOnDisconnect() {
251 Object[] listeners = listenerList.getListenerList();
252
253 for (int i = listeners.length - 2; i >= 0; i -= 2) {
254 if (listeners[i] == IOHandlerListener.class) {
255 ((IOHandlerListener) listeners[i + 1]).onDisconnect(this);
256 }
257 }
258 }
259
260 /***
261 * Fires an onConnect event to all registered listeners
262 */
263 protected void doOnConnect() {
264 Object[] listeners = listenerList.getListenerList();
265
266 for (int i = listeners.length - 2; i >= 0; i -= 2) {
267 if (listeners[i] == IOHandlerListener.class) {
268 ((IOHandlerListener) listeners[i + 1]).onConnect(this);
269 }
270 }
271 }
272
273 /***
274 * Fires an OnStatus event to all registered listeners
275 */
276 protected void doStatus(Status s, Object[] args) {
277 Object[] listeners = listenerList.getListenerList();
278
279 for (int i = listeners.length - 2; i >= 0; i -= 2) {
280 if (listeners[i] == IOHandlerListener.class) {
281 ((IOHandlerListener) listeners[i + 1]).onStatus(this, s, args);
282 }
283 }
284 }
285 }
This page was automatically generated by Maven