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.
(boolean parseAtoms, boolean returnString)
| 443 | * or a ByteArray. Errors or NIL data will return null. |
| 444 | */ |
| 445 | private Object parseString(boolean parseAtoms, boolean returnString) { |
| 446 | byte b; |
| 447 | |
| 448 | // Skip leading spaces |
| 449 | skipSpaces(); |
| 450 | |
| 451 | b = buffer[index]; |
| 452 | if (b == '"') { // QuotedString |
| 453 | index++; // skip the quote |
| 454 | int start = index; |
| 455 | int copyto = index; |
| 456 | |
| 457 | while (index < size && (b = buffer[index]) != '"') { |
| 458 | if (b == '\\') // skip escaped byte |
| 459 | index++; |
| 460 | if (index != copyto) { // only copy if we need to |
| 461 | // Beware: this is a destructive copy. I'm |
| 462 | // pretty sure this is OK, but ... ;> |
| 463 | buffer[copyto] = buffer[index]; |
| 464 | } |
| 465 | copyto++; |
| 466 | index++; |
| 467 | } |
| 468 | if (index >= size) { |
| 469 | // didn't find terminating quote, something is seriously wrong |
| 470 | //throw new ArrayIndexOutOfBoundsException( |
| 471 | // "index = " + index + ", size = " + size); |
| 472 | return null; |
| 473 | } else |
| 474 | index++; // skip past the terminating quote |
| 475 | |
| 476 | if (returnString) |
| 477 | return toString(buffer, start, copyto); |
| 478 | else |
| 479 | return new ByteArray(buffer, start, copyto-start); |
| 480 | } else if (b == '{') { // Literal |
| 481 | int start = ++index; // note the start position |
| 482 | |
| 483 | while (buffer[index] != '}') |
| 484 | index++; |
| 485 | |
| 486 | int count = 0; |
| 487 | try { |
| 488 | count = ASCIIUtility.parseInt(buffer, start, index); |
| 489 | } catch (NumberFormatException nex) { |
| 490 | // throw new ParsingException(); |
| 491 | return null; |
| 492 | } |
| 493 | |
| 494 | start = index + 3; // skip "}\r\n" |
| 495 | index = start + count; // position index to beyond the literal |
| 496 | |
| 497 | if (returnString) // return as String |
| 498 | return toString(buffer, start, start + count); |
| 499 | else |
| 500 | return new ByteArray(buffer, start, count); |
| 501 | } else if (parseAtoms) { // parse as ASTRING-CHARs |
| 502 | int start = index; // track this, so that we can use to |
no test coverage detected