我創建了一個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]
感謝您的幫助!
也許這種解決方案可能是有人幫助:http://shomeser.blogspot.com/2013/12/redirect-stream-to-logcat.html – 2013-12-24 12:35:22