001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.io.j2me.irdaobex;
027:
028: import javax.microedition.io.Connection;
029: import javax.microedition.io.StreamConnection;
030: import javax.microedition.io.StreamConnectionNotifier;
031: import java.io.InputStream;
032: import java.io.DataInputStream;
033: import java.io.OutputStream;
034: import java.io.DataOutputStream;
035: import java.io.IOException;
036: import java.io.InterruptedIOException;
037:
038: /**
039: * Provides the input stream implementation for the IrNativeConnection class.
040: * It simply forwards read() requests to its holder instance.
041: */
042: class IrNativeInputStream extends InputStream {
043:
044: /** IrNativeConnection instance which maintains this stream. */
045: private IrNativeConnection conn;
046:
047: /**
048: * Class constructor.
049: *
050: * @param conn holder of this instance.
051: */
052: public IrNativeInputStream(IrNativeConnection conn) {
053: this .conn = conn;
054: }
055:
056: /**
057: * Defines an abstract method of the interface. This method is not
058: * supposed to be used.
059: *
060: * @return this method does not return any value
061: * @throws RuntimeException on invocation
062: */
063: public int read() throws IOException {
064: throw new RuntimeException("read() method is not supported.");
065: }
066:
067: /**
068: * Reads a number of bytes to the specified byte array starting from the
069: * given offset. This method simply forwards the request to the owner
070: * IrNativeConnection instance.
071: *
072: * @param b destination byte array
073: * @param off offset in the array
074: * @param len number of bytes to read
075: * @return number of bytes actually read
076: * @throws IOException if an I/O error occurs
077: */
078: public int read(byte[] b, int off, int len) throws IOException {
079: return conn.read(b, off, len);
080: }
081:
082: }
083:
084: /**
085: * Provides the output stream implementation for the IrNativeConnection class.
086: * It simply forwards write() requests to its holder instance.
087: */
088: class IrNativeOutputStream extends OutputStream {
089:
090: /** IrNativeConnection instance which maintains this stream. */
091: private IrNativeConnection conn;
092:
093: /**
094: * Class constructor.
095: *
096: * @param conn holder of this instance.
097: */
098: public IrNativeOutputStream(IrNativeConnection conn) {
099: this .conn = conn;
100: }
101:
102: /**
103: * Defines an abstract method of the interface. This method is not
104: * supposed to be used.
105: *
106: * @param b this parameter is not used
107: * @throws RuntimeException on invocation
108: */
109: public void write(int b) throws IOException {
110: throw new RuntimeException(
111: "write(int) method is not supported.");
112: }
113:
114: /**
115: * Writes a number of bytes from the specified byte array starting from
116: * the given offset. This method simply forwards the request to the owner
117: * IrNativeConnection instance.
118: *
119: * @param b source byte array
120: * @param off offset in the array
121: * @param len number of bytes to write
122: * @throws IOException if an I/O error occurs
123: */
124: public void write(byte[] b, int off, int len) throws IOException {
125: conn.write(b, off, len);
126: }
127:
128: }
129:
130: /**
131: * Provides the implementation of a stream connection by the means of
132: * native calls. This class implements both Connection and ConnectionNotifier
133: * interfaces, and is used on client and server sides.
134: */
135: public class IrNativeConnection implements StreamConnection {
136:
137: /**
138: * Handle to the native connection. Corresponds to a network socket
139: * used in this connection. Only accessed from native code.
140: */
141: private int connHandle = getInvalidConnHandle();
142:
143: /** InputStream interface implementation returned by this instance. */
144: private IrNativeInputStream input = new IrNativeInputStream(this );
145:
146: /** OutputStream interface implementation returned by this instance. */
147: private IrNativeOutputStream output = new IrNativeOutputStream(this );
148:
149: /** Static initializer. */
150: static {
151: initialize();
152: }
153:
154: /**
155: * Native class initializer.
156: */
157: private static native void initialize();
158:
159: /**
160: * Class constructor.
161: */
162: public IrNativeConnection() {
163: }
164:
165: /**
166: * Native finalizer. Releases all native resources used by this connection.
167: */
168: private native void finalize();
169:
170: /**
171: * Discovers nearby devices with matching hint bits and IAS class name.
172: *
173: * @param hints hint bits to be used during discovery
174: * @param ias IAS string to be used during discovery
175: * @return integer array containing addresses of discovered devices
176: * @throws IOException if an I/O error occurs
177: */
178: public int[] discover(int hints, String ias) throws IOException {
179: int[] addr = new int[10];
180: int count = discover(hints, ias, addr);
181: int[] res = new int[count];
182: System.arraycopy(addr, 0, res, 0, count);
183: return res;
184: }
185:
186: /**
187: * Discovers devices in range with matching hint bits and IAS.
188: *
189: * @param hints hint bits to be used during discovery
190: * @param ias IAS string to be used during discovery
191: * @param addr output array to receive addresses of discovered devices
192: * @return number of discovered devices
193: * @throws IOException if an I/O error occurs
194: */
195: private native int discover(int hints, String ias, int[] addr)
196: throws IOException;
197:
198: /**
199: * Establishes a new connection to device with the specified address.
200: *
201: * @param addr address of the target device
202: * @param ias IAS class name of the target service
203: * @return true if the connection was established, false otherwise
204: * @throws IOException if an I/O error occurs
205: */
206: public native boolean connect(int addr, String ias)
207: throws IOException;
208:
209: /**
210: * Closes this connection.
211: *
212: * @throws IOException if an I/O error occurs
213: */
214: public native void close() throws IOException;
215:
216: /**
217: * Opens and returns an input stream for this connection
218: *
219: * @return an input stream
220: * @throws IOException if an I/O error occurs
221: */
222: public InputStream openInputStream() throws IOException {
223: return input;
224: }
225:
226: /**
227: * Defines an abstract method of the interface. This method is not
228: * supposed to be used.
229: *
230: * @return this method does not return any value
231: * @throws RuntimeException on invocation
232: */
233: public DataInputStream openDataInputStream() throws IOException {
234: throw new RuntimeException("Method is not supported.");
235: }
236:
237: /**
238: * Opens and returns an output stream for this connection
239: *
240: * @return an output stream
241: * @throws IOException if an I/O error occurs
242: */
243: public OutputStream openOutputStream() throws IOException {
244: return output;
245: }
246:
247: /**
248: * Defines an abstract method of the interface. This method is not
249: * supposed to be used.
250: *
251: * @return this method does not return any value
252: * @throws RuntimeException on invocation
253: */
254: public DataOutputStream openDataOutputStream() throws IOException {
255: throw new RuntimeException("Method is not supported.");
256: }
257:
258: /**
259: * Reads a number of bytes to the specified byte array starting from the
260: * given offset. This method uses its native counterpart for the actual
261: * data delivery.
262: *
263: * @param b destination byte array
264: * @param off offset in the array
265: * @param len number of bytes to read
266: * @return number of bytes actually read
267: * @throws IOException if an I/O error occurs
268: */
269: public native int read(byte[] b, int off, int len)
270: throws IOException;
271:
272: /**
273: * Writes a number of bytes from the specified byte array starting from
274: * the given offset. This method uses its native counterpart for the actual
275: * data delivery.
276: *
277: * @param b source byte array
278: * @param off offset in the array
279: * @param len number of bytes to write
280: * @throws IOException if an I/O error occurs
281: */
282: public native void write(byte[] b, int off, int len)
283: throws IOException;
284:
285: /**
286: * Returns an invalid handle value to be used during initialization of
287: * instances of this class.
288: *
289: * @return a native handle value considered to be invalid
290: */
291: private static native int getInvalidConnHandle();
292:
293: }
|