2

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

谢谢你的帮助!

4

2 回答 2

3

您可以结合TeeInputStreamLogger.stream()

new TeeInputStream(
  yourStream, 
  Logger.stream(Level.INFO, this)
);
于 2016-01-22T20:24:58.097 回答
-1

如果您想在 logcat 中查看登录信息,请尝试Log.i(String tag, String message);System.out.println("");. 它们都有效。您也可以使用, Log.d, Log.wand Log.ealso。

于 2013-05-08T07:40:22.763 回答