001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.tomcat.util.net;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.net.Socket;
023:
024: /**
025: *
026: */
027: public class TcpConnection { // implements Endpoint {
028: /**
029: * Maxium number of times to clear the socket input buffer.
030: */
031: static int MAX_SHUTDOWN_TRIES = 20;
032:
033: public TcpConnection() {
034: }
035:
036: // -------------------- Properties --------------------
037:
038: PoolTcpEndpoint endpoint;
039: Socket socket;
040:
041: public static void setMaxShutdownTries(int mst) {
042: MAX_SHUTDOWN_TRIES = mst;
043: }
044:
045: public void setEndpoint(PoolTcpEndpoint endpoint) {
046: this .endpoint = endpoint;
047: }
048:
049: public PoolTcpEndpoint getEndpoint() {
050: return endpoint;
051: }
052:
053: public void setSocket(Socket socket) {
054: this .socket = socket;
055: }
056:
057: public Socket getSocket() {
058: return socket;
059: }
060:
061: public void recycle() {
062: endpoint = null;
063: socket = null;
064: }
065:
066: // Another frequent repetition
067: public static int readLine(InputStream in, byte[] b, int off,
068: int len) throws IOException {
069: if (len <= 0) {
070: return 0;
071: }
072: int count = 0, c;
073:
074: while ((c = in.read()) != -1) {
075: b[off++] = (byte) c;
076: count++;
077: if (c == '\n' || count == len) {
078: break;
079: }
080: }
081: return count > 0 ? count : -1;
082: }
083:
084: // Usefull stuff - avoid having it replicated everywhere
085: public static void shutdownInput(Socket socket) throws IOException {
086: try {
087: InputStream is = socket.getInputStream();
088: int available = is.available();
089: int count = 0;
090:
091: // XXX on JDK 1.3 just socket.shutdownInput () which
092: // was added just to deal with such issues.
093:
094: // skip any unread (bogus) bytes
095: while (available > 0 && count++ < MAX_SHUTDOWN_TRIES) {
096: is.skip(available);
097: available = is.available();
098: }
099: } catch (NullPointerException npe) {
100: // do nothing - we are just cleaning up, this is
101: // a workaround for Netscape \n\r in POST - it is supposed
102: // to be ignored
103: }
104: }
105: }
|