public static Object fromJSON(byte[] utf8) { // convert directly from bytes to chars // and parse directly from that instead of going through // intermediate strings or readers CharArr chars = new CharArr(); ByteUtils.UTF8toUTF16(utf8, 0, utf8.length, chars); JSONParser parser = new JSONParser(chars.getArray(), chars.getStart(), chars.length()); try { return ObjectBuilder.getVal(parser); } catch (IOException e) { throw new RuntimeException(e); // should never happen w/o using real IO } }
public void indent() { if (indent >= 0) { out.write('\n'); if (indent > 0) { int spaces = level*indent; out.reserve(spaces); for (int i=0; i<spaces; i++) { out.unsafeWrite(' '); } } } }
public void write(char b[], int off, int len) { reserve(len); unsafeWrite(b, off, len); }
/** Creates a JSON add command from a SolrInputDocument list. Doesn't currently handle boosts. */ public static String jsonAdd(SolrInputDocument... docs) { CharArr out = new CharArr(); try { out.append('['); boolean firstField = true; for (SolrInputDocument doc : docs) { if (firstField) firstField=false; else out.append(','); out.append(json(doc)); } out.append(']'); } catch (IOException e) { // should never happen } return out.toString(); }
public static void writeStringPart(String chars, int start, int end, CharArr out) { // TODO: write in chunks? int toWrite = end - start; char[] arr = out.getArray(); int pos = out.getEnd(); int space = arr.length - pos; if (space < toWrite) { writeStringPart((CharSequence)chars, start, end, out); return; } // get chars directly from String into output array chars.getChars(start, end, arr, pos); int endInOut = pos + toWrite; out.setEnd(endInOut); for (int i=pos; i<endInOut ;i++) { char ch = arr[i]; // When ch>=1f, (ch*146087937)&0xd6a01f80) is 0 only for characters that need escaping: " \\ u2028 u2029 // and has 7 false positives: 204a 4051 802f c022 c044 e04a e04b if (ch<=0x1f || ((ch*146087937)&0xd6a01f80)==0 ) { // We hit a char that needs escaping. do the rest char by char. out.setEnd(i); writeStringPart((CharSequence)chars, start+(i-pos), end, out); return; } } }
@Override public CharArr subSequence(int start, int end) { return new CharArr(buf, this.start+start, this.start+end); }
@Override public int length() { return size(); }
public Object getNumber() throws IOException { CharArr num = parser.getNumberChars(); String numstr = num.toString(); double d = Double.parseDouble(numstr); if (!Double.isInfinite(d)) return Double.valueOf(d); // TODO: use more efficient constructor in Java5 return new BigDecimal(num.buf, num.start, num.size()); }
@Override public void whitespaceNotification(int state, CharArr whitespace, boolean containsComment) { System.out.println("state=" + state + " comment=" + containsComment + " ws="+whitespace.toString()); } };
public String readStr(DataInputInputStream dis, StringCache stringCache) throws IOException { int sz = readSize(dis); if (bytes == null || bytes.length < sz) bytes = new byte[sz]; dis.readFully(bytes, 0, sz); if (stringCache != null) { return stringCache.get(bytesRef.reset(bytes, 0, sz)); } else { arr.reset(); ByteUtils.UTF8toUTF16(bytes, 0, sz, arr); return arr.toString(); } }
protected void handleNonDoubleQuoteString(int ch, boolean isName) throws IOException { if (ch == '\'') { stringTerm = ch; if ((flags & ALLOW_SINGLE_QUOTES) == 0) { throw err("Single quoted strings not allowed"); } } else { if (isName && (flags & ALLOW_UNQUOTED_KEYS) == 0 || !isName && (flags & ALLOW_UNQUOTED_STRING_VALUES) == 0 || eof) { if (isName) { throw err("Expected quoted string"); } else { throw err(null); } } if (!isUnquotedStringStart(ch)) { throw err(null); } stringTerm = 0; // signal for unquoted string out.reset(); out.unsafeWrite(ch); } }
} else { stringTerm = 0; out.reset(); out.write(arr, 0, i); if (!eof) { start--; out.reset(); out.unsafeWrite(arr, 0, arr.length); out.unsafeWrite(ch); return false;
public void write(String s, int stringOffset, int len) { reserve(len); s.getChars(stringOffset, len, buf, end); end += len; }
private CharArr readStringChars() throws IOException { if (stringTerm == 0) { // "out" will already contain the first part of the bare string, so don't reset it readStringBare(out); return out; } char terminator = (char) stringTerm; int i; for (i=start; i<end; i++) { char c = buf[i]; if (c == terminator) { tmp.set(buf,start,i); // directly use input buffer start=i+1; // advance past last '"' return tmp; } else if (c=='\\') { break; } } out.reset(); readStringChars2(out, i); return out; }