1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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 }