Java Code Examples for com.alibaba.fastjson.util.IOUtils#stringSize()

The following examples show how to use com.alibaba.fastjson.util.IOUtils#stringSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SerializeWriter.java    From uavstack with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: SerializeWriter.java    From uavstack with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 3
Source File: SerializeWriter.java    From uavstack with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 4
Source File: SerializeWriter.java    From uavstack with Apache License 2.0 4 votes vote down vote up
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;
}