001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.obex.demo;
033:
034: import java.io.ByteArrayOutputStream;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.OutputStream;
038:
039: import javax.microedition.io.Connector;
040:
041: import javax.obex.ClientSession;
042: import javax.obex.HeaderSet;
043: import javax.obex.Operation;
044: import javax.obex.ResponseCodes;
045:
046: final class ObexImageSender implements Runnable {
047: /** Shows if debug prints should be done. */
048: private static final boolean DEBUG = false;
049:
050: /** Indicate that uploading should be stopped */
051: private boolean stop = true;
052:
053: /** Stream with image data */
054: private InputStream imageSource;
055:
056: /** Special stream to load image data to byte array */
057: private ByteArrayOutputStream baos;
058:
059: /** Session to send an image */
060: private ClientSession session;
061:
062: /** Put Operation of image uploading process */
063: private Operation operation;
064:
065: /** Output stream of obex */
066: private OutputStream outputStream;
067:
068: /** Array with image data */
069: private byte[] imageData;
070:
071: /** Contains name of uploading image */
072: private String imageName;
073:
074: /** Reference to GUI part of sender */
075: private GUIImageSender gui;
076:
077: ObexImageSender(GUIImageSender gui) {
078: this .gui = gui;
079: }
080:
081: void setImageName(String imageName) {
082: this .imageName = imageName;
083: }
084:
085: /**
086: * Used to send an image
087: */
088: public void run() {
089: boolean loaded = false;
090: boolean connected = false;
091: stop = false;
092:
093: try {
094: loadImageData(imageName);
095: loaded = true;
096: connect();
097:
098: if (stop) {
099: throw new IOException();
100: }
101:
102: connected = true;
103: uploadImage(imageName);
104: } catch (IOException e) {
105: if (DEBUG) {
106: e.printStackTrace();
107: }
108:
109: if (!stop) {
110: if (connected) {
111: gui.stopMessage();
112: } else if (loaded) {
113: gui.notReadyMessage();
114: } else {
115: gui.errorMessage();
116: }
117:
118: closeAll();
119:
120: return;
121: }
122: } catch (Throwable e) {
123: e.printStackTrace();
124: }
125:
126: closeAll();
127: gui.showImageList();
128: }
129:
130: /** load image data to array */
131: private void loadImageData(String imageName) throws IOException {
132: imageSource = getClass().getResourceAsStream(imageName);
133:
134: // read image data and create a byte array
135: byte[] buff = new byte[1024];
136: baos = new ByteArrayOutputStream(1024);
137:
138: while (true) {
139: // check stop signal
140: if (stop) {
141: throw new IOException();
142: }
143:
144: int length = imageSource.read(buff);
145:
146: if (length == -1) {
147: break;
148: }
149:
150: baos.write(buff, 0, length);
151: }
152:
153: imageData = baos.toByteArray();
154: }
155:
156: /** Connects with image receiver */
157: private void connect() throws IOException {
158: //String url = "tcpobex://localhost:5010";
159: String url = "irdaobex://discover.0210;ias=ImageExchange";
160:
161: // update the Gauge message before connecting
162: gui.showProgress("Connecting ...", imageData.length);
163:
164: session = (ClientSession) Connector.open(url);
165:
166: if (stop) {
167: throw new IOException();
168: }
169:
170: // update the Gauge message after connecting
171: gui.showProgress("Uploading Image ...", imageData.length);
172:
173: HeaderSet response = session.connect(null);
174: int responseCode = response.getResponseCode();
175:
176: if (responseCode != ResponseCodes.OBEX_HTTP_OK) {
177: throw new IOException();
178: }
179: }
180:
181: /** Uploads image to receiver */
182: private void uploadImage(String imageName) throws IOException {
183: int position = 0;
184:
185: // start put operation
186: HeaderSet headers = session.createHeaderSet();
187: headers.setHeader(HeaderSet.NAME, imageName);
188: headers.setHeader(HeaderSet.LENGTH, new Long(imageData.length));
189: operation = session.put(headers);
190: outputStream = operation.openOutputStream();
191:
192: while (position != imageData.length) {
193: OutputStream outputStream = this .outputStream;
194: int sendLength = ((imageData.length - position) > 256) ? 256
195: : (imageData.length - position);
196:
197: if (outputStream == null) {
198: throw new IOException();
199: }
200:
201: outputStream.write(imageData, position, sendLength);
202: position += sendLength;
203: gui.updateProgress(position);
204: }
205:
206: outputStream.close();
207:
208: int code = operation.getResponseCode();
209:
210: if (code != ResponseCodes.OBEX_HTTP_OK) {
211: throw new IOException();
212: }
213: }
214:
215: /** Stops uploading process */
216: void stop() {
217: stop = true;
218: }
219:
220: /** Closes all connections */
221: private void closeAll() {
222: imageData = null;
223:
224: if (imageSource != null) {
225: try {
226: imageSource.close();
227: } catch (IOException ioe) {
228: }
229:
230: imageSource = null;
231: }
232:
233: if (baos != null) {
234: try {
235: baos.close();
236: } catch (IOException ioe) {
237: }
238:
239: baos = null;
240: }
241:
242: if (outputStream != null) {
243: try {
244: outputStream.close();
245: } catch (IOException ioe) {
246: }
247:
248: outputStream = null;
249: }
250:
251: if (operation != null) {
252: try {
253: operation.close();
254: } catch (IOException ioe) {
255: }
256:
257: operation = null;
258: }
259:
260: if (session != null) {
261: try {
262: session.disconnect(null);
263: } catch (IOException ioe) {
264: }
265:
266: try {
267: session.close();
268: } catch (IOException ioe) {
269: }
270:
271: session = null;
272: }
273: }
274: }
|