Mercurial > 510Connectbot
comparison src/org/tn5250j/framework/tn5250/DataStreamProducer.java @ 112:77ac18bc1b2f
cleanup java formatting
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 18 Jun 2014 13:03:01 -0700 |
parents | eda03b809f48 |
children | 3e68cb17c0b6 |
comparison
equal
deleted
inserted
replaced
111:6a0ad4d384ea | 112:77ac18bc1b2f |
---|---|
15 import android.util.Log; | 15 import android.util.Log; |
16 | 16 |
17 | 17 |
18 public class DataStreamProducer implements Runnable { | 18 public class DataStreamProducer implements Runnable { |
19 private static final String TAG = "DataStreamProducer"; | 19 private static final String TAG = "DataStreamProducer"; |
20 private tnvt vt; | 20 private tnvt vt; |
21 private BufferedInputStream bin; | 21 private BufferedInputStream bin; |
22 private ByteArrayOutputStream baosin; | 22 private ByteArrayOutputStream baosin; |
23 private Thread me; | 23 private Thread me; |
24 private byte[] saveStream; | 24 private byte[] saveStream; |
25 private final BlockingQueue<Object> dsq; | 25 private final BlockingQueue<Object> dsq; |
26 private byte[] abyte2; | 26 private byte[] abyte2; |
27 private FileOutputStream fw; | 27 private FileOutputStream fw; |
28 private BufferedOutputStream dw; | 28 private BufferedOutputStream dw; |
29 private boolean dumpBytes = false; | 29 private boolean dumpBytes = false; |
30 private ICodePage codePage; | 30 private ICodePage codePage; |
31 | 31 |
32 | 32 |
33 | 33 |
34 public DataStreamProducer(tnvt vt, BufferedInputStream bin, BlockingQueue<Object> queue, byte[] init) { | 34 public DataStreamProducer(tnvt vt, BufferedInputStream bin, BlockingQueue<Object> queue, byte[] init) { |
35 this.bin = bin; | 35 this.bin = bin; |
36 this.vt = vt; | 36 this.vt = vt; |
37 baosin = new ByteArrayOutputStream(); | 37 baosin = new ByteArrayOutputStream(); |
38 dsq = queue; | 38 dsq = queue; |
39 abyte2 = init; | 39 abyte2 = init; |
40 } | 40 } |
41 | 41 |
42 public void setInputStream(ByteArrayOutputStream is) { | 42 public void setInputStream(ByteArrayOutputStream is) { |
43 | 43 baosin = is; |
44 baosin = is; | 44 } |
45 | 45 |
46 } | 46 public final void run() { |
47 | 47 boolean done = false; |
48 public final void run() { | 48 me = Thread.currentThread(); |
49 | 49 // load the first response screen |
50 boolean done = false; | 50 loadStream(abyte2, 0); |
51 | 51 |
52 me = Thread.currentThread(); | 52 while (!done) { |
53 | 53 try { |
54 // load the first response screen | 54 byte[] abyte0 = readIncoming(); |
55 loadStream(abyte2, 0); | 55 |
56 | 56 // WVL - LDC : 17/05/2004 : Device name negotiations send TIMING MARK |
57 while (!done) { | 57 // Restructured to the readIncoming() method to return null |
58 try { | 58 // on TIMING MARK. Don't process in that case (abyte0 == null)! |
59 | 59 if (abyte0 != null) { |
60 byte[] abyte0 = readIncoming(); | 60 // WVL - LDC : 16/07/2003 : TR.000345 |
61 | 61 // When the socket has been closed, the reading returns |
62 // WVL - LDC : 17/05/2004 : Device name negotiations send TIMING MARK | 62 // no bytes (an empty byte arrray). |
63 // Restructured to the readIncoming() method to return null | 63 // But the loadStream fails on this, so we check it here! |
64 // on TIMING MARK. Don't process in that case (abyte0 == null)! | 64 if (abyte0.length > 0) { |
65 if (abyte0 != null) | 65 loadStream(abyte0, 0); |
66 { | 66 } |
67 // WVL - LDC : 16/07/2003 : TR.000345 | 67 // WVL - LDC : 16/07/2003 : TR.000345 |
68 // When the socket has been closed, the reading returns | 68 // Returning no bytes means the input buffer has |
69 // no bytes (an empty byte arrray). | 69 // reached end-of-stream, so we do a disconnect! |
70 // But the loadStream fails on this, so we check it here! | 70 else { |
71 if (abyte0.length > 0) | 71 done = true; |
72 { | 72 vt.disconnect(); |
73 loadStream(abyte0, 0); | 73 } |
74 } | 74 } |
75 // WVL - LDC : 16/07/2003 : TR.000345 | 75 } |
76 // Returning no bytes means the input buffer has | 76 catch (SocketException se) { |
77 // reached end-of-stream, so we do a disconnect! | 77 Log.w(TAG, " DataStreamProducer thread interrupted and stopping " + se.getMessage()); |
78 else | 78 done = true; |
79 { | 79 } |
80 done = true; | 80 catch (IOException ioe) { |
81 vt.disconnect(); | 81 Log.w(TAG, ioe.getMessage()); |
82 } | 82 |
83 } | 83 if (me.isInterrupted()) |
84 | 84 done = true; |
85 } | 85 } |
86 | 86 catch (Exception ex) { |
87 catch (SocketException se) { | 87 Log.w(TAG, ex.getMessage()); |
88 Log.w(TAG," DataStreamProducer thread interrupted and stopping " + se.getMessage()); | 88 |
89 done = true; | 89 if (me.isInterrupted()) |
90 } | 90 done = true; |
91 | 91 } |
92 catch (IOException ioe) { | 92 } |
93 | 93 } |
94 Log.w(TAG,ioe.getMessage()); | 94 |
95 if (me.isInterrupted()) | 95 private final void loadStream(byte abyte0[], int i) { |
96 done = true; | 96 int j = 0; |
97 | 97 int size = 0; |
98 } | 98 |
99 catch (Exception ex) { | 99 if (saveStream == null) { |
100 | 100 j = (abyte0[i] & 0xff) << 8 | abyte0[i + 1] & 0xff; |
101 Log.w(TAG,ex.getMessage()); | 101 size = abyte0.length; |
102 if (me.isInterrupted()) | 102 } |
103 done = true; | 103 else { |
104 | 104 size = saveStream.length + abyte0.length; |
105 } | 105 byte[] inter = new byte[size]; |
106 } | 106 System.arraycopy(saveStream, 0, inter, 0, saveStream.length); |
107 } | 107 System.arraycopy(abyte0, 0, inter, saveStream.length, abyte0.length); |
108 | 108 abyte0 = new byte[size]; |
109 private final void loadStream(byte abyte0[], int i) { | 109 System.arraycopy(inter, 0, abyte0, 0, size); |
110 | 110 saveStream = null; |
111 int j = 0; | 111 inter = null; |
112 int size = 0; | 112 j = (abyte0[i] & 0xff) << 8 | abyte0[i + 1] & 0xff; |
113 if (saveStream == null) { | 113 Log.d(TAG, "partial stream found"); |
114 j = (abyte0[i] & 0xff) << 8 | abyte0[i + 1] & 0xff; | 114 } |
115 size = abyte0.length; | 115 |
116 } | 116 if (j > size) { |
117 else { | 117 saveStream = new byte[abyte0.length]; |
118 size = saveStream.length + abyte0.length; | 118 System.arraycopy(abyte0, 0, saveStream, 0, abyte0.length); |
119 byte[] inter = new byte[size]; | 119 Log.d(TAG, "partial stream saved"); |
120 System.arraycopy(saveStream, 0, inter, 0, saveStream.length); | 120 } |
121 System.arraycopy(abyte0, 0, inter, saveStream.length, abyte0.length); | 121 else { |
122 abyte0 = new byte[size]; | 122 byte abyte1[]; |
123 System.arraycopy(inter, 0, abyte0, 0, size); | 123 |
124 saveStream = null; | 124 try { |
125 inter = null; | 125 abyte1 = new byte[j + 2]; |
126 j = (abyte0[i] & 0xff) << 8 | abyte0[i + 1] & 0xff; | 126 System.arraycopy(abyte0, i, abyte1, 0, j + 2); |
127 Log.d(TAG,"partial stream found"); | 127 dsq.put(abyte1); |
128 } | 128 |
129 | 129 if (abyte0.length > abyte1.length + i) |
130 if (j > size) { | 130 loadStream(abyte0, i + j + 2); |
131 saveStream = new byte[abyte0.length]; | 131 } |
132 System.arraycopy(abyte0, 0, saveStream, 0, abyte0.length); | 132 catch (Exception ex) { |
133 Log.d(TAG,"partial stream saved"); | 133 Log.w(TAG, "load stream error " + ex.getMessage()); |
134 } | 134 // ex.printStackTrace(); |
135 else { | 135 // dump(abyte0); |
136 byte abyte1[]; | 136 } |
137 try { | 137 } |
138 abyte1 = new byte[j + 2]; | 138 } |
139 | 139 |
140 System.arraycopy(abyte0, i, abyte1, 0, j + 2); | 140 public final byte[] readIncoming() |
141 dsq.put(abyte1); | 141 throws IOException { |
142 if(abyte0.length > abyte1.length + i) | 142 boolean done = false; |
143 loadStream(abyte0, i + j + 2); | 143 boolean negotiate = false; |
144 } | 144 baosin.reset(); |
145 catch (Exception ex) { | 145 int j = -1; |
146 | 146 int i = 0; |
147 Log.w(TAG,"load stream error " + ex.getMessage()); | 147 |
148 // ex.printStackTrace(); | 148 while (!done) { |
149 // dump(abyte0); | 149 i = bin.read(); |
150 | 150 |
151 } | 151 // WVL - LDC : 16/07/2003 : TR.000345 |
152 } | 152 // The inStream return -1 when end-of-stream is reached. This |
153 } | 153 // happens e.g. when the connection is closed from the AS/400. |
154 | 154 // So we stop in this case! |
155 public final byte[] readIncoming() | 155 // ==> an empty byte array is returned from this method. |
156 throws IOException { | 156 if (i == -1) { // nothing read! |
157 | 157 done = true; |
158 boolean done = false; | 158 vt.disconnect(); |
159 boolean negotiate = false; | 159 continue; |
160 | 160 } |
161 baosin.reset(); | 161 |
162 int j = -1; | 162 // We use the values instead of the static values IAC and EOR |
163 int i = 0; | 163 // because they are defined as bytes. |
164 | 164 // |
165 while(!done) { | 165 // The > if(i != 255 || j != 255) < is a hack for the double FF FF's |
166 i = bin.read(); | 166 // that are being returned. I do not know why this is like this and |
167 | 167 // can not find any documentation for it. It is also being returned |
168 // WVL - LDC : 16/07/2003 : TR.000345 | 168 // on my Client Access tcp dump as well so they are handling it. |
169 // The inStream return -1 when end-of-stream is reached. This | 169 // |
170 // happens e.g. when the connection is closed from the AS/400. | 170 // my5250 |
171 // So we stop in this case! | 171 // 0000: 00 50 DA 44 C8 45 42 00 00 00 00 24 08 00 45 00 .P.D.EB....$..E. |
172 // ==> an empty byte array is returned from this method. | 172 // 0010: 04 2A BC F9 00 00 40 06 D0 27 C1 A8 33 04 C1 A8 .*....@..'..3... |
173 if (i == -1) // nothing read! | 173 // 0020: 33 58 00 17 04 18 6F A2 83 CB 00 1E D1 BA 50 18 3X....o.......P. |
174 { | 174 // 0030: 20 00 8A 9A 00 00 03 FF FF 12 A0 00 00 04 00 00 ............... |
175 done = true; | 175 // --------------------------- || || ------------------------------------- |
176 vt.disconnect(); | 176 // 0040: 03 04 40 04 11 00 20 01 07 00 00 00 18 00 00 10 ..@... ......... |
177 continue; | 177 |
178 } | 178 if (j == 255 && i == 255) { |
179 | 179 j = -1; |
180 // We use the values instead of the static values IAC and EOR | 180 continue; |
181 // because they are defined as bytes. | 181 } |
182 // | 182 |
183 // The > if(i != 255 || j != 255) < is a hack for the double FF FF's | 183 baosin.write(i); |
184 // that are being returned. I do not know why this is like this and | 184 |
185 // can not find any documentation for it. It is also being returned | 185 // check for end of record EOR and IAC - FFEF |
186 // on my Client Access tcp dump as well so they are handling it. | 186 if (j == 255 && i == 239) |
187 // | 187 done = true; |
188 // my5250 | 188 |
189 // 0000: 00 50 DA 44 C8 45 42 00 00 00 00 24 08 00 45 00 .P.D.EB....$..E. | 189 // This is to check for the TELNET TIMING MARK OPTION |
190 // 0010: 04 2A BC F9 00 00 40 06 D0 27 C1 A8 33 04 C1 A8 .*....@..'..3... | 190 // rfc860 explains this in more detail. When we receive it |
191 // 0020: 33 58 00 17 04 18 6F A2 83 CB 00 1E D1 BA 50 18 3X....o.......P. | 191 // we will negotiate with the server by sending a WONT'T TIMING-MARK |
192 // 0030: 20 00 8A 9A 00 00 03 FF FF 12 A0 00 00 04 00 00 ............... | 192 // This will let the server know that we processed the information |
193 // --------------------------- || || ------------------------------------- | 193 // and are just waiting for the user to enter some data so keep the |
194 // 0040: 03 04 40 04 11 00 20 01 07 00 00 00 18 00 00 10 ..@... ......... | 194 // socket alive. This is more or less a AYT (ARE YOU THERE) or not. |
195 | 195 if (i == 253 && j == 255) { |
196 if(j == 255 && i == 255) { | 196 done = true; |
197 j = -1; | 197 negotiate = true; |
198 continue; | 198 } |
199 } | 199 |
200 baosin.write(i); | 200 j = i; |
201 // check for end of record EOR and IAC - FFEF | 201 } |
202 if(j == 255 && i == 239) | 202 |
203 done = true; | 203 // after the initial negotiation we might get other options such as |
204 | 204 // timing marks ?????????????? do we ???????????? look at telnet spec |
205 // This is to check for the TELNET TIMING MARK OPTION | 205 // yes we do. rfc860 explains about timing marks. |
206 // rfc860 explains this in more detail. When we receive it | 206 // WVL - LDC : 17/05/2004 : Device name negotiations send TIMING MARK |
207 // we will negotiate with the server by sending a WONT'T TIMING-MARK | 207 // to existing device! |
208 // This will let the server know that we processed the information | 208 // Handled incorrectly: we cannot continue processing the TIMING MARK DO |
209 // and are just waiting for the user to enter some data so keep the | 209 // after we have handled it in the vt.negotiate() |
210 // socket alive. This is more or less a AYT (ARE YOU THERE) or not. | 210 // We should not return the bytes; |
211 if(i == 253 && j == 255) { | 211 // ==> restructured to return null after negotiation! |
212 done = true; | 212 // Impacts the run method! Added the null check. |
213 negotiate = true; | 213 byte[] rBytes = baosin.toByteArray(); |
214 } | 214 |
215 j = i; | 215 if (dumpBytes) { |
216 } | 216 dump(rBytes); |
217 | 217 } |
218 // after the initial negotiation we might get other options such as | 218 |
219 // timing marks ?????????????? do we ???????????? look at telnet spec | 219 if (negotiate) { |
220 // yes we do. rfc860 explains about timing marks. | |
221 | |
222 // WVL - LDC : 17/05/2004 : Device name negotiations send TIMING MARK | |
223 // to existing device! | |
224 // Handled incorrectly: we cannot continue processing the TIMING MARK DO | |
225 // after we have handled it in the vt.negotiate() | |
226 // We should not return the bytes; | |
227 // ==> restructured to return null after negotiation! | |
228 // Impacts the run method! Added the null check. | |
229 byte[] rBytes = baosin.toByteArray(); | |
230 | |
231 if (dumpBytes) { | |
232 dump(rBytes); | |
233 } | |
234 | |
235 if (negotiate) { | |
236 // get the negotiation option | 220 // get the negotiation option |
237 baosin.write(bin.read()); | 221 baosin.write(bin.read()); |
238 vt.negotiate(rBytes); | 222 vt.negotiate(rBytes); |
239 | |
240 return null; | 223 return null; |
241 } | 224 } |
242 return rBytes; | 225 |
243 } | 226 return rBytes; |
244 | 227 } |
245 protected final void toggleDebug (ICodePage cp) { | 228 |
246 | 229 protected final void toggleDebug(ICodePage cp) { |
247 if (codePage == null) | 230 if (codePage == null) |
248 codePage = cp; | 231 codePage = cp; |
249 | 232 |
250 dumpBytes = !dumpBytes; | 233 dumpBytes = !dumpBytes; |
251 if (dumpBytes) { | 234 |
252 | 235 if (dumpBytes) { |
253 try { | 236 try { |
254 if (fw == null) { | 237 if (fw == null) { |
255 fw = new FileOutputStream("log.txt"); | 238 fw = new FileOutputStream("log.txt"); |
256 dw = new BufferedOutputStream(fw); | 239 dw = new BufferedOutputStream(fw); |
257 } | 240 } |
258 } | 241 } |
259 catch (FileNotFoundException fnfe) { | 242 catch (FileNotFoundException fnfe) { |
260 Log.w(TAG,fnfe.getMessage()); | 243 Log.w(TAG, fnfe.getMessage()); |
261 } | 244 } |
262 | 245 } |
263 } | 246 else { |
264 else { | 247 try { |
265 | 248 if (dw != null) |
266 try { | 249 dw.close(); |
267 | 250 |
268 if (dw != null) | 251 if (fw != null) |
269 dw.close(); | 252 fw.close(); |
270 if (fw != null) | 253 |
271 fw.close(); | 254 dw = null; |
272 dw = null; | 255 fw = null; |
273 fw = null; | 256 codePage = null; |
274 codePage = null; | 257 } |
275 } | 258 catch (IOException ioe) { |
276 catch(IOException ioe) { | 259 Log.w(TAG, ioe.getMessage()); |
277 | 260 } |
278 Log.w(TAG,ioe.getMessage()); | 261 } |
279 } | 262 |
280 } | 263 Log.i(TAG, "Data Stream output is now " + dumpBytes); |
281 | 264 } |
282 Log.i(TAG,"Data Stream output is now " + dumpBytes); | 265 |
283 } | 266 public void dump(byte[] abyte0) { |
284 | 267 try { |
285 public void dump (byte[] abyte0) { | 268 Log.i(TAG, "\n Buffer Dump of data from AS400: "); |
286 try { | 269 dw.write("\r\n Buffer Dump of data from AS400: ".getBytes()); |
287 | 270 StringBuffer h = new StringBuffer(); |
288 Log.i(TAG,"\n Buffer Dump of data from AS400: "); | 271 |
289 dw.write("\r\n Buffer Dump of data from AS400: ".getBytes()); | 272 for (int x = 0; x < abyte0.length; x++) { |
290 | 273 if (x % 16 == 0) { |
291 StringBuffer h = new StringBuffer(); | 274 System.out.println(" " + h.toString()); |
292 for (int x = 0; x < abyte0.length; x++) { | 275 dw.write((" " + h.toString() + "\r\n").getBytes()); |
293 if (x % 16 == 0) { | 276 h.setLength(0); |
294 System.out.println(" " + h.toString()); | 277 h.append("+0000"); |
295 dw.write((" " + h.toString() + "\r\n").getBytes()); | 278 h.setLength(5 - Integer.toHexString(x).length()); |
296 | 279 h.append(Integer.toHexString(x).toUpperCase()); |
297 h.setLength(0); | 280 System.out.print(h.toString()); |
298 h.append("+0000"); | 281 dw.write(h.toString().getBytes()); |
299 h.setLength(5 - Integer.toHexString(x).length()); | 282 h.setLength(0); |
300 h.append(Integer.toHexString(x).toUpperCase()); | 283 } |
301 | 284 |
302 System.out.print(h.toString()); | 285 char ac = codePage.ebcdic2uni(abyte0[x]); |
303 dw.write(h.toString().getBytes()); | 286 |
304 | 287 if (ac < ' ') |
305 h.setLength(0); | 288 h.append('.'); |
306 } | 289 else |
307 char ac = codePage.ebcdic2uni(abyte0[x]); | 290 h.append(ac); |
308 if (ac < ' ') | 291 |
309 h.append('.'); | 292 if (x % 4 == 0) { |
310 else | 293 System.out.print(" "); |
311 h.append(ac); | 294 dw.write((" ").getBytes()); |
312 if (x % 4 == 0) { | 295 } |
313 System.out.print(" "); | 296 |
314 dw.write((" ").getBytes()); | 297 if (Integer.toHexString(abyte0[x] & 0xff).length() == 1) { |
315 | 298 System.out.print("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase()); |
316 } | 299 dw.write(("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes()); |
317 | 300 } |
318 if (Integer.toHexString(abyte0[x] & 0xff).length() == 1){ | 301 else { |
319 System.out.print("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase()); | 302 System.out.print(Integer.toHexString(abyte0[x] & 0xff).toUpperCase()); |
320 dw.write(("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes()); | 303 dw.write((Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes()); |
321 | 304 } |
322 } | 305 } |
323 else { | 306 |
324 System.out.print(Integer.toHexString(abyte0[x] & 0xff).toUpperCase()); | 307 System.out.println(); |
325 dw.write((Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes()); | 308 dw.write("\r\n".getBytes()); |
326 } | 309 dw.flush(); |
327 | 310 } |
328 } | 311 catch (EOFException _ex) { } |
329 System.out.println(); | 312 catch (Exception _ex) { |
330 dw.write("\r\n".getBytes()); | 313 Log.w(TAG, "Cannot dump from host\n\r"); |
331 | 314 } |
332 dw.flush(); | 315 } |
333 } | |
334 catch(EOFException _ex) { } | |
335 catch(Exception _ex) { | |
336 Log.w(TAG,"Cannot dump from host\n\r"); | |
337 } | |
338 | |
339 } | |
340 | 316 |
341 // public void dumpBytes() { | 317 // public void dumpBytes() { |
342 // byte shit[] = bk.buffer; | 318 // byte shit[] = bk.buffer; |
343 // for (int i = 0;i < shit.length;i++) | 319 // for (int i = 0;i < shit.length;i++) |
344 // System.out.println(i + ">" + shit[i] + "< - ascii - >" + getASCIIChar(shit[i]) + "<"); | 320 // System.out.println(i + ">" + shit[i] + "< - ascii - >" + getASCIIChar(shit[i]) + "<"); |