1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package com.intel.bluetooth.gcf.socket;
23
24 import java.io.DataInputStream;
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.net.Socket;
30
31 public class SocketConnection implements javax.microedition.io.SocketConnection {
32
33 protected Socket socket;
34
35 public SocketConnection() {
36 }
37
38 public SocketConnection(String host, int port) throws IOException {
39 this.socket = new Socket(host, port);
40 }
41
42 public SocketConnection(Socket socket) {
43 this.socket = socket;
44 }
45
46 public String getAddress() throws IOException {
47 if (socket == null || socket.isClosed()) {
48 throw new IOException();
49 }
50
51 return socket.getInetAddress().toString();
52 }
53
54 public String getLocalAddress() throws IOException {
55 if (socket == null || socket.isClosed()) {
56 throw new IOException();
57 }
58
59 return socket.getLocalAddress().toString();
60 }
61
62 public int getLocalPort() throws IOException {
63 if (socket == null || socket.isClosed()) {
64 throw new IOException();
65 }
66
67 return socket.getLocalPort();
68 }
69
70 public int getPort() throws IOException {
71 if (socket == null || socket.isClosed()) {
72 throw new IOException();
73 }
74
75 return socket.getPort();
76 }
77
78 public int getSocketOption(byte option) throws IllegalArgumentException,
79 IOException {
80 if (socket != null && socket.isClosed()) {
81 throw new IOException();
82 }
83 switch (option) {
84 case DELAY:
85 if (socket.getTcpNoDelay()) {
86 return 1;
87 } else {
88 return 0;
89 }
90 case LINGER:
91 int value = socket.getSoLinger();
92 if (value == -1) {
93 return 0;
94 } else {
95 return value;
96 }
97 case KEEPALIVE:
98 if (socket.getKeepAlive()) {
99 return 1;
100 } else {
101 return 0;
102 }
103 case RCVBUF:
104 return socket.getReceiveBufferSize();
105 case SNDBUF:
106 return socket.getSendBufferSize();
107 default:
108 throw new IllegalArgumentException();
109 }
110 }
111
112 public void setSocketOption(byte option, int value)
113 throws IllegalArgumentException, IOException {
114 if (socket.isClosed()) {
115 throw new IOException();
116 }
117 switch (option) {
118 case DELAY:
119 int delay;
120 if (value == 0) {
121 delay = 0;
122 } else {
123 delay = 1;
124 }
125 socket.setTcpNoDelay(delay == 0 ? false : true);
126 break;
127 case LINGER:
128 if (value < 0) {
129 throw new IllegalArgumentException();
130 }
131 socket.setSoLinger(value == 0 ? false : true, value);
132 break;
133 case KEEPALIVE:
134 int keepalive;
135 if (value == 0) {
136 keepalive = 0;
137 } else {
138 keepalive = 1;
139 }
140 socket.setKeepAlive(keepalive == 0 ? false : true);
141 break;
142 case RCVBUF:
143 if (value <= 0) {
144 throw new IllegalArgumentException();
145 }
146 socket.setReceiveBufferSize(value);
147 break;
148 case SNDBUF:
149 if (value <= 0) {
150 throw new IllegalArgumentException();
151 }
152 socket.setSendBufferSize(value);
153 break;
154 default:
155 throw new IllegalArgumentException();
156 }
157 }
158
159 public void close() throws IOException {
160
161
162 socket.close();
163 }
164
165 public InputStream openInputStream() throws IOException {
166 return socket.getInputStream();
167 }
168
169 public DataInputStream openDataInputStream() throws IOException {
170 return new DataInputStream(openInputStream());
171 }
172
173 public OutputStream openOutputStream() throws IOException {
174 return socket.getOutputStream();
175 }
176
177 public DataOutputStream openDataOutputStream() throws IOException {
178 return new DataOutputStream(openOutputStream());
179 }
180 }