1 22 package org.xsocket.stream; 23 24 import java.io.IOException ; 25 import java.io.UnsupportedEncodingException ; 26 import java.net.InetAddress ; 27 import java.net.InetSocketAddress ; 28 import java.net.SocketTimeoutException ; 29 import java.nio.ByteBuffer ; 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.util.Timer ; 37 import java.util.TimerTask ; 38 import java.util.concurrent.Executor ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 import org.xsocket.ClosedConnectionException; 43 import org.xsocket.DataConverter; 44 import org.xsocket.ILifeCycle; 45 import org.xsocket.MaxReadSizeExceededException; 46 47 48 53 abstract class AbstractConnectionPool implements IConnectionPool { 54 55 private static final Logger LOG = Logger.getLogger(AbstractConnectionPool.class.getName()); 56 57 static final long MIN_CHECKPERIOD_MILLIS = 60L * 1000L; 58 static final long NULL = -1; 59 static final int MAX_SIZE = Integer.MAX_VALUE; 60 static final long MAX_TIMEOUT = Long.MAX_VALUE; 61 62 static final int CREATE_CONNECTION_TIMEOUT = 250; 63 64 private final Timer timer = new Timer ("ConPoolWatchdog", true); 65 66 private int maxActive = Integer.MAX_VALUE; 67 private int maxIdle = 3; 68 private long maxWaitMillis = 0; 69 private long idleTimeoutMillis = MAX_TIMEOUT; 70 private long lifeTimeoutMillis = MAX_TIMEOUT; 71 72 private boolean isOpen = true; 73 private final Map <InetSocketAddress , List <PoolableConnection>> idlePool = new HashMap <InetSocketAddress , List <PoolableConnection>>(); 74 private final Set <PoolableConnection> activePool = new HashSet <PoolableConnection>(); 75 76 private long checkPeriod = 0; 77 private TimerTask watchDogTask = null; 78 79 private final List <ILifeCycle> listeners = new ArrayList <ILifeCycle>(); 81 82 83 91 AbstractConnectionPool(long idleTimeoutMillis, long lifeTimeoutMillis, int maxActive, long maxWaitMillis, int maxIdle) { 92 this.idleTimeoutMillis = idleTimeoutMillis; 93 this.lifeTimeoutMillis = lifeTimeoutMillis; 94 this.maxActive = maxActive; 95 this.maxWaitMillis = maxWaitMillis; 96 this.maxIdle = maxIdle; 97 98 resetCheckPeriod(); 99 } 100 101 102 107 public synchronized final void destroyConnection(IConnection connection) throws IOException { 108 if (connection == null) { 109 return; 110 } 111 112 if (connection instanceof PoolableConnection) { 113 if (LOG.isLoggable(Level.FINE)) { 114 LOG.fine("destroying connection " + connection.getId()); 115 } 116 activePool.remove(connection); 117 ((PoolableConnection) connection).reallyClose(); 118 } else { 119 connection.close(); 120 } 121 } 122 123 124 128 public void addListener(ILifeCycle listener) { 129 listeners.add(listener); 130 } 131 132 137 public boolean removeListener(ILifeCycle listener) { 138 boolean result = listeners.remove(listener); 139 140 return result; 141 } 142 143 144 private void resetCheckPeriod() { 145 146 if (watchDogTask != null) { 147 watchDogTask.cancel(); 148 } 149 150 watchDogTask = new TimerTask () { 151 @Override 152 public void run() { 153 Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 154 List <PoolableConnection> idleConnections = idleConnectionList(); 155 checkTimeout(idleConnections); 156 checkSize(idleConnections); 157 } 158 }; 159 160 161 long time = MIN_CHECKPERIOD_MILLIS; 162 if ((lifeTimeoutMillis / 5) < time) { 163 time = (lifeTimeoutMillis / 5); 164 } 165 if ((idleTimeoutMillis / 5) < time) { 166 time = (idleTimeoutMillis / 5); 167 } 168 169 checkPeriod = time; 170 timer.schedule(watchDogTask, checkPeriod, checkPeriod); 171 } 172 173 174 final long getCheckPeriodMillis() { 175 return checkPeriod; 176 } 177 178 181 public final synchronized int getMaxActive() { 182 return maxActive; 183 } 184 185 188 public final synchronized void setMaxActive(int maxActive) { 189 this.maxActive = maxActive; 190 notifyAll(); 191 } 192 193 196 public final synchronized long getMaxWaitMillis() { 197 return maxWaitMillis; 198 } 199 200 201 204 public final synchronized void setMaxWaitMillis(long maxWaitMillis) { 205 this.maxWaitMillis = maxWaitMillis; 206 notifyAll(); 207 } 208 209 210 213 public final synchronized int getMaxIdle() { 214 return maxIdle; 215 } 216 217 218 221 public final synchronized void setMaxIdle(int maxIdle) { 222 this.maxIdle = maxIdle; 223 notifyAll(); 224 } 225 226 227 230 public final synchronized int getNumActive() { 231 return activePool.size(); 232 } 233 234 235 238 public final synchronized int getNumIdle() { 239 int size = 0; 240 for (List <PoolableConnection> connectionList : idlePool.values()) { 241 size += connectionList.size(); 242 } 243 244 return size; 245 } 246 247 248 251 public final long getIdleTimeoutMillis() { 252 return idleTimeoutMillis; 253 } 254 255 256 259 public final void setIdleTimeoutMillis(long idleTimeoutMillis) { 260 this.idleTimeoutMillis = idleTimeoutMillis; 261 resetCheckPeriod(); 262 } 263 264 265 268 public final long getLifeTimeoutMillis() { 269 return lifeTimeoutMillis; 270 } 271 272 273 276 public final void setLifeTimeoutMillis(long lifeTimeoutMillis) { 277 this.lifeTimeoutMillis = lifeTimeoutMillis; 278 resetCheckPeriod(); 279 } 280 281 282 synchronized List <String > getConnectionInfo() { 283 List <String > info = new ArrayList <String >(); 284 285 for (PoolableConnection activeConnection : activePool) { 286 info.add(activeConnection.toString()); 287 } 288 289 for (PoolableConnection idleConnection : idleConnectionList()) { 290 info.add(idleConnection.toString()); 291 } 292 293 return info; 294 } 295 296 308 synchronized final PoolableConnection getConnection(InetSocketAddress address, Executor workerPool) throws IOException , WaitTimeoutException { 309 310 if (isOpen) { 312 PoolableConnection poolableConnection = getConnectionFromPool(address); 313 314 if (poolableConnection == null) { 315 316 if (maxWaitMillis == NULL) { 317 poolableConnection = newConnection(address, workerPool); 318 if (LOG.isLoggable(Level.FINE)) { 319 LOG.fine("new connection to " + poolableConnection.getId() + " has been established (idling=" + getNumIdle() + ", active=" + getNumActive() + ", maxActive=" + getMaxActive() + ", idleTimeoutMillis=" + getIdleTimeoutMillis() + ", lifeTimeout=" + getLifeTimeoutMillis() + ")"); 320 } 321 } else { 322 if (activePool.size() < maxActive) { 323 poolableConnection = newConnection(address, workerPool); 324 if (LOG.isLoggable(Level.FINE)) { 325 LOG.fine("new connection to " + poolableConnection.getId() + " has been established (idling=" + getNumIdle() + ", active=" + getNumActive() + ", maxActive=" + getMaxActive() + ", idleTimeoutMillis=" + getIdleTimeoutMillis() + ", lifeTimeout=" + getLifeTimeoutMillis() + ")"); 326 } 327 } else { 328 if (LOG.isLoggable(Level.FINE)) { 329 LOG.fine("no free connection available waiting for a free connection (idling=" + getNumIdle() + ", active=" + getNumActive() + ", maxActive=" + getMaxActive() + ", idleTimeoutMillis=" + getIdleTimeoutMillis() + ", lifeTimeout=" + getLifeTimeoutMillis() + ")"); 330 } 331 332 long start = System.currentTimeMillis(); 333 while ((System.currentTimeMillis() - start) < maxWaitMillis) { 334 try { 335 wait(maxWaitMillis - (System.currentTimeMillis() - start)); 336 } catch (InterruptedException ignore) { } 337 338 poolableConnection = getConnectionFromPool(address); 339 if (poolableConnection != null) { 340 break; 341 } 342 } 343 if (poolableConnection == null) { 344 if (LOG.isLoggable(Level.FINE)) { 345 LOG.fine("wait timeout reached (" + DataConverter.toFormatedDuration(maxWaitMillis) + ")"); 346 } 347 throw new WaitTimeoutException("wait timeout reached (" + DataConverter.toFormatedDuration(maxWaitMillis) + ")"); 348 } 349 } 350 } 351 } 352 353 if (poolableConnection != null) { 354 activePool.add(poolableConnection); 355 poolableConnection.setStateActive(); 356 } 357 return poolableConnection; 358 359 360 } else { 362 throw new RuntimeException ("pool is already closed"); 363 } 364 } 365 366 367 private PoolableConnection getConnectionFromPool(InetSocketAddress address) throws IOException { 368 369 try { 370 List <PoolableConnection> connectionList = idlePool.get(address); 371 372 if (connectionList != null) { 374 375 if (!connectionList.isEmpty()) { 377 PoolableConnection poolableConnection = connectionList.remove(0); 378 379 if (isConnectionValid(System.currentTimeMillis(), poolableConnection)) { 381 poolableConnection.reset(); 382 383 if (connectionList.isEmpty()) { 385 idlePool.remove(address); 386 } 387 388 if (LOG.isLoggable(Level.FINE)) { 389 LOG.fine("got connection to " + poolableConnection.getId() + " from pool (idling=" + getNumIdle() + ", active=" + getNumActive() + ", maxActive=" + getMaxActive() + ", idleTimeoutMillis=" + getIdleTimeoutMillis() + ", lifeTimeout=" + getLifeTimeoutMillis() + ")"); 390 } 391 392 return poolableConnection; 393 394 } else { 396 return getConnectionFromPool(address); 397 } 398 } 399 } 400 } catch (Exception e) { 401 return null; 402 } 403 404 return null; 405 } 406 407 413 final String getAddressString(String host, int port) { 414 return host + ":" + port; 415 } 416 417 418 424 final synchronized void returnConnection(PoolableConnection poolableConnection) throws IOException { 425 426 activePool.remove(poolableConnection); 427 poolableConnection.setStateIdle(); 428 429 if (isOpen) { 431 432 if (!isConnectionValid(System.currentTimeMillis(), poolableConnection)) { 434 if (LOG.isLoggable(Level.FINE)) { 435 LOG.fine("do not return given connection to pool, because it is not valid/reuseable"); 436 } 437 poolableConnection.reallyClose(); 438 return; 439 440 } else { 441 try { 442 poolableConnection.reset(); 443 } catch (Exception e) { 444 poolableConnection.reallyClose(); 445 return; 446 } 447 448 addConnectionToPool(poolableConnection); 449 } 450 451 } else { 453 if (LOG.isLoggable(Level.FINE)) { 454 LOG.fine("pool is already closed destroy returned connection to " + poolableConnection.getId()); 455 } 456 poolableConnection.reallyClose(); 457 return; 458 } 459 460 notifyAll(); 461 } 462 463 464 private void addConnectionToPool(PoolableConnection connection) throws IOException { 465 if (idlePool.size() < maxIdle) { 466 List <PoolableConnection> connectionList = idlePool.get(connection.getAddress()); 467 if (connectionList == null) { 468 connectionList = new ArrayList <PoolableConnection>(); 469 idlePool.put(connection.getAddress(), connectionList); 470 } 471 472 connectionList.add(connection); 473 474 if (LOG.isLoggable(Level.FINER)) { 475 LOG.finer("connection to " + connection.getId() + " has been inserted into the pool (idling=" + getNumIdle() + ", active=" + getNumActive() + ", maxActive=" + getMaxActive() + ", idleTimeoutMillis=" + getIdleTimeoutMillis() + ", lifeTimeout=" + getLifeTimeoutMillis() + ")"); 476 } 477 } else { 478 connection.reallyClose(); 479 } 480 } 481 482 483 488 public final synchronized void close() { 489 if (LOG.isLoggable(Level.FINE)) { 490 LOG.fine("closing (idling=" + getNumIdle() + ", active=" + getNumActive() + ")"); 491 } 492 493 timer.cancel(); 494 495 for (List <PoolableConnection> connectionList : idlePool.values()) { 496 for (PoolableConnection connection : connectionList) { 497 try { 498 connection.reallyClose(); 499 } catch (Exception e) { 500 if (LOG.isLoggable(Level.FINE)) { 501 LOG.fine("error occured by (really) closing of conection " + connection.getId() + ". Reason: " + e.toString()); 502 } 503 } 504 } 505 } 506 507 idlePool.clear(); 508 509 for (ILifeCycle lifeCycle : listeners) { 510 lifeCycle.onDestroy(); 511 } 512 } 513 514 515 518 @Override 519 public String toString() { 520 return this.getClass().getSimpleName() + " idling=" + getNumIdle() + ", active=" + getNumActive() + ", maxActive=" + getMaxActive() + ", idleTimeoutMillis=" + getIdleTimeoutMillis() + ", lifeTimeout=" + getLifeTimeoutMillis() + ")"; 521 } 522 523 524 525 private boolean isConnectionValid(long currentTime, PoolableConnection poolableConnection) { 526 527 if (!poolableConnection.isOpen()) { 528 return false; 529 } 530 531 if (!poolableConnection.isReuseable()) { 532 return false; 533 } 534 535 if (idleTimeoutMillis != MAX_TIMEOUT) { 536 if (currentTime > (poolableConnection.getLastUsageTime() + idleTimeoutMillis)) { 537 return false; 538 } 539 } 540 541 if (lifeTimeoutMillis != MAX_TIMEOUT) { 542 if (currentTime > (poolableConnection.getCreationTime() + lifeTimeoutMillis)) { 543 return false; 544 } 545 } 546 547 return true; 548 } 549 550 551 552 private void checkTimeout(List <PoolableConnection> idleConnections) { 553 long currentTime = System.currentTimeMillis(); 554 555 for (PoolableConnection poolableConnection : idleConnections) { 556 557 if (!isConnectionValid(currentTime, poolableConnection)) { 558 closeConnection(poolableConnection, "auto"); 559 } 560 } 561 } 562 563 564 private void checkSize(List <PoolableConnection> idleConnections) { 565 if (idleConnections.size() > maxIdle) { 566 for (int i =0; i < (idleConnections.size() - maxIdle); i++ ) { 567 PoolableConnection poolableConnection = idleConnections.get(i); 568 closeConnection(poolableConnection, "auto"); 569 } 570 } 571 } 572 573 574 private synchronized List <PoolableConnection> idleConnectionList() { 575 List <PoolableConnection> idleConnections = new ArrayList <PoolableConnection>(); 576 577 for (List <PoolableConnection> connectionList : idlePool.values()) { 578 idleConnections.addAll(connectionList); 579 } 580 581 return idleConnections; 582 } 583 584 585 private synchronized boolean closeConnection(PoolableConnection poolableConnection, String reason) { 586 assert (poolableConnection != null); 587 588 List <PoolableConnection> connectionList = idlePool.get(poolableConnection.getAddress()); 589 if (connectionList.contains(poolableConnection)) { 590 connectionList.remove(poolableConnection); 591 try { 592 poolableConnection.reallyClose(); 593 } catch (Exception e) { 594 if (LOG.isLoggable(Level.FINE)) { 595 LOG.fine("error occured by (really) closing of connection " + poolableConnection.getId() + ". Reason: " + e.toString()); 596 } 597 } 598 599 600 if (LOG.isLoggable(Level.FINE)) { 601 LOG.fine(reason + " close of connection " + poolableConnection.getId() + " (idling=" + getNumIdle() + ", active=" + getNumActive() + ", maxActive=" + getMaxActive() + ", idleTimeoutMillis=" + getIdleTimeoutMillis() + ", lifeTimeout=" + getLifeTimeoutMillis() + ")"); 602 } 603 604 return true; 605 } else { 606 return false; 607 } 608 } 609 610 611 private PoolableConnection newConnection(InetSocketAddress address, Executor workerPool) throws IOException { 612 int trials = 0; 613 int sleepTime = 3; 614 615 IOException ex = null; 616 617 long start = System.currentTimeMillis(); 618 PoolableConnection connection = null; 619 do { 620 trials++; 621 try { 622 623 connection = createConnection(address, workerPool); 624 return connection; 625 } catch (IOException ioe) { 626 ex = ioe; 627 sleepTime = sleepTime * 3; 628 } 629 630 try { 631 Thread.sleep(sleepTime); 632 } catch (InterruptedException ignore) { } 633 } while (System.currentTimeMillis() < (start + CREATE_CONNECTION_TIMEOUT)); 634 635 if (LOG.isLoggable(Level.FINE)) { 636 LOG.fine("error occured by creating connection to " + address 637 + ". creation timeout " + CREATE_CONNECTION_TIMEOUT + " reached. (" + trials + " trials done)"); 638 } 639 640 if (ex != null) { 641 throw ex; 642 } else { 643 throw new IOException ("couldn't create a new connetion to " + address); 644 } 645 } 646 647 648 657 abstract PoolableConnection createConnection(InetSocketAddress address, Executor workerPool) throws IOException ; 658 659 660 665 abstract class PoolableConnection implements IConnection { 666 667 668 private AbstractConnectionPool pool = null; 669 private InetSocketAddress address = null; 670 private Connection delegee; 671 672 private long creationTime = System.currentTimeMillis(); 673 private long lastUsageTime = System.currentTimeMillis(); 674 private boolean isActive = false; 675 private long enteredState = System.currentTimeMillis(); 676 677 private boolean isDeleted = false; 678 private boolean isReuseable = true; 679 680 public PoolableConnection(AbstractConnectionPool pool, Connection delegee, InetSocketAddress address) throws IOException { 681 this.pool = pool; 682 this.delegee = delegee; 683 this.address = address; 684 } 685 686 final boolean isReuseable() { 687 return isReuseable; 688 } 689 690 final void setReuseable(boolean isReusable) { 691 this.isReuseable = isReusable; 692 } 693 694 final void setStateActive() { 695 isActive = true; 696 enteredState = System.currentTimeMillis(); 697 } 698 699 final void setStateIdle() { 700 isActive = false; 701 enteredState = System.currentTimeMillis(); 702 } 703 704 final boolean isActive() { 705 return isActive; 706 } 707 708 final long getStateEntered() { 709 return enteredState; 710 } 711 712 final Connection getDelegee() { 713 return delegee; 714 } 715 716 final InetSocketAddress getAddress() { 717 return address; 718 } 719 720 final long getLastUsageTime() { 721 return lastUsageTime; 722 } 723 724 final long getCreationTime() { 725 return creationTime; 726 } 727 728 729 public void close() throws IOException { 730 if (delegee.isOpen()) { 731 lastUsageTime = System.currentTimeMillis(); 732 pool.returnConnection(this); 733 } else { 734 if (LOG.isLoggable(Level.FINE)) { 735 LOG.fine("connection " + delegee.getId() + " will not return to pool because it is closed"); 736 } 737 } 738 } 739 740 void reset() throws IOException { 741 delegee.reset(); 742 } 743 744 public void setIdleTimeoutSec(int timeoutInSec) { 745 getDelegee().setIdleTimeoutSec(timeoutInSec); 746 } 747 748 public int getIdleTimeoutSec() { 749 return getDelegee().getIdleTimeoutSec(); 750 } 751 752 public Object getOption(String name) throws IOException { 753 return getDelegee().getOption(name); 754 } 755 756 public Map <String , Class > getOptions() { 757 return getDelegee().getOptions(); 758 } 759 760 761 public void setConnectionTimeoutSec(int timeoutSec) { 762 getDelegee().setConnectionTimeoutSec(timeoutSec); 763 } 764 765 766 public int getConnectionTimeoutSec() { 767 return getDelegee().getConnectionTimeoutSec(); 768 } 769 770 public void resumeRead() throws IOException { 771 getDelegee().resumeRead(); 772 } 773 774 public void suspendRead() throws IOException { 775 getDelegee().suspendRead(); 776 } 777 778 void reallyClose() throws IOException { 779 isDeleted = true; 780 delegee.close(); 781 } 782 783 public void flush() throws ClosedConnectionException, IOException , SocketTimeoutException { 784 delegee.flush(); 785 } 786 787 public boolean isOpen() { 788 return !isDeleted && delegee.isOpen(); 789 } 790 791 public void activateSecuredMode() throws IOException { 792 throw new UnsupportedOperationException ("activateSecuredMode is not supported for a pooled connection"); 793 } 794 795 public final int getPendingWriteDataSize() { 796 return delegee.getPendingWriteDataSize(); 797 } 798 799 800 public boolean getAutoflush() { 801 return delegee.getAutoflush(); 802 } 803 804 public void setAutoflush(boolean autoflush) { 805 delegee.setAutoflush(autoflush); 806 } 807 808 public void setDefaultEncoding(String encoding) { 809 delegee.setDefaultEncoding(encoding); 810 } 811 812 813 public String getDefaultEncoding() { 814 return delegee.getDefaultEncoding(); 815 } 816 817 public int getIndexOf(String str) throws IOException , ClosedConnectionException { 818 return delegee.getIndexOf(str); 819 } 820 821 public int getIndexOf(String str, int maxLength) throws IOException , ClosedConnectionException, MaxReadSizeExceededException { 822 return delegee.getIndexOf(str, maxLength); 823 } 824 825 public int getIndexOf(String str, String encoding, int maxLength) throws IOException , ClosedConnectionException, MaxReadSizeExceededException { 826 return delegee.getIndexOf(str, encoding, maxLength); 827 } 828 829 public int read(ByteBuffer buffer) throws IOException { 830 return delegee.read(buffer); 831 } 832 833 public byte readByte() throws IOException , ClosedConnectionException, SocketTimeoutException { 834 return delegee.readByte(); 835 } 836 837 public ByteBuffer [] readByteBufferByDelimiter(String delimiter) throws IOException , ClosedConnectionException, SocketTimeoutException { 838 return delegee.readByteBufferByDelimiter(delimiter); 839 } 840 841 public ByteBuffer [] readByteBufferByDelimiter(String delimiter, int maxLength) throws IOException , ClosedConnectionException, SocketTimeoutException , MaxReadSizeExceededException { 842 return delegee.readByteBufferByDelimiter(delimiter, maxLength); 843 } 844 845 public ByteBuffer [] readByteBufferByDelimiter(String delimiter, String encoding, int maxLength) throws IOException , ClosedConnectionException, MaxReadSizeExceededException { 846 return delegee.readByteBufferByDelimiter(delimiter, encoding, maxLength); 847 } 848 849 public ByteBuffer [] readByteBufferByLength(int length) throws IOException , ClosedConnectionException, SocketTimeoutException { 850 return delegee.readByteBufferByLength(length); 851 } 852 853 public byte[] readBytesByDelimiter(String delimiter) throws IOException , ClosedConnectionException, SocketTimeoutException { 854 return delegee.readBytesByDelimiter(delimiter); 855 } 856 857 public byte[] readBytesByDelimiter(String delimiter, int maxLength) throws IOException , ClosedConnectionException, SocketTimeoutException , MaxReadSizeExceededException { 858 return delegee.readBytesByDelimiter(delimiter, maxLength); 859 } 860 861 public byte[] readBytesByDelimiter(String delimiter, String encoding, int maxLength) throws IOException , ClosedConnectionException, MaxReadSizeExceededException { 862 return delegee.readBytesByDelimiter(delimiter, encoding, maxLength); 863 } 864 865 public byte[] readBytesByLength(int length) throws IOException , ClosedConnectionException, SocketTimeoutException { 866 return delegee.readBytesByLength(length); 867 } 868 869 public double readDouble() throws IOException , ClosedConnectionException, SocketTimeoutException { 870 return delegee.readDouble(); 871 } 872 873 public int readInt() throws IOException , ClosedConnectionException, SocketTimeoutException { 874 return delegee.readInt(); 875 } 876 877 public short readShort() throws IOException , ClosedConnectionException, SocketTimeoutException { 878 return delegee.readShort(); 879 } 880 881 public long readLong() throws IOException , ClosedConnectionException, SocketTimeoutException { 882 return delegee.readLong(); 883 } 884 885 public String readStringByDelimiter(String delimiter) throws IOException , ClosedConnectionException, UnsupportedEncodingException , SocketTimeoutException { 886 return delegee.readStringByDelimiter(delimiter); 887 } 888 889 public String readStringByDelimiter(String delimiter, int maxLength) throws IOException , ClosedConnectionException, UnsupportedEncodingException , SocketTimeoutException , MaxReadSizeExceededException { 890 return delegee.readStringByDelimiter(delimiter, maxLength); 891 } 892 893 public String readStringByDelimiter(String delimiter, String encoding) throws IOException , ClosedConnectionException, UnsupportedEncodingException , SocketTimeoutException { 894 return delegee.readStringByDelimiter(delimiter, encoding); 895 } 896 897 public String readStringByDelimiter(String delimiter, String encoding, int maxLength) throws IOException , ClosedConnectionException, UnsupportedEncodingException , SocketTimeoutException , MaxReadSizeExceededException { 898 return delegee.readStringByDelimiter(delimiter, encoding, maxLength); 899 } 900 901 public int indexOf(String str) throws IOException , ClosedConnectionException { 902 return delegee.indexOf(str); 903 } 904 905 public String readStringByLength(int length) throws IOException , ClosedConnectionException, SocketTimeoutException { 906 return delegee.readStringByLength(length); 907 } 908 909 public String readStringByLength(int length, String encoding) throws IOException , ClosedConnectionException, SocketTimeoutException { 910 return delegee.readStringByLength(length, encoding); 911 } 912 913 public void removeReadMark() { 914 delegee.removeReadMark(); 915 } 916 917 public void removeWriteMark() { 918 delegee.removeWriteMark(); 919 } 920 921 public void markReadPosition() { 922 delegee.markReadPosition(); 923 } 924 925 public void markWritePosition() { 926 delegee.markWritePosition(); 927 } 928 929 public boolean resetToReadMark() { 930 return delegee.resetToReadMark(); 931 } 932 933 public boolean resetToWriteMark() { 934 return delegee.resetToWriteMark(); 935 } 936 937 public String getId() { 938 return delegee.getId(); 939 } 940 941 public InetAddress getLocalAddress() { 942 return delegee.getLocalAddress(); 943 } 944 945 public int getLocalPort() { 946 return delegee.getLocalPort(); 947 } 948 949 public InetAddress getRemoteAddress() { 950 return delegee.getRemoteAddress(); 951 } 952 953 public int getRemotePort() { 954 return delegee.getRemotePort(); 955 } 956 957 public int write(byte b) throws ClosedConnectionException, IOException , SocketTimeoutException { 958 return delegee.write(b); 959 } 960 961 public int write(byte... bytes) throws ClosedConnectionException, IOException , SocketTimeoutException { 962 return delegee.write(bytes); 963 } 964 965 public int write(short s) throws ClosedConnectionException, IOException , SocketTimeoutException { 966 return delegee.write(s); 967 } 968 969 public int write(byte[] bytes, int offset, int length) throws ClosedConnectionException, IOException , SocketTimeoutException { 970 return delegee.write(bytes, offset, length); 971 } 972 973 public int write(ByteBuffer buffer) throws ClosedConnectionException, IOException , SocketTimeoutException { 974 return delegee.write(buffer); 975 } 976 977 public long write(ByteBuffer [] arg0, int arg1, int arg2) throws IOException { 978 return delegee.write(arg0, arg1, arg2); 979 } 980 981 public long write(ByteBuffer [] buffers) throws ClosedConnectionException, IOException , SocketTimeoutException { 982 return delegee.write(buffers); 983 } 984 985 public int write(double d) throws ClosedConnectionException, IOException , SocketTimeoutException { 986 return delegee.write(d); 987 } 988 989 public int write(int i) throws ClosedConnectionException, IOException , SocketTimeoutException { 990 return delegee.write(i); 991 } 992 993 public int write(long l) throws ClosedConnectionException, IOException , SocketTimeoutException { 994 return delegee.write(l); 995 } 996 997 public int write(String message) throws ClosedConnectionException, IOException , SocketTimeoutException { 998 return delegee.write(message); 999 } 1000 1001 public int write(String message, String encoding) throws ClosedConnectionException, IOException , SocketTimeoutException { 1002 return delegee.write(message, encoding); 1003 } 1004 1005 public Object attach(Object obj) { 1006 return delegee.attach(obj); 1007 } 1008 1009 public Object attachment() { 1010 return delegee.attachment(); 1011 } 1012 1013 1014 @Override 1015 public String toString() { 1016 String state = "idle"; 1017 if (isActive()) { 1018 state = "active"; 1019 } 1020 return "[" + state + ", since " + DataConverter.toFormatedDuration(System.currentTimeMillis() - getStateEntered()) + "] " + delegee.toString(); 1021 } 1022 } 1023} 1024 | Popular Tags |