001: package net.matuschek.http.connection;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.io.InterruptedIOException;
006: import java.io.OutputStream;
007: import java.net.InetAddress;
008: import java.net.Socket;
009: import net.matuschek.util.TimedSocket;
010:
011: /*********************************************
012: Copyright (c) 2001 by Daniel Matuschek
013: *********************************************/
014:
015: /**
016: * An HttpConnection object simply represents a TCP socket connection
017: * between the client and an HTTP server.
018: *
019: * @author Daniel Matuschek
020: * @version $Id: HttpConnection.java,v 1.4 2002/05/31 14:45:55 matuschd Exp $
021: */
022: public class HttpConnection {
023:
024: /** the TCP socket to use */
025: private Socket socket = null;
026:
027: /**
028: * Create a new HttpConnection to the given host and port.
029: *
030: * @param address the IP address to connect to
031: * @param port the port to connect to (usually 80 for HTTP)
032: * @param timeout connection timeout in milliseconds. If the connection
033: * could not be established after this time, an exception will
034: * be thrown. This timeout will also be set as timeout for the
035: * TCP connection (so timeout)
036: * An timeout of 0 will be interpreted as an infinite timeout.
037: * @return a new HttpConnection object
038: * @exception IOException if the TCP socket connection could
039: * not be established
040: */
041: public static HttpConnection createConnection(InetAddress address,
042: int port, int timeout) throws IOException {
043: HttpConnection connection = new HttpConnection();
044: try {
045: connection.socket = TimedSocket.getSocket(address, port,
046: timeout);
047: connection.socket.setSoTimeout(timeout);
048: } catch (InterruptedIOException e) {
049: throw new IOException("timeout during connect: "
050: + e.getMessage());
051: }
052: return connection;
053: }
054:
055: /**
056: * Gets the InputStream of this connection. Don't close this stream,
057: * but close the HttpConnection when finished.
058: *
059: * @return an InputStream or null if no connection is established
060: * @exception IOException if an I/O error occurs when creating the stream
061: */
062: public InputStream getInputStream() throws IOException {
063: if (socket == null)
064: throw new IOException("not conected");
065: return socket.getInputStream();
066: }
067:
068: /**
069: * Gets the OutputStream of this connection. Don't close this stream,
070: * but close the HttpConnection when finished.
071: *
072: * @return an OutputStream or null if no connection is established
073: * @exception IOException if an I/O error occurs when creating the stream
074: */
075: public OutputStream getOutputStream() throws IOException {
076: if (socket == null)
077: throw new IOException("not conected");
078: return socket.getOutputStream();
079: }
080:
081: /**
082: * Close this connection (including the streams)
083: */
084: public void close() {
085: try {
086: socket.close();
087: } catch (IOException e) {
088: // do not throw an I/O error on close
089: }
090: }
091:
092: /**
093: * Create a new HttpConnection
094: */
095: protected HttpConnection() {
096: }
097:
098: /**
099: * Construct an HTTP connection object based on an existing socket
100: * connection.
101: *
102: * @param socket a Socket object containing a socket connection
103: * that is already established.
104: */
105: public HttpConnection(Socket socket) {
106: this.socket = socket;
107: }
108: }
|