2013-05-08 90 views
1

我創建了一個InputStream類,它擴展了CiphetInputStream。我想從我的InputStream中所有的數據記錄(我在解析器輸入使用進一步的),所以我做了以下內容:Logging 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] 

感謝您的幫助!

+0

也許這種解決方案可能是有人幫助:http://shomeser.blogspot.com/2013/12/redirect-stream-to-logcat.html – 2013-12-24 12:35:22

回答

0

如果你想看到登錄logcat,請嘗試Log.i(String tag, String message);System.out.println("");。他們兩個都有效。您也可以使用Log.d,Log.wLog.e

+0

我的Utils.log包裝Log.d,當然我使用它。 – ADK 2013-05-08 07:46:30