KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > BlockingConnection


1 // $Id: BlockingConnection.java 1584 2007-07-30 06:36:07Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.stream;
23
24 import java.io.IOException JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.net.InetSocketAddress JavaDoc;
28 import java.net.SocketTimeoutException JavaDoc;
29 import java.nio.BufferUnderflowException JavaDoc;
30 import java.nio.ByteBuffer JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.net.ssl.SSLContext;
36
37 import org.xsocket.ClosedConnectionException;
38 import org.xsocket.DataConverter;
39 import org.xsocket.MaxReadSizeExceededException;
40
41
42
43 /**
44  * Implementation of the <code>IBlockingConnection</code> interface. <br><br>
45  *
46  * A newly created connection is in the open state. The methods of this class are not thread-safe.
47  *
48  * @author grro@xsocket.org
49  */

50 public final class BlockingConnection extends Connection implements IBlockingConnection {
51
52     // read thread handling
53
private Object JavaDoc readGuard = new Object JavaDoc();
54     private long receiveTimeout = 0;
55
56
57
58     /**
59      * constructor. <br><br>
60      *
61      * @param hostname the remote host
62      * @param port the port of the remote host to connect
63      * @throws IOException If some other I/O error occurs
64      */

65     public BlockingConnection(String JavaDoc hostname, int port) throws IOException JavaDoc {
66         this(new InetSocketAddress JavaDoc(hostname, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), null, false);
67     }
68
69
70     /**
71      * constructor. <br><br>
72      * @deprecated
73      *
74      * @param hostname the remote host
75      * @param port the port of the remote host to connect
76      * @param socketConfiguration the socket configuration
77      * @throws IOException If some other I/O error occurs
78      */

79     public BlockingConnection(String JavaDoc hostname, int port, StreamSocketConfiguration socketConfiguration) throws IOException JavaDoc {
80         this(new InetSocketAddress JavaDoc(hostname, port), socketConfiguration.toOptions(), null, false);
81     }
82
83
84     /**
85      * constructor. <br><br>
86      *
87      * @param hostname the remote host
88      * @param port the port of the remote host to connect
89      * @param options the socket options
90      * @throws IOException If some other I/O error occurs
91      */

92     public BlockingConnection(String JavaDoc hostname, int port, Map JavaDoc<String JavaDoc, Object JavaDoc> options) throws IOException JavaDoc {
93         this(new InetSocketAddress JavaDoc(hostname, port), options, null, false);
94     }
95
96
97     /**
98      * constructor
99      *
100      * @param address the remote host address
101      * @param port the remote host port
102      * @throws IOException If some other I/O error occurs
103      */

104     public BlockingConnection(InetAddress JavaDoc address, int port) throws IOException JavaDoc {
105         this(new InetSocketAddress JavaDoc(address, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), null, false);
106     }
107
108     /**
109      * constructor
110      * @deprecated
111      *
112      * @param address the remote host address
113      * @param port the remote host port
114      * @param socketConf the socket configuration
115      * @throws IOException If some other I/O error occurs
116      */

117     public BlockingConnection(InetAddress JavaDoc address, int port, StreamSocketConfiguration socketConf) throws IOException JavaDoc {
118         this(new InetSocketAddress JavaDoc(address, port), socketConf.toOptions(), null, false);
119     }
120
121
122     /**
123      * constructor
124      * @deprecated
125      *
126      * @param address the remote host address
127      * @param port the remote host port
128      * @param options the socket options
129      * @throws IOException If some other I/O error occurs
130      */

131     public BlockingConnection(InetAddress JavaDoc address, int port, Map JavaDoc<String JavaDoc, Object JavaDoc> options) throws IOException JavaDoc {
132         this(new InetSocketAddress JavaDoc(address, port), options, null, false);
133     }
134
135
136
137
138     /**
139      * constructor
140      *
141      * @param address the remote host name
142      * @param port the remote host port
143      * @param sslContext the sslContext to use
144      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
145      * @throws IOException If some other I/O error occurs
146      */

147     public BlockingConnection(InetAddress JavaDoc address, int port, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
148         this(new InetSocketAddress JavaDoc(address, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), sslContext, sslOn);
149     }
150
151
152
153     /**
154      * constructor
155      * @deprecated
156      *
157      * @param address the remote host name
158      * @param port the remote host port
159      * @param socketConf the socket configuration
160      * @param sslContext the sslContext to use
161      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
162      * @throws IOException If some other I/O error occurs
163      */

164     public BlockingConnection(InetAddress JavaDoc address, int port, StreamSocketConfiguration socketConf, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
165         this(new InetSocketAddress JavaDoc(address, port), socketConf.toOptions(), sslContext, sslOn);
166     }
167     
168     /**
169      * constructor
170      *
171      * @param address the remote host name
172      * @param port the remote host port
173      * @param options the socket options
174      * @param sslContext the sslContext to use
175      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
176      * @throws IOException If some other I/O error occurs
177      */

178     public BlockingConnection(InetAddress JavaDoc address, int port, Map JavaDoc<String JavaDoc, Object JavaDoc> options, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
179         this(new InetSocketAddress JavaDoc(address, port), options, sslContext, sslOn);
180     }
181
182     /**
183      * constructor
184      *
185      * @param hostname the remote host name
186      * @param port the remote host port
187      * @param sslContext the sslContext to use
188      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
189 * * @throws IOException If some other I/O error occurs
190      */

191     public BlockingConnection(String JavaDoc hostname, int port, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
192         this(new InetSocketAddress JavaDoc(hostname, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), sslContext, sslOn);
193     }
194
195
196     /**
197      * constructor
198      * @deprecated
199      *
200      * @param hostname the remote host name
201      * @param port the remote host port
202      * @param socketConf the socket configuration
203      * @param sslContext the sslContext to use
204      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
205      * @throws IOException If some other I/O error occurs
206      */

207     public BlockingConnection(String JavaDoc hostname, int port, StreamSocketConfiguration socketConf, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
208         this(new InetSocketAddress JavaDoc(hostname, port), socketConf.toOptions(), sslContext, sslOn);
209     }
210
211
212
213     /**
214      * constructor
215      *
216      * @param hostname the remote host name
217      * @param port the remote host port
218      * @param option the socket options
219      * @param sslContext the sslContext to use
220      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
221      * @throws IOException If some other I/O error occurs
222      */

223     public BlockingConnection(String JavaDoc hostname, int port, Map JavaDoc<String JavaDoc, Object JavaDoc> options, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
224         this(new InetSocketAddress JavaDoc(hostname, port), options, sslContext, sslOn);
225     }
226
227
228
229     
230
231     /**
232      * constructor
233      */

234     private BlockingConnection(InetSocketAddress JavaDoc remoteAddress, Map JavaDoc<String JavaDoc, Object JavaDoc> options, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
235         super(new IoHandlerContext(null, null), remoteAddress, options, sslContext, sslOn);
236
237         setReceiveTimeoutMillis(INITIAL_RECEIVE_TIMEOUT);
238         setFlushmode(FlushMode.SYNC);
239
240
241         init();
242     }
243
244
245
246     @Override JavaDoc
247     void reset() throws IOException JavaDoc {
248         readGuard = new Object JavaDoc();
249
250         super.reset();
251
252         setReceiveTimeoutMillis(IBlockingConnection.INITIAL_RECEIVE_TIMEOUT);
253         setFlushmode(FlushMode.SYNC);
254     }
255
256
257
258     /**
259      * {@inheritDoc}
260      *
261      **/

262     public byte readByte() throws IOException JavaDoc ,ClosedConnectionException, SocketTimeoutException JavaDoc {
263
264         long start = System.currentTimeMillis();
265         long remainingTime = receiveTimeout;
266
267         synchronized (readGuard) {
268             do {
269                 try {
270                     return extractByteFromReadQueue();
271                 } catch (BufferUnderflowException JavaDoc bue) {
272                     // if (!isOpen()) {
273
// throw new ClosedConnectionException("connection " + getId() + " is already closed");
274
// }
275

276                     try {
277                         readGuard.wait(remainingTime);
278                     } catch (InterruptedException JavaDoc ignore) { }
279                 }
280                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
281             } while (remainingTime > 0);
282         }
283
284         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
285     }
286
287
288     /**
289      * {@inheritDoc}
290      */

291     public final void setReceiveTimeoutMillis(long timeout) {
292         this.receiveTimeout = timeout;
293     }
294
295
296     /**
297      * {@inheritDoc}
298      */

299     public ByteBuffer JavaDoc[] readByteBufferByDelimiter(String JavaDoc delimiter) throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc {
300         return readByteBufferByDelimiter(delimiter, Integer.MAX_VALUE);
301     }
302
303
304
305     /**
306      * {@inheritDoc}
307      */

308     public ByteBuffer JavaDoc[] readByteBufferByDelimiter(String JavaDoc delimiter, int maxLength) throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc, MaxReadSizeExceededException {
309         return readByteBufferByDelimiter(delimiter, getDefaultEncoding(), maxLength);
310     }
311
312
313     /**
314      * {@inheritDoc}
315      */

316     public ByteBuffer JavaDoc[] readByteBufferByDelimiter(String JavaDoc delimiter, String JavaDoc encoding, int maxLength) throws IOException JavaDoc, ClosedConnectionException, MaxReadSizeExceededException {
317
318         long start = System.currentTimeMillis();
319         long remainingTime = receiveTimeout;
320
321         synchronized (readGuard) {
322             do {
323                 try {
324                     LinkedList JavaDoc<ByteBuffer JavaDoc> result = extractBytesByDelimiterFromReadQueue(delimiter.getBytes(encoding), maxLength);
325                     return result.toArray(new ByteBuffer JavaDoc[result.size()]);
326                 } catch (MaxReadSizeExceededException mee) {
327                     throw mee;
328
329                 } catch (BufferUnderflowException JavaDoc bue) {
330
331                     try {
332                         readGuard.wait(remainingTime);
333                     } catch (InterruptedException JavaDoc ignore) { }
334                 }
335                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
336             } while (remainingTime > 0);
337         }
338
339         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
340     }
341
342
343     /**
344      * {@inheritDoc}
345      */

346     public ByteBuffer JavaDoc[] readByteBufferByLength(int length) throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc {
347
348         if (length <= 0) {
349             return null;
350         }
351
352         long start = System.currentTimeMillis();
353         long remainingTime = receiveTimeout;
354
355         synchronized (readGuard) {
356             do {
357                 try {
358                     LinkedList JavaDoc<ByteBuffer JavaDoc> result = extractBytesByLength(length);
359                     return result.toArray(new ByteBuffer JavaDoc[result.size()]);
360                 } catch (BufferUnderflowException JavaDoc bue) {
361                     try {
362                         readGuard.wait(remainingTime);
363                     } catch (InterruptedException JavaDoc ignore) { }
364                 }
365                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
366             } while (remainingTime > 0);
367         }
368
369         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
370     }
371
372
373     /**
374      * {@inheritDoc}
375      */

376     public byte[] readBytesByDelimiter(String JavaDoc delimiter) throws IOException JavaDoc ,ClosedConnectionException ,SocketTimeoutException JavaDoc {
377         return readBytesByDelimiter(delimiter, Integer.MAX_VALUE);
378     }
379
380
381
382     /**
383      * {@inheritDoc}
384      */

385     public byte[] readBytesByDelimiter(String JavaDoc delimiter, int maxLength) throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc, MaxReadSizeExceededException {
386         return readBytesByDelimiter(delimiter, getDefaultEncoding(), maxLength);
387     }
388
389
390     /**
391      * {@inheritDoc}
392      */

393     public byte[] readBytesByDelimiter(String JavaDoc delimiter, String JavaDoc encoding, int maxLength) throws IOException JavaDoc, ClosedConnectionException, MaxReadSizeExceededException {
394         return DataConverter.toBytes(readByteBufferByDelimiter(delimiter, encoding, maxLength));
395     }
396
397
398     /**
399      * {@inheritDoc}
400      */

401     public byte[] readBytesByLength(int length) throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc {
402         return DataConverter.toBytes(readByteBufferByLength(length));
403     }
404
405     /**
406      * {@inheritDoc}
407      */

408     public double readDouble() throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc {
409         long start = System.currentTimeMillis();
410         long remainingTime = receiveTimeout;
411
412         synchronized (readGuard) {
413             do {
414                 try {
415                     return extractDoubleFromReadQueue();
416                 } catch (BufferUnderflowException JavaDoc bue) {
417                     try {
418                         readGuard.wait(remainingTime);
419                     } catch (InterruptedException JavaDoc ignore) { }
420                 }
421                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
422             } while (remainingTime> 0);
423         }
424
425         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
426
427     }
428
429     /**
430      * {@inheritDoc}
431      */

432     public int readInt() throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc {
433         long start = System.currentTimeMillis();
434         long remainingTime = receiveTimeout;
435
436         synchronized (readGuard) {
437             do {
438                 try {
439                     return extractIntFromReadQueue();
440                 } catch (BufferUnderflowException JavaDoc bue) {
441                     try {
442                         readGuard.wait(remainingTime);
443                     } catch (InterruptedException JavaDoc ignore) { }
444                 }
445                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
446             } while (remainingTime > 0 );
447         }
448
449         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
450
451     }
452
453     
454     
455
456     /**
457      * {@inheritDoc}
458      */

459     public short readShort() throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc {
460         long start = System.currentTimeMillis();
461         long remainingTime = receiveTimeout;
462
463         synchronized (readGuard) {
464             do {
465                 try {
466                     return extractShortFromReadQueue();
467                 } catch (BufferUnderflowException JavaDoc bue) {
468                     try {
469                         readGuard.wait(remainingTime);
470                     } catch (InterruptedException JavaDoc ignore) { }
471                 }
472                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
473             } while (remainingTime > 0 );
474         }
475
476         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
477
478     }
479
480     /**
481      * {@inheritDoc}.
482      */

483     public final int read(ByteBuffer JavaDoc buffer) throws IOException JavaDoc {
484         int size = buffer.remaining();
485         if (size < 1) {
486             return 0;
487         }
488
489         long start = System.currentTimeMillis();
490         long remainingTime = receiveTimeout;
491
492         synchronized (readGuard) {
493             do {
494                 int availableSize = getReadQueue().getSize();
495
496                 // if at least one byte is available -> read and return
497
if (availableSize > 0) {
498                     if (size > availableSize) {
499                         size = availableSize;
500                     }
501                     ByteBuffer JavaDoc[] bufs = readByteBufferByLength(size);
502
503                     for (ByteBuffer JavaDoc buf : bufs) {
504                         buffer.put(buf);
505                     }
506
507                     return size;
508
509                 // ... or wait for at least one byte
510
}else {
511                     try {
512                         readGuard.wait(remainingTime);
513                     } catch (InterruptedException JavaDoc ignore) { }
514                 }
515                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
516             } while (remainingTime > 0);
517         }
518
519         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
520     }
521
522
523     /**
524      * {@inheritDoc}
525      */

526     public long readLong() throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc {
527         long start = System.currentTimeMillis();
528         long remainingTime = receiveTimeout;
529
530         synchronized (readGuard) {
531             do {
532                 try {
533                     return extractLongFromReadQueue();
534                 } catch (BufferUnderflowException JavaDoc bue) {
535                     try {
536                         readGuard.wait(remainingTime);
537                     } catch (InterruptedException JavaDoc ignore) { }
538                 }
539                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
540             } while (remainingTime > 0);
541         }
542
543         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
544     }
545
546
547
548     /**
549      * {@inheritDoc}
550      */

551     public String JavaDoc readStringByDelimiter(String JavaDoc delimiter) throws IOException JavaDoc, ClosedConnectionException, UnsupportedEncodingException JavaDoc, SocketTimeoutException JavaDoc {
552         return readStringByDelimiter(delimiter, Integer.MAX_VALUE);
553     }
554
555
556     /**
557      * {@inheritDoc}
558      */

559     public String JavaDoc readStringByDelimiter(String JavaDoc delimiter, int maxLength) throws IOException JavaDoc ,ClosedConnectionException ,java.io.UnsupportedEncodingException JavaDoc ,SocketTimeoutException JavaDoc ,MaxReadSizeExceededException {
560         return readStringByDelimiter(delimiter, getDefaultEncoding(), maxLength);
561     };
562
563
564     /**
565      * {@inheritDoc}
566      */

567     public String JavaDoc readStringByDelimiter(String JavaDoc delimiter, String JavaDoc encoding) throws IOException JavaDoc, ClosedConnectionException, UnsupportedEncodingException JavaDoc, SocketTimeoutException JavaDoc {
568         return readStringByDelimiter(delimiter, encoding, Integer.MAX_VALUE);
569     }
570
571     /**
572      * {@inheritDoc}
573      */

574     public String JavaDoc readStringByDelimiter(String JavaDoc delimiter, String JavaDoc encoding, int maxLength) throws IOException JavaDoc, ClosedConnectionException, UnsupportedEncodingException JavaDoc, SocketTimeoutException JavaDoc, MaxReadSizeExceededException {
575         long start = System.currentTimeMillis();
576         long remainingTime = receiveTimeout;
577
578         synchronized (readGuard) {
579             do {
580                 try {
581                     LinkedList JavaDoc<ByteBuffer JavaDoc> extracted = extractBytesByDelimiterFromReadQueue(delimiter.getBytes(encoding), maxLength);
582                     return DataConverter.toString(extracted, encoding);
583                 } catch (MaxReadSizeExceededException mle) {
584                     throw mle;
585
586                 } catch (BufferUnderflowException JavaDoc bue) {
587                     try {
588                         readGuard.wait(remainingTime);
589                     } catch (InterruptedException JavaDoc ignore) { }
590                 }
591                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
592             } while (remainingTime > 0);
593         }
594
595         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
596     }
597
598
599     /**
600      * {@inheritDoc}
601      */

602     public int getIndexOf(String JavaDoc str) throws IOException JavaDoc ,ClosedConnectionException, SocketTimeoutException JavaDoc {
603         return getIndexOf(str, Integer.MAX_VALUE);
604     }
605
606     /**
607      * {@inheritDoc}
608      */

609     public int getIndexOf(String JavaDoc str, int maxLength) throws IOException JavaDoc, ClosedConnectionException, MaxReadSizeExceededException, SocketTimeoutException JavaDoc {
610         return getIndexOf(str, getDefaultEncoding(), maxLength);
611     }
612
613
614     /**
615      * {@inheritDoc}
616      */

617     public int getIndexOf(String JavaDoc str, String JavaDoc encoding, int maxLength) throws IOException JavaDoc, ClosedConnectionException, MaxReadSizeExceededException {
618         long start = System.currentTimeMillis();
619         long remainingTime = receiveTimeout;
620
621         synchronized (readGuard) {
622             do {
623                 try {
624                     return readIndexOf(str.getBytes(encoding), maxLength);
625                 } catch (MaxReadSizeExceededException mle) {
626                     throw mle;
627
628                 } catch (BufferUnderflowException JavaDoc bue) {
629                     try {
630                         readGuard.wait(remainingTime);
631                     } catch (InterruptedException JavaDoc ignore) { }
632                 }
633                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
634             } while (remainingTime > 0);
635         }
636
637         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
638     }
639
640
641     /**
642      * {@inheritDoc}
643      */

644     public String JavaDoc readStringByLength(int length) throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc {
645         return readStringByLength(length, getDefaultEncoding());
646     }
647
648
649     /**
650      * {@inheritDoc}
651      */

652     public String JavaDoc readStringByLength(int length, String JavaDoc encoding) throws IOException JavaDoc, ClosedConnectionException, SocketTimeoutException JavaDoc {
653
654         if (length <= 0) {
655             return null;
656         }
657
658         long start = System.currentTimeMillis();
659         long remainingTime = receiveTimeout;
660
661         synchronized (readGuard) {
662             do {
663                 try {
664                     LinkedList JavaDoc<ByteBuffer JavaDoc> extracted = extractBytesByLength(length);
665                     return DataConverter.toString(extracted, encoding);
666                 } catch (BufferUnderflowException JavaDoc bue) {
667                     try {
668                         readGuard.wait(remainingTime);
669                     } catch (InterruptedException JavaDoc ignore) { }
670                 }
671                 remainingTime = (start + receiveTimeout) - System.currentTimeMillis();
672             } while (remainingTime > 0);
673         }
674
675         throw new SocketTimeoutException JavaDoc("timeout " + DataConverter.toFormatedDuration(receiveTimeout) + " reached");
676     }
677
678     
679     public IBlockingConnection setOption(String JavaDoc name, Object JavaDoc value) throws IOException JavaDoc {
680         return (IBlockingConnection) super.setOption(name, value);
681     }
682
683     @Override JavaDoc
684     protected int onDataEvent() {
685
686         // perform read
687
int addSize = super.onDataEvent();
688
689         if (addSize > 0) {
690             synchronized (readGuard) {
691                 readGuard.notify();
692             }
693         }
694
695         return addSize;
696     }
697 }
698
Popular Tags