}
public static final List<String> postgresArray2StringList(final String value, final int appendStringSize)
throws ArrayParserException {
if (!(value.startsWith("{") && value.endsWith("}"))) {
throw new ArrayParserException(String.format(
"postgresArray2StringList() ARRAY must begin with '{' and ends with '}': %s", value));
}
if (value.length() == 2) {
return Collections.emptyList();
}
// This is a simple copy-paste from the ROW processing code, and
// strictly speaking is not quite correct for PostgreSQL ARRAYs
final List<String> result = new ArrayList<String>();
final char[] c = value.toCharArray();
StringBuilder element = new StringBuilder(appendStringSize);
// this processor will fail if value has spaces between ',' and '"' or
// ')'
int i = 1;
while (c[i] != '}') {
if (c[i] == ',') {
final char nextChar = c[i + 1];
if (nextChar == ',' || nextChar == '}') {
throw new ArrayParserException("Empty array value at position " + i + " should be quoted: "
+ value);
}
i++;
} else if (c[i] == '\"') {
i++;
boolean insideQuote = true;
while (insideQuote) {
final char nextChar = c[i + 1];
if (c[i] == '\"') {
if (nextChar == ',' || nextChar == '}') {
result.add(element.toString());
element = new StringBuilder(appendStringSize);
insideQuote = false;
} else if (nextChar == '\"') {
i++;
element.append(c[i]);
} else {
throw new ArrayParserException("postgresArray2StringList() char after \" is not valid");
}
} else if (c[i] == '\\') {
if (nextChar == '\\' || nextChar == '\"') {
i++;
element.append(c[i]);
} else {
throw new ArrayParserException("postgresArray2StringList() char after \\ is not valid");
}
} else {
element.append(c[i]);
}