Mercurial > 510Connectbot
comparison src/org/tn5250j/framework/tn5250/DataStreamProducer.java @ 25:5949eb469a79 tn5250
adding tn5250 files, native android logging
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 03 Jun 2014 12:10:21 -0700 |
parents | e8d2a24e85c6 |
children | 294435151b0c |
comparison
equal
deleted
inserted
replaced
24:d1c549560aff | 25:5949eb469a79 |
---|---|
9 import java.io.IOException; | 9 import java.io.IOException; |
10 import java.net.SocketException; | 10 import java.net.SocketException; |
11 import java.util.concurrent.BlockingQueue; | 11 import java.util.concurrent.BlockingQueue; |
12 | 12 |
13 import org.tn5250j.encoding.ICodePage; | 13 import org.tn5250j.encoding.ICodePage; |
14 import org.tn5250j.tools.logging.TN5250jLogFactory; | 14 import android.util.Log; |
15 import org.tn5250j.tools.logging.TN5250jLogger; | 15 |
16 | 16 |
17 public class DataStreamProducer implements Runnable { | 17 public class DataStreamProducer implements Runnable { |
18 | 18 private static final String TAG = "DataStreamProducer"; |
19 private BufferedInputStream bin; | 19 private BufferedInputStream bin; |
20 private ByteArrayOutputStream baosin; | 20 private ByteArrayOutputStream baosin; |
21 private Thread me; | 21 private Thread me; |
22 private byte[] saveStream; | 22 private byte[] saveStream; |
23 private final BlockingQueue<Object> dsq; | 23 private final BlockingQueue<Object> dsq; |
26 private FileOutputStream fw; | 26 private FileOutputStream fw; |
27 private BufferedOutputStream dw; | 27 private BufferedOutputStream dw; |
28 private boolean dumpBytes = false; | 28 private boolean dumpBytes = false; |
29 private ICodePage codePage; | 29 private ICodePage codePage; |
30 | 30 |
31 private TN5250jLogger log = TN5250jLogFactory.getLogger (this.getClass()); | 31 |
32 | 32 |
33 public DataStreamProducer(tnvt vt, BufferedInputStream in, BlockingQueue<Object> queue, byte[] init) { | 33 public DataStreamProducer(tnvt vt, BufferedInputStream in, BlockingQueue<Object> queue, byte[] init) { |
34 bin = in; | 34 bin = in; |
35 this.vt = vt; | 35 this.vt = vt; |
36 baosin = new ByteArrayOutputStream(); | 36 baosin = new ByteArrayOutputStream(); |
50 | 50 |
51 me = Thread.currentThread(); | 51 me = Thread.currentThread(); |
52 | 52 |
53 // load the first response screen | 53 // load the first response screen |
54 loadStream(abyte2, 0); | 54 loadStream(abyte2, 0); |
55 | 55 |
56 while (!done) { | 56 while (!done) { |
57 try { | 57 try { |
58 | 58 |
59 byte[] abyte0 = readIncoming(); | 59 byte[] abyte0 = readIncoming(); |
60 | 60 |
82 } | 82 } |
83 | 83 |
84 } | 84 } |
85 | 85 |
86 catch (SocketException se) { | 86 catch (SocketException se) { |
87 log.warn(" DataStreamProducer thread interrupted and stopping " + se.getMessage()); | 87 Log.w(TAG," DataStreamProducer thread interrupted and stopping " + se.getMessage()); |
88 done = true; | 88 done = true; |
89 } | 89 } |
90 | 90 |
91 catch (IOException ioe) { | 91 catch (IOException ioe) { |
92 | 92 |
93 log.warn(ioe.getMessage()); | 93 Log.w(TAG,ioe.getMessage()); |
94 if (me.isInterrupted()) | 94 if (me.isInterrupted()) |
95 done = true; | 95 done = true; |
96 | 96 |
97 } | 97 } |
98 catch (Exception ex) { | 98 catch (Exception ex) { |
99 | 99 |
100 log.warn(ex.getMessage()); | 100 Log.w(TAG,ex.getMessage()); |
101 if (me.isInterrupted()) | 101 if (me.isInterrupted()) |
102 done = true; | 102 done = true; |
103 | 103 |
104 } | 104 } |
105 } | 105 } |
121 abyte0 = new byte[size]; | 121 abyte0 = new byte[size]; |
122 System.arraycopy(inter, 0, abyte0, 0, size); | 122 System.arraycopy(inter, 0, abyte0, 0, size); |
123 saveStream = null; | 123 saveStream = null; |
124 inter = null; | 124 inter = null; |
125 j = (abyte0[i] & 0xff) << 8 | abyte0[i + 1] & 0xff; | 125 j = (abyte0[i] & 0xff) << 8 | abyte0[i + 1] & 0xff; |
126 log.debug("partial stream found"); | 126 Log.d(TAG,"partial stream found"); |
127 } | 127 } |
128 | 128 |
129 if (j > size) { | 129 if (j > size) { |
130 saveStream = new byte[abyte0.length]; | 130 saveStream = new byte[abyte0.length]; |
131 System.arraycopy(abyte0, 0, saveStream, 0, abyte0.length); | 131 System.arraycopy(abyte0, 0, saveStream, 0, abyte0.length); |
132 log.debug("partial stream saved"); | 132 Log.d(TAG,"partial stream saved"); |
133 } | 133 } |
134 else { | 134 else { |
135 byte abyte1[]; | 135 byte abyte1[]; |
136 try { | 136 try { |
137 abyte1 = new byte[j + 2]; | 137 abyte1 = new byte[j + 2]; |
141 if(abyte0.length > abyte1.length + i) | 141 if(abyte0.length > abyte1.length + i) |
142 loadStream(abyte0, i + j + 2); | 142 loadStream(abyte0, i + j + 2); |
143 } | 143 } |
144 catch (Exception ex) { | 144 catch (Exception ex) { |
145 | 145 |
146 log.warn("load stream error " + ex.getMessage()); | 146 Log.w(TAG,"load stream error " + ex.getMessage()); |
147 // ex.printStackTrace(); | 147 // ex.printStackTrace(); |
148 // dump(abyte0); | 148 // dump(abyte0); |
149 | 149 |
150 } | 150 } |
151 } | 151 } |
158 boolean negotiate = false; | 158 boolean negotiate = false; |
159 | 159 |
160 baosin.reset(); | 160 baosin.reset(); |
161 int j = -1; | 161 int j = -1; |
162 int i = 0; | 162 int i = 0; |
163 | 163 |
164 while(!done) { | 164 while(!done) { |
165 | 165 |
166 i = bin.read(); | 166 i = bin.read(); |
167 | 167 |
168 // WVL - LDC : 16/07/2003 : TR.000345 | 168 // WVL - LDC : 16/07/2003 : TR.000345 |
169 // The inStream return -1 when end-of-stream is reached. This | 169 // The inStream return -1 when end-of-stream is reached. This |
170 // happens e.g. when the connection is closed from the AS/400. | 170 // happens e.g. when the connection is closed from the AS/400. |
255 fw = new FileOutputStream("log.txt"); | 255 fw = new FileOutputStream("log.txt"); |
256 dw = new BufferedOutputStream(fw); | 256 dw = new BufferedOutputStream(fw); |
257 } | 257 } |
258 } | 258 } |
259 catch (FileNotFoundException fnfe) { | 259 catch (FileNotFoundException fnfe) { |
260 log.warn(fnfe.getMessage()); | 260 Log.w(TAG,fnfe.getMessage()); |
261 } | 261 } |
262 | 262 |
263 } | 263 } |
264 else { | 264 else { |
265 | 265 |
273 fw = null; | 273 fw = null; |
274 codePage = null; | 274 codePage = null; |
275 } | 275 } |
276 catch(IOException ioe) { | 276 catch(IOException ioe) { |
277 | 277 |
278 log.warn(ioe.getMessage()); | 278 Log.w(TAG,ioe.getMessage()); |
279 } | 279 } |
280 } | 280 } |
281 | 281 |
282 log.info("Data Stream output is now " + dumpBytes); | 282 Log.i(TAG,"Data Stream output is now " + dumpBytes); |
283 } | 283 } |
284 | 284 |
285 public void dump (byte[] abyte0) { | 285 public void dump (byte[] abyte0) { |
286 try { | 286 try { |
287 | 287 |
288 log.info("\n Buffer Dump of data from AS400: "); | 288 Log.i(TAG,"\n Buffer Dump of data from AS400: "); |
289 dw.write("\r\n Buffer Dump of data from AS400: ".getBytes()); | 289 dw.write("\r\n Buffer Dump of data from AS400: ".getBytes()); |
290 | 290 |
291 StringBuffer h = new StringBuffer(); | 291 StringBuffer h = new StringBuffer(); |
292 for (int x = 0; x < abyte0.length; x++) { | 292 for (int x = 0; x < abyte0.length; x++) { |
293 if (x % 16 == 0) { | 293 if (x % 16 == 0) { |
331 | 331 |
332 dw.flush(); | 332 dw.flush(); |
333 } | 333 } |
334 catch(EOFException _ex) { } | 334 catch(EOFException _ex) { } |
335 catch(Exception _ex) { | 335 catch(Exception _ex) { |
336 log.warn("Cannot dump from host\n\r"); | 336 Log.w(TAG,"Cannot dump from host\n\r"); |
337 } | 337 } |
338 | 338 |
339 } | 339 } |
340 | 340 |
341 // public void dumpBytes() { | 341 // public void dumpBytes() { |