KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > net > TcpConnection


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.tomcat.util.net;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.Socket JavaDoc;
23
24 /**
25  *
26  */

27 public class TcpConnection { // implements Endpoint {
28
/**
29      * Maxium number of times to clear the socket input buffer.
30      */

31     static int MAX_SHUTDOWN_TRIES=20;
32
33     public TcpConnection() {
34     }
35
36     // -------------------- Properties --------------------
37

38     PoolTcpEndpoint endpoint;
39     Socket JavaDoc 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 JavaDoc socket) {
53     this.socket=socket;
54     }
55
56     public Socket JavaDoc getSocket() {
57     return socket;
58     }
59
60     public void recycle() {
61         endpoint = null;
62         socket = null;
63     }
64
65     // Another frequent repetition
66
public static int readLine(InputStream JavaDoc in, byte[] b, int off, int len)
67     throws IOException JavaDoc
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     // Usefull stuff - avoid having it replicated everywhere
86
public static void shutdownInput(Socket JavaDoc socket)
87     throws IOException JavaDoc
88     {
89     try {
90         InputStream JavaDoc is = socket.getInputStream();
91         int available = is.available ();
92         int count=0;
93         
94         // XXX on JDK 1.3 just socket.shutdownInput () which
95
// was added just to deal with such issues.
96

97         // skip any unread (bogus) bytes
98
while (available > 0 && count++ < MAX_SHUTDOWN_TRIES) {
99         is.skip (available);
100         available = is.available();
101         }
102     }catch(NullPointerException JavaDoc npe) {
103         // do nothing - we are just cleaning up, this is
104
// a workaround for Netscape \n\r in POST - it is supposed
105
// to be ignored
106
}
107     }
108 }
109
110
111
Popular Tags