View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001-2003 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  It is licensed under the following two licenses as alternatives:
6    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
7    *    2. Apache License (the "AL") Version 2.0
8    *
9    *  You may not use this file except in compliance with at least one of
10   *  the above two licenses.
11   *
12   *  You may obtain a copy of the LGPL at
13   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
14   *
15   *  You may obtain a copy of the AL at
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the LGPL or the AL for the specific language governing permissions and
22   *  limitations.
23   */
24  
25  package org.microemu.cldc.socket;
26  
27  import java.io.DataInputStream;
28  import java.io.DataOutputStream;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.net.Socket;
33  
34  public class SocketConnection implements javax.microedition.io.SocketConnection {
35  
36  	protected Socket socket;
37  	
38  	public SocketConnection() {		
39  	}
40  
41  	public SocketConnection(String host, int port) throws IOException {
42  		this.socket = new Socket(host, port);
43  	}
44  	
45  	public SocketConnection(Socket socket) {
46  		this.socket = socket;
47  	}
48  
49  	public String getAddress() throws IOException {
50  		if (socket == null || socket.isClosed()) {
51  			throw new IOException();
52  		}
53  
54  		return socket.getInetAddress().toString();
55  	}
56  
57  	public String getLocalAddress() throws IOException {
58  		if (socket == null || socket.isClosed()) {
59  			throw new IOException();
60  		}
61  
62  		return socket.getLocalAddress().toString();
63  	}
64  
65  	public int getLocalPort() throws IOException {
66  		if (socket == null || socket.isClosed()) {
67  			throw new IOException();
68  		}
69  
70  		return socket.getLocalPort();
71  	}
72  
73  	public int getPort() throws IOException {
74  		if (socket == null || socket.isClosed()) {
75  			throw new IOException();
76  		}
77  
78  		return socket.getPort();
79  	}
80  
81  	public int getSocketOption(byte option) throws IllegalArgumentException,
82  			IOException {
83  		if (socket != null && socket.isClosed()) {
84  			throw new IOException();
85  		}
86  		switch (option) {
87  		case DELAY:
88  			if (socket.getTcpNoDelay()) {
89  				return 1;
90  			} else {
91  				return 0;
92  			}
93  		case LINGER:
94  			int value = socket.getSoLinger();
95  			if (value == -1) {
96  				return 0;
97  			} else {
98  				return value;
99  			}
100 		case KEEPALIVE:
101 			if (socket.getKeepAlive()) {
102 				return 1;
103 			} else {
104 				return 0;
105 			}
106 		case RCVBUF:
107 			return socket.getReceiveBufferSize();
108 		case SNDBUF:
109 			return socket.getSendBufferSize();
110 		default:
111 			throw new IllegalArgumentException();
112 		}
113 	}
114 
115 	public void setSocketOption(byte option, int value)
116 			throws IllegalArgumentException, IOException {
117 		if (socket.isClosed()) {
118 			throw new IOException();
119 		}
120 		switch (option) {
121 		case DELAY:
122 			int delay;
123 			if (value == 0) {
124 				delay = 0;
125 			} else {
126 				delay = 1;
127 			}
128 			socket.setTcpNoDelay(delay == 0 ? false : true);
129 			break;
130 		case LINGER:
131 			if (value < 0) {
132 				throw new IllegalArgumentException();
133 			}
134 			socket.setSoLinger(value == 0 ? false : true, value);
135 			break;
136 		case KEEPALIVE:
137 			int keepalive;
138 			if (value == 0) {
139 				keepalive = 0;
140 			} else {
141 				keepalive = 1;
142 			}
143 			socket.setKeepAlive(keepalive == 0 ? false : true);
144 			break;
145 		case RCVBUF:
146 			if (value <= 0) {
147 				throw new IllegalArgumentException();
148 			}
149 			socket.setReceiveBufferSize(value);
150 			break;
151 		case SNDBUF:
152 			if (value <= 0) {
153 				throw new IllegalArgumentException();
154 			}
155 			socket.setSendBufferSize(value);
156 			break;
157 		default:
158 			throw new IllegalArgumentException();
159 		}
160 	}
161 
162 	public void close() throws IOException {
163 		// TODO fix differences between Java ME and Java SE
164 		
165 		socket.close();
166 	}
167 
168 	public InputStream openInputStream() throws IOException {
169 		return socket.getInputStream();
170 	}
171 
172 	public DataInputStream openDataInputStream() throws IOException {
173 		return new DataInputStream(openInputStream());
174 	}
175 
176 	public OutputStream openOutputStream() throws IOException {
177 		return socket.getOutputStream();
178 	}
179 
180 	public DataOutputStream openDataOutputStream() throws IOException {
181 		return new DataOutputStream(openOutputStream());
182 	}
183 
184 }