byte转string再转回为有损过程
private class HostRemoveResponseModifier implements Modifier {
protected String getHost() {
return url;
}
@Override
public byte[] mod(byte[] origin) {
String strString = new String(origin);
String target = getHost() + ":" + portRemote;
boolean textContentType = false;
if(-1!=strString.indexOf(target)) {
strString = strString.replace(target, "");
textContentType = true;
}
target = getHost();
if(-1!=strString.indexOf(target)) {
strString = strString.replace(target, "");
textContentType = true;
}
/**
* string来回转是有损的,字符串替换本身有一定风险
* 所以如果不用替换就返回origin,对于非text类型,必须返回原来二进制
*/
if(textContentType)
return strString.getBytes();
else {
return origin;
}
}
}
private static byte[] encodeUTF8(byte coder, byte[] val, boolean doReplace) {
if (coder == UTF16)
return encodeUTF8_UTF16(val, doReplace);
if (!hasNegatives(val, 0, val.length))
return Arrays.copyOf(val, val.length);
int dp = 0;
byte[] dst = new byte[val.length << 1];
for (int sp = 0; sp < val.length; sp++) {
byte c = val[sp];
if (c < 0) {
dst[dp++] = (byte)(0xc0 | ((c & 0xff) >> 6));
dst[dp++] = (byte)(0x80 | (c & 0x3f));
} else {
dst[dp++] = c;
}
}
if (dp == dst.length)
return dst;
return Arrays.copyOf(dst, dp);
}