下面列出了com.alibaba.fastjson.util.IOUtils#UTF8 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @since 1.2.11
*/
@SuppressWarnings("unchecked")
public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, Feature... features) {
if (charset == null) {
charset = IOUtils.UTF8;
}
String strVal;
if (charset == IOUtils.UTF8) {
char[] chars = allocateChars(bytes.length);
int chars_len = IOUtils.decodeUTF8(bytes, offset, len, chars);
if (chars_len < 0) {
return null;
}
strVal = new String(chars, 0, chars_len);
} else {
if (len < 0) {
return null;
}
strVal = new String(bytes, offset, len, charset);
}
return (T) parseObject(strVal, clazz, features);
}
public int writeToEx(OutputStream out, Charset charset) throws IOException {
if (this.writer != null) {
throw new UnsupportedOperationException("writer not null");
}
if (charset == IOUtils.UTF8) {
return encodeToUTF8(out);
} else {
byte[] bytes = new String(buf, 0, count).getBytes(charset);
out.write(bytes);
return bytes.length;
}
}
public byte[] toBytes(Charset charset) {
if (this.writer != null) {
throw new UnsupportedOperationException("writer not null");
}
if (charset == IOUtils.UTF8) {
return encodeToUTF8Bytes();
} else {
return new String(buf, 0, count).getBytes(charset);
}
}
/**
* @since 1.2.11
*/
@SuppressWarnings("unchecked")
public static <T> T parseObject(InputStream is, //
Charset charset, //
Type type, //
Feature... features) throws IOException {
if (charset == null) {
charset = IOUtils.UTF8;
}
byte[] bytes = allocateBytes(1024 * 64);
int offset = 0;
for (;;) {
int readCount = is.read(bytes, offset, bytes.length - offset);
if (readCount == -1) {
break;
}
offset += readCount;
if (offset == bytes.length) {
byte[] newBytes = new byte[bytes.length * 3 / 2];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
bytes = newBytes;
}
}
return (T) parseObject(bytes, 0, offset, charset, type, features);
}