1 package net.matuschek.http.connection; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.InterruptedIOException ; 6 import java.io.OutputStream ; 7 import java.net.InetAddress ; 8 import java.net.Socket ; 9 import net.matuschek.util.TimedSocket; 10 13 14 15 16 17 24 public class HttpConnection { 25 26 27 private Socket socket = null; 28 29 30 44 public static HttpConnection createConnection(InetAddress address, 45 int port, 46 int timeout) 47 throws IOException 48 { 49 HttpConnection connection = new HttpConnection(); 50 try { 51 connection.socket = TimedSocket.getSocket(address, port, timeout); 52 connection.socket.setSoTimeout(timeout); 53 } catch (InterruptedIOException e) { 54 throw new IOException ("timeout during connect: "+e.getMessage()); 55 } 56 return connection; 57 } 58 59 66 public InputStream getInputStream() throws IOException { 67 if (socket == null) throw new IOException ("not conected"); 68 return socket.getInputStream(); 69 } 70 71 72 79 public OutputStream getOutputStream() throws IOException { 80 if (socket == null) throw new IOException ("not conected"); 81 return socket.getOutputStream(); 82 } 83 84 85 88 public void close() { 89 try { 90 socket.close(); 91 } catch (IOException e) { 92 } 94 } 95 96 97 100 protected HttpConnection() { 101 } 102 103 110 public HttpConnection(Socket socket) { 111 this.socket=socket; 112 } 113 } 114 | Popular Tags |