use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class JSONReaderScanner method next.
public final char next() {
int index = ++bp;
if (index >= bufLength) {
if (bufLength == -1) {
return EOI;
}
if (sp > 0) {
int offset;
offset = bufLength - sp;
if (ch == '"' && offset > 0) {
offset--;
}
System.arraycopy(buf, offset, buf, 0, sp);
}
np = -1;
index = bp = sp;
try {
int startPos = bp;
int readLength = buf.length - startPos;
if (readLength == 0) {
char[] newBuf = new char[buf.length * 2];
System.arraycopy(buf, 0, newBuf, 0, buf.length);
buf = newBuf;
readLength = buf.length - startPos;
}
bufLength = reader.read(buf, bp, readLength);
} catch (IOException e) {
throw new JSONException(e.getMessage(), e);
}
if (bufLength == 0) {
throw new JSONException("illegal stat, textLength is zero");
}
if (bufLength == -1) {
return ch = EOI;
}
bufLength += bp;
}
return ch = buf[index];
}
use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class JSONScanner method scanFieldString.
public String scanFieldString(char[] fieldName) {
matchStat = UNKNOWN;
int startPos = this.bp;
char startChar = this.ch;
if (!charArrayCompare(text, bp, fieldName)) {
matchStat = NOT_MATCH_NAME;
return stringDefaultValue();
}
int index = bp + fieldName.length;
char ch = charAt(index++);
if (ch != '"') {
matchStat = NOT_MATCH;
return stringDefaultValue();
}
final String strVal;
{
int startIndex = index;
int endIndex = indexOf('"', startIndex);
if (endIndex == -1) {
throw new JSONException("unclosed str");
}
String stringVal = subString(startIndex, endIndex - startIndex);
if (stringVal.indexOf('\\') != -1) {
for (; ; ) {
int slashCount = 0;
for (int i = endIndex - 1; i >= 0; --i) {
if (charAt(i) == '\\') {
slashCount++;
} else {
break;
}
}
if (slashCount % 2 == 0) {
break;
}
endIndex = indexOf('"', endIndex + 1);
}
int chars_len = endIndex - (bp + fieldName.length + 1);
char[] chars = sub_chars(bp + fieldName.length + 1, chars_len);
stringVal = readString(chars, chars_len);
}
ch = charAt(endIndex + 1);
for (; ; ) {
if (ch == ',' || ch == '}') {
bp = endIndex + 1;
this.ch = ch;
strVal = stringVal;
break;
} else if (isWhitespace(ch)) {
endIndex++;
ch = charAt(endIndex + 1);
} else {
matchStat = NOT_MATCH;
return stringDefaultValue();
}
}
}
if (ch == ',') {
this.ch = charAt(++bp);
matchStat = VALUE;
return strVal;
} else {
//condition ch == '}' is always 'true'
ch = charAt(++bp);
if (ch == ',') {
token = JSONToken.COMMA;
this.ch = charAt(++bp);
} else if (ch == ']') {
token = JSONToken.RBRACKET;
this.ch = charAt(++bp);
} else if (ch == '}') {
token = JSONToken.RBRACE;
this.ch = charAt(++bp);
} else if (ch == EOI) {
token = JSONToken.EOF;
} else {
this.bp = startPos;
this.ch = startChar;
matchStat = NOT_MATCH;
return stringDefaultValue();
}
matchStat = END;
}
return strVal;
}
use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class IOUtils method decode.
public static void decode(CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) {
try {
CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true);
if (!cr.isUnderflow()) {
cr.throwException();
}
cr = charsetDecoder.flush(charByte);
if (!cr.isUnderflow()) {
cr.throwException();
}
} catch (CharacterCodingException x) {
// so this shouldn't happen
throw new JSONException("utf8 decode error, " + x.getMessage(), x);
}
}
use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class IOUtils method encodeUTF8.
public static int encodeUTF8(char[] sa, int sp, int len, byte[] da) {
int sl = sp + len;
int dp = 0;
int dlASCII = dp + Math.min(len, da.length);
// ASCII only optimized loop
while (dp < dlASCII && sa[sp] < '') {
da[dp++] = (byte) sa[sp++];
}
while (sp < sl) {
char c = sa[sp++];
if (c < 0x80) {
// Have at most seven bits
da[dp++] = (byte) c;
} else if (c < 0x800) {
// 2 bytes, 11 bits
da[dp++] = (byte) (0xc0 | (c >> 6));
da[dp++] = (byte) (0x80 | (c & 0x3f));
} else if (c >= '�' && c < ('�' + 1)) {
//Character.isSurrogate(c) but 1.7
final int uc;
int ip = sp - 1;
if (Character.isHighSurrogate(c)) {
if (sl - ip < 2) {
uc = -1;
} else {
char d = sa[ip + 1];
if (Character.isLowSurrogate(d)) {
uc = Character.toCodePoint(c, d);
} else {
throw new JSONException("encodeUTF8 error", new MalformedInputException(1));
}
}
} else {
if (Character.isLowSurrogate(c)) {
throw new JSONException("encodeUTF8 error", new MalformedInputException(1));
} else {
uc = c;
}
}
if (uc < 0) {
da[dp++] = (byte) '?';
} else {
da[dp++] = (byte) (0xf0 | ((uc >> 18)));
da[dp++] = (byte) (0x80 | ((uc >> 12) & 0x3f));
da[dp++] = (byte) (0x80 | ((uc >> 6) & 0x3f));
da[dp++] = (byte) (0x80 | (uc & 0x3f));
// 2 chars
sp++;
}
} else {
// 3 bytes, 16 bits
da[dp++] = (byte) (0xe0 | ((c >> 12)));
da[dp++] = (byte) (0x80 | ((c >> 6) & 0x3f));
da[dp++] = (byte) (0x80 | (c & 0x3f));
}
}
return dp;
}
use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class IOUtils method readAll.
/**
* @deprecated
*/
public static String readAll(Reader reader) {
StringBuilder buf = new StringBuilder();
try {
char[] chars = new char[2048];
for (; ; ) {
int len = reader.read(chars, 0, chars.length);
if (len < 0) {
break;
}
buf.append(chars, 0, len);
}
} catch (Exception ex) {
throw new JSONException("read string from reader error", ex);
}
return buf.toString();
}
Aggregations