001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: package com.sun.midp.io.j2me.irdaobex;
026:
027: import com.sun.kvem.jsr082.obex.ObexTransport;
028: import com.sun.midp.main.Configuration;
029: import javax.microedition.io.Connection;
030: import javax.microedition.io.StreamConnection;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.io.IOException;
034:
035: /**
036: * Provides the implemetation of the OBEX over IrDA transport.
037: */
038: public class IrOBEXConnection implements ObexTransport {
039:
040: /** Indicates whether this connection has been closed */
041: boolean isClosed = false;
042:
043: /** Underlying connection. */
044: private StreamConnection conn = null;
045:
046: /** Input stream provided by the underlying connection. */
047: private InputStream input = null;
048:
049: /** Output stream provided by the underlying connection. */
050: private OutputStream output = null;
051:
052: /**
053: * Creates this connection using the underlying stream connection.
054: *
055: * @param conn undelying connection object
056: * @throws IOException if the connection can not be established
057: */
058: public IrOBEXConnection(StreamConnection conn) throws IOException {
059: this .conn = conn;
060: input = conn.openInputStream();
061: output = conn.openOutputStream();
062: }
063:
064: /**
065: * Closes this connection and the underlying connection.
066: *
067: * @throws IOException if an I/O error occurs
068: */
069: public void close() throws IOException {
070: synchronized (this ) {
071: if (isClosed) {
072: return;
073: }
074: isClosed = true;
075: }
076: input.close();
077: output.close();
078: conn.close();
079: }
080:
081: /**
082: * Reads the packet data into the specified buffer.
083: *
084: * @param inData destination buffer
085: * @throws IOException if an I/O error occurs
086: * @throws NullPointerException if the specified buffer is null
087: * @return number of bytes read
088: */
089: public int read(byte[] inData) throws IOException {
090: if (isClosed) {
091: throw new IOException("The connection is closed.");
092: }
093: if (inData == null) {
094: throw new NullPointerException("Input buffer is null.");
095: }
096: int size = getMaximumPacketSize();
097: int len = inData.length;
098: if (size == 0 || len <= size) {
099: return input.read(inData, 0, len);
100: }
101: int off = 0;
102: while (off < len) {
103: int count = input.read(inData, off, Math.min(size, len
104: - off));
105: if (count == 0) {
106: break;
107: }
108: off += count;
109: }
110: return off;
111: }
112:
113: /**
114: * Transfer the len bytes from specified packet over the irda connection.
115: *
116: * @param outData source buffer
117: * @param len data length
118: * @throws IOException if an I/O error occurs
119: * @throws NullPointerException if the specified buffer is null
120: */
121: public void write(byte[] outData, int len) throws IOException {
122: if (isClosed) {
123: throw new IOException("The connection is closed.");
124: }
125: if (outData == null) {
126: throw new NullPointerException("Output buffer is null.");
127: }
128: int size = getMaximumPacketSize();
129: if (size == 0 || len <= size) {
130: output.write(outData, 0, len);
131: return;
132: }
133: int off = 0;
134: while (off < len) {
135: int count = Math.min(size, len - off);
136: output.write(outData, off, count);
137: off += count;
138: }
139: }
140:
141: /**
142: * Returns the amount of data that can be successfully sent or received
143: * in a single read/write operation.
144: *
145: * @return the maximum packet size, or zero if any size may be used
146: */
147: public final int getMaximumPacketSize() {
148: return Configuration.getIntProperty("obex.packetLength.max", 0);
149: }
150:
151: /**
152: * Returns the underlying connection, or null if there is none.
153: *
154: * @return underlying connection object
155: */
156: public Connection getUnderlyingConnection() {
157: return conn;
158: }
159:
160: }
|