View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001-2003 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.microemu.cldc.socket;
21  
22  import java.io.DataInputStream;
23  import java.io.DataOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.net.Socket;
28  
29  public class SocketConnection implements javax.microedition.io.SocketConnection {
30  
31  	protected Socket socket;
32  	
33  	public SocketConnection() {		
34  	}
35  
36  	public SocketConnection(String host, int port) throws IOException {
37  		this.socket = new Socket(host, port);
38  	}
39  	
40  	public SocketConnection(Socket socket) {
41  		this.socket = socket;
42  	}
43  
44  	public String getAddress() throws IOException {
45  		if (socket == null || socket.isClosed()) {
46  			throw new IOException();
47  		}
48  
49  		return socket.getInetAddress().toString();
50  	}
51  
52  	public String getLocalAddress() throws IOException {
53  		if (socket == null || socket.isClosed()) {
54  			throw new IOException();
55  		}
56  
57  		return socket.getLocalAddress().toString();
58  	}
59  
60  	public int getLocalPort() throws IOException {
61  		if (socket == null || socket.isClosed()) {
62  			throw new IOException();
63  		}
64  
65  		return socket.getLocalPort();
66  	}
67  
68  	public int getPort() throws IOException {
69  		if (socket == null || socket.isClosed()) {
70  			throw new IOException();
71  		}
72  
73  		return socket.getPort();
74  	}
75  
76  	public int getSocketOption(byte option) throws IllegalArgumentException,
77  			IOException {
78  		if (socket != null && socket.isClosed()) {
79  			throw new IOException();
80  		}
81  		switch (option) {
82  		case DELAY:
83  			if (socket.getTcpNoDelay()) {
84  				return 1;
85  			} else {
86  				return 0;
87  			}
88  		case LINGER:
89  			int value = socket.getSoLinger();
90  			if (value == -1) {
91  				return 0;
92  			} else {
93  				return value;
94  			}
95  		case KEEPALIVE:
96  			if (socket.getKeepAlive()) {
97  				return 1;
98  			} else {
99  				return 0;
100 			}
101 		case RCVBUF:
102 			return socket.getReceiveBufferSize();
103 		case SNDBUF:
104 			return socket.getSendBufferSize();
105 		default:
106 			throw new IllegalArgumentException();
107 		}
108 	}
109 
110 	public void setSocketOption(byte option, int value)
111 			throws IllegalArgumentException, IOException {
112 		if (socket.isClosed()) {
113 			throw new IOException();
114 		}
115 		switch (option) {
116 		case DELAY:
117 			int delay;
118 			if (value == 0) {
119 				delay = 0;
120 			} else {
121 				delay = 1;
122 			}
123 			socket.setTcpNoDelay(delay == 0 ? false : true);
124 			break;
125 		case LINGER:
126 			if (value < 0) {
127 				throw new IllegalArgumentException();
128 			}
129 			socket.setSoLinger(value == 0 ? false : true, value);
130 			break;
131 		case KEEPALIVE:
132 			int keepalive;
133 			if (value == 0) {
134 				keepalive = 0;
135 			} else {
136 				keepalive = 1;
137 			}
138 			socket.setKeepAlive(keepalive == 0 ? false : true);
139 			break;
140 		case RCVBUF:
141 			if (value <= 0) {
142 				throw new IllegalArgumentException();
143 			}
144 			socket.setReceiveBufferSize(value);
145 			break;
146 		case SNDBUF:
147 			if (value <= 0) {
148 				throw new IllegalArgumentException();
149 			}
150 			socket.setSendBufferSize(value);
151 			break;
152 		default:
153 			throw new IllegalArgumentException();
154 		}
155 	}
156 
157 	public void close() throws IOException {
158 		// TODO fix differences between Java ME and Java SE
159 		
160 		socket.close();
161 	}
162 
163 	public InputStream openInputStream() throws IOException {
164 		return socket.getInputStream();
165 	}
166 
167 	public DataInputStream openDataInputStream() throws IOException {
168 		return new DataInputStream(openInputStream());
169 	}
170 
171 	public OutputStream openOutputStream() throws IOException {
172 		return socket.getOutputStream();
173 	}
174 
175 	public DataOutputStream openDataOutputStream() throws IOException {
176 		return new DataOutputStream(openOutputStream());
177 	}
178 
179 }