protected void prepare0(final String stream) {
final int comma = stream.indexOf(',');
final String countString = stream.substring(0, comma);
int count = Integer.parseInt(countString);
if (count < 0) {
throw new SerializationException("String table count is invalid, count: " + count);
}
// extract strings and add them to the string table...
final Map<Integer, String> strings = this.createStrings();
int j = comma + 1;
for (int i = 0; i < count; i++) {
final StringBuffer buf = new StringBuffer();
// consume leading double quote...
final char first = stream.charAt(j);
j++;
Checker.equals("must be start of quoted string", '\"', first);
while (true) {
// consume a char within the quoted string...
final char c = stream.charAt(j);
j++;
// check if closing quote...
if (c == '"') {
break;
}
// not a backslash add literally...
if (c != '\'') {
buf.append(c);
continue;
}
// handle escaped char...
final char d = stream.charAt(j);
j++;
if (d == '\0') {
buf.append('\0');
continue;
}
if (d == '\t') {
buf.append('\t');
continue;
}
if (d == '\n') {
buf.append('\n');
continue;
}
if (d == '\r') {
buf.append('\r');
continue;
}
if (d == '\\') {
buf.append('\\');
continue;
}
if (d == '"') {
buf.append('"');
continue;
}
if (d == 'u') {
final String codeString = stream.substring(j, j + 4);
final int code = Integer.parseInt(codeString);
buf.append((char) code);
// consume trailing semi colon
final char semiColon = stream.charAt(j);
if (semiColon != ';') {
throw new SerializationException("Stream is corrupted, expected semiColon got \"" + semiColon + "\".");
}
j++;
continue;
}
throw new SerializationException("Unknown escaped char '" + d + "'.");
}
// add string to table...
final Integer reference = new Integer(i + Constants.STRING_BIAS);
strings.put(reference, buf.toString());