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
26
27 package org.metricshub.wbem.sblim.cimclient.internal.http.io;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 import java.io.FilterInputStream;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.io.OutputStream;
53 import java.util.logging.Level;
54 import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
55 import org.metricshub.wbem.sblim.cimclient.internal.logging.TimeStamp;
56
57
58
59
60
61 public class DebugInputStream extends FilterInputStream {
62 private byte[] iBuf;
63
64 private boolean iBuffered;
65
66 private int iCur = 0;
67
68 private int iMaxLen = 0;
69
70 private OutputStream iStream;
71
72 private String iOrigin;
73
74
75
76
77
78
79
80 public DebugInputStream(InputStream is, OutputStream os) {
81 this(is, os, null);
82 }
83
84
85
86
87
88
89
90
91 public DebugInputStream(InputStream is, OutputStream os, String pOrigin) {
92 super(is);
93 this.iBuf = new byte[512];
94 this.iBuffered = false;
95 this.iStream = os;
96 this.iOrigin = pOrigin == null ? "unknown" : pOrigin;
97 }
98
99 private void buffer() throws IOException {
100 this.iBuffered = true;
101 int total;
102 try {
103 while ((total = this.in.read(this.iBuf, this.iMaxLen, this.iBuf.length - this.iMaxLen)) > -1) {
104 this.iMaxLen += total;
105 if (this.iMaxLen == this.iBuf.length) {
106 byte b[] = new byte[this.iBuf.length << 1];
107 System.arraycopy(this.iBuf, 0, b, 0, this.iBuf.length);
108 this.iBuf = b;
109 }
110 }
111 } catch (TrailerException e) {
112
113 writeBuffer(this.iOrigin + " begin (TrailerException occurred)");
114 throw e;
115 }
116 writeBuffer(this.iOrigin + " begin");
117 }
118
119 private void writeBuffer(String header) throws IOException {
120 StringBuilder outStr = new StringBuilder("<--- ");
121 outStr.append(header);
122 outStr.append(' ');
123 outStr.append(TimeStamp.formatWithMillis(System.currentTimeMillis()));
124 outStr.append(" ----\n");
125 outStr.append(new String(this.iBuf, 0, this.iMaxLen));
126 if (this.iMaxLen > 0 && this.iBuf[this.iMaxLen - 1] != '\n') outStr.append('\n');
127 outStr.append("---- ");
128 outStr.append(this.iOrigin);
129 outStr.append(" end ----->\n");
130 if (this.iStream != null) this.iStream.write(outStr.toString().getBytes());
131 if (LogAndTraceBroker.getBroker().isLoggableCIMXMLTrace(Level.FINEST)) LogAndTraceBroker
132 .getBroker()
133 .traceCIMXML(Level.FINEST, outStr.toString(), false);
134 }
135
136 @Override
137 public synchronized int read() throws IOException {
138 if (!this.iBuffered) buffer();
139
140 if (this.iCur >= this.iMaxLen) return -1;
141 return this.iBuf[this.iCur++];
142 }
143
144 @Override
145 public synchronized int read(byte b[], int off, int len) throws IOException {
146 if (b == null) {
147 throw new NullPointerException();
148 } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
149 throw new IndexOutOfBoundsException();
150 } else if (len == 0) {
151 return 0;
152 }
153
154 int c = read();
155 if (c == -1) {
156 return -1;
157 }
158 b[off] = (byte) c;
159
160 int i = 1;
161 for (; i < len; i++) {
162 c = read();
163 if (c == -1) {
164 break;
165 }
166 if (b != null) {
167 b[off + i] = (byte) c;
168 }
169 }
170 return i;
171 }
172 }