1 17 18 package org.apache.tomcat.util.net; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.Socket ; 23 24 27 public class TcpConnection { 31 static int MAX_SHUTDOWN_TRIES=20; 32 33 public TcpConnection() { 34 } 35 36 38 PoolTcpEndpoint endpoint; 39 Socket socket; 40 41 public static void setMaxShutdownTries(int mst) { 42 MAX_SHUTDOWN_TRIES = mst; 43 } 44 public void setEndpoint(PoolTcpEndpoint endpoint) { 45 this.endpoint = endpoint; 46 } 47 48 public PoolTcpEndpoint getEndpoint() { 49 return endpoint; 50 } 51 52 public void setSocket(Socket socket) { 53 this.socket=socket; 54 } 55 56 public Socket getSocket() { 57 return socket; 58 } 59 60 public void recycle() { 61 endpoint = null; 62 socket = null; 63 } 64 65 public static int readLine(InputStream in, byte[] b, int off, int len) 67 throws IOException 68 { 69 if (len <= 0) { 70 return 0; 71 } 72 int count = 0, c; 73 74 while ((c = in.read()) != -1) { 75 b[off++] = (byte)c; 76 count++; 77 if (c == '\n' || count == len) { 78 break; 79 } 80 } 81 return count > 0 ? count : -1; 82 } 83 84 85 public static void shutdownInput(Socket socket) 87 throws IOException 88 { 89 try { 90 InputStream is = socket.getInputStream(); 91 int available = is.available (); 92 int count=0; 93 94 97 while (available > 0 && count++ < MAX_SHUTDOWN_TRIES) { 99 is.skip (available); 100 available = is.available(); 101 } 102 }catch(NullPointerException npe) { 103 } 107 } 108 } 109 110 111 | Popular Tags |