/** * Generic parsing routine that can parse out a Quoted-String, Literal or Atom and return the * parsed token as a String or a ByteArray. Errors or NIL data will return null. */ private Object parseString(boolean parseAtoms, boolean returnString) { byte b; // Skip leading spaces skipSpaces(); b = buffer[index]; if (b == '"') { // QuotedString index++; // skip the quote int start = index; int copyto = index; while ((b = buffer[index]) != '"') { if (b == '\\') // skip escaped byte index++; if (index != copyto) { // only copy if we need to // Beware: this is a destructive copy. I'm // pretty sure this is OK, but ... ;> buffer[copyto] = buffer[index]; } copyto++; index++; } index++; // skip past the terminating quote if (returnString) return ASCIIUtility.toString(buffer, start, copyto); else return new ByteArray(buffer, start, copyto - start); } else if (b == '{') { // Literal int start = ++index; // note the start position while (buffer[index] != '}') index++; int count = 0; try { count = ASCIIUtility.parseInt(buffer, start, index); } catch (NumberFormatException nex) { // throw new ParsingException(); return null; } start = index + 3; // skip "}\r\n" index = start + count; // position index to beyond the literal if (returnString) // return as String return ASCIIUtility.toString(buffer, start, start + count); else return new ByteArray(buffer, start, count); } else if (parseAtoms) { // parse as an ATOM int start = index; // track this, so that we can use to // creating ByteArrayInputStream below. String s = readAtom(); if (returnString) return s; else // *very* unlikely return new ByteArray(buffer, start, index); } else if (b == 'N' || b == 'n') { // the only valid value is 'NIL' index += 3; // skip past NIL return null; } return null; // Error }
/** * Extract an integer, starting at the current position. Updates the internal index to beyond the * number. Returns -1 if a number was not found. * * @return a number */ public int readNumber() { // Skip leading spaces skipSpaces(); int start = index; while (index < size && Character.isDigit((char) buffer[index])) index++; if (index > start) { try { return ASCIIUtility.parseInt(buffer, start, index); } catch (NumberFormatException nex) { } } return -1; }