首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测井InputStream

测井InputStream
EN

Stack Overflow用户
提问于 2013-05-08 07:09:38
回答 2查看 4.3K关注 0票数 2

我创建了一个InputStream类,它扩展了CiphetInputStream。我希望记录我的InputStream中的所有数据(进一步用作解析器中的输入),因此我做了以下工作:

代码语言:javascript
复制
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();
        }
    }
}

实际上,我的溪流中有这样的东西:

代码语言:javascript
复制
<response>
<result>0</result>
</response>

但日志显示的气味就像这种突变:

代码语言:javascript
复制
<<response>
<resultt >0</resullt>
</respoonse>
[and (?) symbol at the end]

谢谢你的帮助!

EN

回答 2

Stack Overflow用户

发布于 2016-01-22 20:24:58

您可以将TeeInputStreamLogger.stream()结合起来

代码语言:javascript
复制
new TeeInputStream(
  yourStream, 
  Logger.stream(Level.INFO, this)
);
票数 3
EN

Stack Overflow用户

发布于 2013-05-08 07:40:22

如果要查看登录日志,请尝试Log.i(String tag, String message);System.out.println("");。这两种方法都有效。您也可以使用,Log.dLog.wLog.e

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16434699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档