View Javadoc

1   package org.sourceforge.jemm.comm.connection.socket;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.io.OutputStream;
6   import java.net.Socket;
7   
8   import org.sourceforge.jemm.comm.connection.Connection;
9   import org.sourceforge.jemm.comm.connection.ConnectionException;
10  
11  public class SocketConnection implements Connection {
12  
13  	private final Socket socket;
14  	private final InputStream inputStream;
15  	private final OutputStream outputStream;
16  	private final String hostname;
17  	private final int port;
18  	
19  	
20  	public SocketConnection(Socket socket, InputStream is, OutputStream os,
21  			String hostname, int port) {
22  		this.socket = socket;
23  		this.inputStream = is;
24  		this.outputStream  = os;
25  		this.hostname = hostname;
26  		this.port = port;
27  	}
28  	
29  	@Override
30  	public void close() throws ConnectionException {
31  		if(!socket.isClosed()) {
32  			try {
33  				socket.close();
34  			}catch(IOException ioe) {
35  				throw new ConnectionException("Error closing underlying connection",ioe);
36  			}
37  		}
38  	}
39  
40  	@Override
41  	public InputStream getInputStream() {
42  		return inputStream;
43  	}
44  
45  	@Override
46  	public OutputStream getOutputStream() {
47  		return outputStream;
48  	}
49  
50  	@Override
51  	public String getConnectionName() {
52  		return "socket:" + hostname + ":" + port;
53  	}
54  }