我创建了一个 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]
谢谢你的帮助!