comparison app/src/main/java/org/tn5250j/framework/tn5250/DataStreamDumper.java @ 445:8fa8e73e2f5c

update to tn5250j version 0.7.7, svn r1270
author Carl Byington <carl@five-ten-sg.com>
date Mon, 04 Jan 2016 15:52:25 -0800
parents
children 5ce5235adde6
comparison
equal deleted inserted replaced
444:5a74f1b7c1db 445:8fa8e73e2f5c
1 /**
2 * Title: DataStreamDumper.java
3 * Copyright: Copyright (c) 2015
4 * Company:
5 *
6 * @author Martin W. Kirst
7 * <p/>
8 * Description:
9 * <p/>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 * <p/>
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * <p/>
20 * You should have received a copy of the GNU General Public License
21 * along with this software; see the file COPYING. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23 * Boston, MA 02111-1307 USA
24 */
25 package org.tn5250j.framework.tn5250;
26
27 import org.tn5250j.encoding.ICodePage;
28
29 import java.io.BufferedOutputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.util.concurrent.atomic.AtomicInteger;
34
35 import android.util.Log;
36
37 public class DataStreamDumper {
38 private static final String TAG = "DataStreamDumper";
39
40 private AtomicInteger counter = new AtomicInteger(0);
41
42 private FileOutputStream fw;
43 private BufferedOutputStream dw;
44 private boolean dumpActive = false;
45 private ICodePage codePage;
46
47 public void toggleDebug(ICodePage cp) {
48 if (codePage == null) codePage = cp;
49
50 dumpActive = !dumpActive;
51 if (dumpActive) {
52 try {
53 if (fw == null) {
54 fw = new FileOutputStream("log.txt");
55 dw = new BufferedOutputStream(fw);
56 }
57 } catch (FileNotFoundException fnfe) {
58 Log.w(TAG, fnfe.getMessage());
59 }
60
61 } else {
62 try {
63 if (dw != null) dw.close();
64 if (fw != null) fw.close();
65 dw = null;
66 fw = null;
67 codePage = null;
68 } catch (IOException ioe) {
69 Log.w(TAG, ioe.getMessage());
70 }
71 }
72
73 Log.i(TAG, "Data Stream output is now " + dumpActive);
74 }
75
76 public void dump(byte[] abyte0) {
77 if (!dumpActive) return;
78
79 try {
80 Log.i(TAG, "\n Buffer Dump of data from AS400: ");
81 dw.write("\r\n Buffer Dump of data from AS400: ".getBytes());
82 StringBuilder h = new StringBuilder();
83
84 for (int x = 0; x < abyte0.length; x++) {
85 if (x % 16 == 0) {
86 System.out.println(" " + h.toString());
87 dw.write((" " + h.toString() + "\r\n").getBytes());
88 h.setLength(0);
89 h.append("+0000");
90 h.setLength(5 - Integer.toHexString(x).length());
91 h.append(Integer.toHexString(x).toUpperCase());
92 System.out.print(h.toString());
93 dw.write(h.toString().getBytes());
94 h.setLength(0);
95 }
96
97 char ac = codePage.ebcdic2uni(abyte0[x]);
98
99 if (ac < ' ')
100 h.append('.');
101 else
102 h.append(ac);
103
104 if (x % 4 == 0) {
105 System.out.print(" ");
106 dw.write((" ").getBytes());
107 }
108
109 if (Integer.toHexString(abyte0[x] & 0xff).length() == 1) {
110 System.out.print("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase());
111 dw.write(("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes());
112 }
113 else {
114 System.out.print(Integer.toHexString(abyte0[x] & 0xff).toUpperCase());
115 dw.write((Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes());
116 }
117 }
118
119 System.out.println();
120 dw.write("\r\n".getBytes());
121 dw.flush();
122 }
123 catch (EOFException _ex) { }
124 catch (Exception _ex) {
125 Log.w(TAG, "Cannot dump from host\n\r");
126 }
127 }
128
129 void dumpRaw(byte[] buffer) {
130 try {
131 String fname = "dump_" + counter.get() + ".data";
132 Log.d(TAG, "Dumping file: " + fname);
133 FileOutputStream fos = new FileOutputStream(fname);
134 fos.write(buffer);
135 fos.close();
136 }
137 catch (FileNotFoundException e) {
138 e.printStackTrace();
139 }
140 catch (IOException e) {
141 e.printStackTrace();
142 }
143 }
144 }