Java 记录输入流

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

我创建了一个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>

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)
);