Java 记录输入流
我创建了一个InputStream类,它扩展了CiphetInputStream。我想记录InputStream中的所有数据(我在解析器中进一步将其用作输入),因此我完成了以下工作:Java 记录输入流,java,android,logging,inputstream,Java,Android,Logging,Inputstream,我创建了一个InputStream类,它扩展了CiphetInputStream。我想记录InputStream中的所有数据(我在解析器中进一步将其用作输入),因此我完成了以下工作: public class MyInputStream extends CipherInputStream { private OutputStream logStream = new ByteArrayOutputStream(); ..... @Override public int re
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();
}
}
}
实际上,我的流有如下内容:
<response>
<result>0</result>
</response>
0
但是日志显示smth像这样的突变:
如果要在logcat中查看日志,请尝试log.i(字符串标记,字符串消息)代码>或系统输出打印项数(“”)代码>。这两种方法都有效。您还可以使用、Log.d
、Log.w
和Log.e
。您可以组合和:
也许这个解决方案对某人有帮助:
<<response>
<resultt >0</resullt>
</respoonse>
[and (?) symbol at the end]
new TeeInputStream(
yourStream,
Logger.stream(Level.INFO, this)
);