下面列出了com.alibaba.fastjson.util.IOUtils#stringSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void writeInt(int i) {
if (i == Integer.MIN_VALUE) {
write("-2147483648");
return;
}
int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i);
int newcount = count + size;
if (newcount > buf.length) {
if (writer == null) {
expandCapacity(newcount);
} else {
char[] chars = new char[size];
IOUtils.getChars(i, size, chars);
write(chars, 0, chars.length);
return;
}
}
IOUtils.getChars(i, newcount, buf);
count = newcount;
}
public void writeFieldValue(char seperator, String name, int value) {
if (value == Integer.MIN_VALUE || !quoteFieldNames) {
write(seperator);
writeFieldName(name);
writeInt(value);
return;
}
int intSize = (value < 0) ? IOUtils.stringSize(-value) + 1 : IOUtils.stringSize(value);
int nameLen = name.length();
int newcount = count + nameLen + 4 + intSize;
if (newcount > buf.length) {
if (writer != null) {
write(seperator);
writeFieldName(name);
writeInt(value);
return;
}
expandCapacity(newcount);
}
int start = count;
count = newcount;
buf[start] = seperator;
int nameEnd = start + nameLen + 1;
buf[start + 1] = keySeperator;
name.getChars(0, nameLen, buf, start + 2);
buf[nameEnd + 1] = keySeperator;
buf[nameEnd + 2] = ':';
IOUtils.getChars(value, count, buf);
}
public void writeFieldValue(char seperator, String name, long value) {
if (value == Long.MIN_VALUE || !quoteFieldNames) {
write(seperator);
writeFieldName(name);
writeLong(value);
return;
}
int intSize = (value < 0) ? IOUtils.stringSize(-value) + 1 : IOUtils.stringSize(value);
int nameLen = name.length();
int newcount = count + nameLen + 4 + intSize;
if (newcount > buf.length) {
if (writer != null) {
write(seperator);
writeFieldName(name);
writeLong(value);
return;
}
expandCapacity(newcount);
}
int start = count;
count = newcount;
buf[start] = seperator;
int nameEnd = start + nameLen + 1;
buf[start + 1] = keySeperator;
name.getChars(0, nameLen, buf, start + 2);
buf[nameEnd + 1] = keySeperator;
buf[nameEnd + 2] = ':';
IOUtils.getChars(value, count, buf);
}
public void writeLong(long i) {
boolean needQuotationMark = isEnabled(SerializerFeature.BrowserCompatible) //
&& (!isEnabled(SerializerFeature.WriteClassName)) //
&& (i > 9007199254740991L || i < -9007199254740991L);
if (i == Long.MIN_VALUE) {
if (needQuotationMark) write("\"-9223372036854775808\"");
else write("-9223372036854775808");
return;
}
int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i);
int newcount = count + size;
if (needQuotationMark) newcount += 2;
if (newcount > buf.length) {
if (writer == null) {
expandCapacity(newcount);
} else {
char[] chars = new char[size];
IOUtils.getChars(i, size, chars);
if (needQuotationMark) {
write('"');
write(chars, 0, chars.length);
write('"');
} else {
write(chars, 0, chars.length);
}
return;
}
}
if (needQuotationMark) {
buf[count] = '"';
IOUtils.getChars(i, newcount - 1, buf);
buf[newcount - 1] = '"';
} else {
IOUtils.getChars(i, newcount, buf);
}
count = newcount;
}