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 net.sf.bluecove.obex;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.InputStream;
31 import java.net.URL;
32
33
34
35
36
37 public class Deploy implements UserInteraction {
38
39 private String fileName;
40
41 private int progressMaximum;
42
43 public static void main(String[] args) {
44 if ((args.length < 2) || args[0].equalsIgnoreCase("--help")) {
45 StringBuffer usage = new StringBuffer();
46 usage.append("Usage:\n java ").append(Deploy.class.getName());
47 usage.append(" bluetoothURL yourApp.jar\n");
48 System.out.println(usage);
49 System.exit(1);
50 return;
51 }
52 String obexUrl = args[0];
53
54 String filePath = args[1];
55
56 Logger.debugOn = false;
57 Deploy d = new Deploy();
58 byte[] data = d.readFile(filePath);
59 if (data == null) {
60 System.exit(1);
61 return;
62 }
63 ObexBluetoothClient o = new ObexBluetoothClient(d, d.fileName, data);
64 if (o.obexPut(obexUrl)) {
65 System.exit(0);
66 } else {
67 System.exit(2);
68 }
69 }
70
71 private Deploy() {
72 }
73
74 private static String simpleFileName(String filePath) {
75 int idx = filePath.lastIndexOf('/');
76 if (idx == -1) {
77 idx = filePath.lastIndexOf('\\');
78 }
79 if (idx == -1) {
80 return filePath;
81 }
82 return filePath.substring(idx + 1);
83 }
84
85 private byte[] readFile(final String filePath) {
86 InputStream is = null;
87 byte[] data = null;
88 try {
89 String path = filePath;
90 String inputFileName;
91 File file = new File(filePath);
92 if (file.exists()) {
93 is = new FileInputStream(file);
94 inputFileName = file.getName();
95 } else {
96 URL url = new URL(path);
97 is = url.openConnection().getInputStream();
98 inputFileName = url.getFile();
99 }
100 ByteArrayOutputStream bos = new ByteArrayOutputStream();
101 byte[] buffer = new byte[0xFF];
102 int i = is.read(buffer);
103 int done = 0;
104 while (i != -1) {
105 bos.write(buffer, 0, i);
106 done += i;
107
108 i = is.read(buffer);
109 }
110 data = bos.toByteArray();
111 fileName = simpleFileName(inputFileName);
112 showStatus((data.length / 1024) + "k " + fileName);
113 } catch (Throwable e) {
114 Logger.error(e);
115 showStatus("Download error " + e.getMessage());
116 } finally {
117 IOUtils.closeQuietly(is);
118 }
119 return data;
120 }
121
122
123
124
125
126
127 public void setProgressMaximum(int n) {
128 progressMaximum = n;
129 }
130
131
132
133
134
135
136 public void setProgressValue(int n) {
137
138
139 }
140
141
142
143
144
145
146 public void setProgressDone() {
147
148 }
149
150
151
152
153
154
155 public void showStatus(String message) {
156 System.out.println(message);
157 }
158
159 }