KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > http > connection > HttpConnection


1 package net.matuschek.http.connection;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.InterruptedIOException JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.net.InetAddress JavaDoc;
8 import java.net.Socket JavaDoc;
9 import net.matuschek.util.TimedSocket;
10 /*********************************************
11     Copyright (c) 2001 by Daniel Matuschek
12 *********************************************/

13
14
15
16
17 /**
18  * An HttpConnection object simply represents a TCP socket connection
19  * between the client and an HTTP server.
20  *
21  * @author Daniel Matuschek
22  * @version $Id: HttpConnection.java,v 1.4 2002/05/31 14:45:55 matuschd Exp $
23  */

24 public class HttpConnection {
25
26   /** the TCP socket to use */
27   private Socket JavaDoc socket = null;
28   
29
30   /**
31    * Create a new HttpConnection to the given host and port.
32    *
33    * @param address the IP address to connect to
34    * @param port the port to connect to (usually 80 for HTTP)
35    * @param timeout connection timeout in milliseconds. If the connection
36    * could not be established after this time, an exception will
37    * be thrown. This timeout will also be set as timeout for the
38    * TCP connection (so timeout)
39    * An timeout of 0 will be interpreted as an infinite timeout.
40    * @return a new HttpConnection object
41    * @exception IOException if the TCP socket connection could
42    * not be established
43    */

44   public static HttpConnection createConnection(InetAddress JavaDoc address,
45                         int port,
46                         int timeout)
47     throws IOException JavaDoc
48   {
49     HttpConnection connection = new HttpConnection();
50     try {
51       connection.socket = TimedSocket.getSocket(address, port, timeout);
52       connection.socket.setSoTimeout(timeout);
53     } catch (InterruptedIOException JavaDoc e) {
54       throw new IOException JavaDoc("timeout during connect: "+e.getMessage());
55     }
56     return connection;
57   }
58
59   /**
60    * Gets the InputStream of this connection. Don't close this stream,
61    * but close the HttpConnection when finished.
62    *
63    * @return an InputStream or null if no connection is established
64    * @exception IOException if an I/O error occurs when creating the stream
65    */

66   public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
67     if (socket == null) throw new IOException JavaDoc("not conected");
68     return socket.getInputStream();
69   }
70
71
72   /**
73    * Gets the OutputStream of this connection. Don't close this stream,
74    * but close the HttpConnection when finished.
75    *
76    * @return an OutputStream or null if no connection is established
77    * @exception IOException if an I/O error occurs when creating the stream
78    */

79   public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
80     if (socket == null) throw new IOException JavaDoc("not conected");
81     return socket.getOutputStream();
82   }
83
84
85   /**
86    * Close this connection (including the streams)
87    */

88   public void close() {
89     try {
90       socket.close();
91     } catch (IOException JavaDoc e) {
92       // do not throw an I/O error on close
93
}
94   }
95
96
97   /**
98    * Create a new HttpConnection
99    */

100   protected HttpConnection() {
101   }
102
103   /**
104    * Construct an HTTP connection object based on an existing socket
105    * connection.
106    *
107    * @param socket a Socket object containing a socket connection
108    * that is already established.
109    */

110   public HttpConnection(Socket JavaDoc socket) {
111     this.socket=socket;
112   }
113 }
114
Popular Tags