CiphetInputStream を拡張する InputStream クラスを作成します。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>
しかし、この突然変異のようなログショーのsmth :
<<response>
<resultt >0</resullt>
</respoonse>
[and (?) symbol at the end]
助けてくれてありがとう!