445
|
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;
|
446
|
33 import java.io.EOFException;
|
445
|
34 import java.util.concurrent.atomic.AtomicInteger;
|
|
35
|
|
36 import android.util.Log;
|
|
37
|
|
38 public class DataStreamDumper {
|
|
39 private static final String TAG = "DataStreamDumper";
|
|
40
|
|
41 private AtomicInteger counter = new AtomicInteger(0);
|
|
42
|
|
43 private FileOutputStream fw;
|
|
44 private BufferedOutputStream dw;
|
|
45 private boolean dumpActive = false;
|
|
46 private ICodePage codePage;
|
|
47
|
|
48 public void toggleDebug(ICodePage cp) {
|
|
49 if (codePage == null) codePage = cp;
|
|
50
|
|
51 dumpActive = !dumpActive;
|
|
52 if (dumpActive) {
|
|
53 try {
|
|
54 if (fw == null) {
|
|
55 fw = new FileOutputStream("log.txt");
|
|
56 dw = new BufferedOutputStream(fw);
|
|
57 }
|
|
58 } catch (FileNotFoundException fnfe) {
|
|
59 Log.w(TAG, fnfe.getMessage());
|
|
60 }
|
|
61
|
|
62 } else {
|
|
63 try {
|
|
64 if (dw != null) dw.close();
|
|
65 if (fw != null) fw.close();
|
|
66 dw = null;
|
|
67 fw = null;
|
|
68 codePage = null;
|
|
69 } catch (IOException ioe) {
|
|
70 Log.w(TAG, ioe.getMessage());
|
|
71 }
|
|
72 }
|
|
73
|
|
74 Log.i(TAG, "Data Stream output is now " + dumpActive);
|
|
75 }
|
|
76
|
|
77 public void dump(byte[] abyte0) {
|
|
78 if (!dumpActive) return;
|
|
79
|
|
80 try {
|
|
81 Log.i(TAG, "\n Buffer Dump of data from AS400: ");
|
|
82 dw.write("\r\n Buffer Dump of data from AS400: ".getBytes());
|
|
83 StringBuilder h = new StringBuilder();
|
|
84
|
|
85 for (int x = 0; x < abyte0.length; x++) {
|
|
86 if (x % 16 == 0) {
|
|
87 System.out.println(" " + h.toString());
|
|
88 dw.write((" " + h.toString() + "\r\n").getBytes());
|
|
89 h.setLength(0);
|
|
90 h.append("+0000");
|
|
91 h.setLength(5 - Integer.toHexString(x).length());
|
|
92 h.append(Integer.toHexString(x).toUpperCase());
|
|
93 System.out.print(h.toString());
|
|
94 dw.write(h.toString().getBytes());
|
|
95 h.setLength(0);
|
|
96 }
|
|
97
|
|
98 char ac = codePage.ebcdic2uni(abyte0[x]);
|
|
99
|
|
100 if (ac < ' ')
|
|
101 h.append('.');
|
|
102 else
|
|
103 h.append(ac);
|
|
104
|
|
105 if (x % 4 == 0) {
|
|
106 System.out.print(" ");
|
|
107 dw.write((" ").getBytes());
|
|
108 }
|
|
109
|
|
110 if (Integer.toHexString(abyte0[x] & 0xff).length() == 1) {
|
|
111 System.out.print("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase());
|
|
112 dw.write(("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes());
|
|
113 }
|
|
114 else {
|
|
115 System.out.print(Integer.toHexString(abyte0[x] & 0xff).toUpperCase());
|
|
116 dw.write((Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes());
|
|
117 }
|
|
118 }
|
|
119
|
|
120 System.out.println();
|
|
121 dw.write("\r\n".getBytes());
|
|
122 dw.flush();
|
|
123 }
|
|
124 catch (EOFException _ex) { }
|
|
125 catch (Exception _ex) {
|
|
126 Log.w(TAG, "Cannot dump from host\n\r");
|
|
127 }
|
|
128 }
|
|
129
|
|
130 void dumpRaw(byte[] buffer) {
|
|
131 try {
|
|
132 String fname = "dump_" + counter.get() + ".data";
|
|
133 Log.d(TAG, "Dumping file: " + fname);
|
|
134 FileOutputStream fos = new FileOutputStream(fname);
|
|
135 fos.write(buffer);
|
|
136 fos.close();
|
|
137 }
|
|
138 catch (FileNotFoundException e) {
|
|
139 e.printStackTrace();
|
|
140 }
|
|
141 catch (IOException e) {
|
|
142 e.printStackTrace();
|
|
143 }
|
|
144 }
|
|
145 }
|