我创建了一个InputStream类,它扩展了CiphetInputStream。我希望记录我的InputStream中的所有数据(进一步用作解析器中的输入),因此我做了以下工作:
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>但日志显示的气味就像这种突变:
<<response>
<resultt >0</resullt>
</respoonse>
[and (?) symbol at the end]谢谢你的帮助!
发布于 2016-01-22 20:24:58
您可以将TeeInputStream和Logger.stream()结合起来
new TeeInputStream(
yourStream,
Logger.stream(Level.INFO, this)
);发布于 2013-05-08 07:40:22
如果要查看登录日志,请尝试Log.i(String tag, String message);或System.out.println("");。这两种方法都有效。您也可以使用,Log.d,Log.w和Log.e。
https://stackoverflow.com/questions/16434699
复制相似问题