记录InputStream [英] Logging InputStream
本文介绍了记录InputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个InputStream类,该类扩展了CiphetInputStream.我想记录我InputStream中的所有数据(我将其进一步用作解析器中的输入),所以我完成了以下操作:
I create an InputStream class, that extends CiphetInputStream. I want to log all data from my InputStream (that i use as input in parser further) so i done following:
public class MyInputStream extends CipherInputStream {
private OutputStream logStream = new ByteArrayOutputStream();
.....
@Override
public int read() throws IOException {
int read = super.read();
logStream.write(read);
return read;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int read = super.read(b, off, len);
if (read > 0) {
logStream.write(b, off, read);
}
return read;
}
@Override
public int read(byte[] buffer) throws IOException {
int read = super.read(buffer);
if (read()>0) {
logStream.write(buffer);
}
return read;
}
@Override
public void close() throws IOException {
log();
super.close();
}
public void log() {
String logStr = new String(((ByteArrayOutputStream) logStream).toByteArray(), Charset.defaultCharset());
Log.d(getClass(), logStr);
try {
logStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
实际上,我的信息流具有以下内容:
In actual my stream has something like this:
<response>
<result>0</result>
</response>
但是日志显示类似这样的 mutation
but log show smth like this mutation :
<<response>
<resultt >0</resullt>
</respoonse>
[and (?) symbol at the end]
感谢您的帮助!
推荐答案
您可以组合 Logger.stream()
:
You can combine TeeInputStream
and Logger.stream()
:
new TeeInputStream(
yourStream,
Logger.stream(Level.INFO, this)
);
这篇关于记录InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文