我从InputStream中得到一个XML字符串,但是这个字符串中存在非法字符(XML认为Unicode的0x1a、0x5、0xffff等是非法字符),现在我希望将这些字符过滤掉,将它们替换成空字符(对应的int值为78),代码如下:
private static ByteArrayInputStream inputStreamFilter(InputStream inputStream) throws IOException {
List byteList = new ArrayList();
int byteInt = 0;
while ((byteInt = inputStream.read()) != -1) {
if (byteInt != 26 && byteInt != 5 && byteInt != 65535)
byteList.add(new Byte((byte)byteInt));
else byteList.add(new Byte((byte)78));
byte[] bytes = new byte[byteList.size()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = ((Byte)byteList.get(i)).byteValue();
}
return new ByteArrayInputStream(bytes);
}
这个程序很奇怪的一点就是:过滤0x1a(26)和0x5(5)的时候都是没问题,但是似乎对于0xffff(65535)不起作用,请问高手们这是为什么?应该如何处理这种情况?谢谢!!
private static ByteArrayInputStream inputStreamFilter(InputStream inputStream) throws IOException {
List byteList = new ArrayList();
int byteInt = 0;
while ((byteInt = inputStream.read()) != -1) {
if (byteInt != 26 && byteInt != 5 && byteInt != 65535)
byteList.add(new Byte((byte)byteInt));
else byteList.add(new Byte((byte)78));
byte[] bytes = new byte[byteList.size()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = ((Byte)byteList.get(i)).byteValue();
}
return new ByteArrayInputStream(bytes);
}
这个程序很奇怪的一点就是:过滤0x1a(26)和0x5(5)的时候都是没问题,但是似乎对于0xffff(65535)不起作用,请问高手们这是为什么?应该如何处理这种情况?谢谢!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货