下面列出了java.io.StringReader#mark ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static int skipLws(StringReader input, boolean withReset) throws IOException {
if (withReset) {
input.mark(1);
}
int c = input.read();
while (c == 32 || c == 9 || c == 10 || c == 13) {
if (withReset) {
input.mark(1);
}
c = input.read();
}
if (withReset) {
input.reset();
}
return c;
}
private static int skipLws(StringReader input, boolean withReset)
throws IOException {
if (withReset) {
input.mark(1);
}
int c = input.read();
while (c == 32 || c == 9 || c == 10 || c == 13) {
if (withReset) {
input.mark(1);
}
c = input.read();
}
if (withReset) {
input.reset();
}
return c;
}
/**
* Parse a regular identifier (unquoted) returning returning either
* the value of the identifier or a delimited identifier. Ensures
* that all characters in the identifer are valid for a regular identifier.
*
* @param r Regular identifier to parse.
* @param normalize If true return the identifer converted to a single case, otherwise return the identifier as entered.
* @return the value of the identifer or a delimited identifier
* @throws IOException Error accessing value
* @throws StandardException Error parsing identifier.
*/
private static String parseUnQId(StringReader r, boolean normalize)
throws IOException,StandardException
{
StringBuilder b = new StringBuilder();
int c;
boolean first;
//
for(first = true; ; first=false)
{
r.mark(0);
if (idChar(first,c=r.read()))
b.append((char)c);
else
break;
}
if (c != -1) r.reset();
String id = b.toString();
if (normalize)
return StringUtil.SQLToUpperCase(id);
else
return id;
}
private Object parse(StringReader is) throws IOException {
is.mark(0);
int readChar = is.read();
switch (readChar) {
case 'i':
return parseInteger(is);
case 'l':
return parseList(is);
case 'd':
return parseDictionary(is);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
is.reset();
return parseByteString(is);
default:
throw new IOException("Problem parsing bencoded file");
}
}
/**
* Parse a regular identifier (unquoted) returning returning either
* the value of the identifier or a delimited identifier. Ensures
* that all characters in the identifer are valid for a regular identifier.
*
* @param r Regular identifier to parse.
* @param normalize If true return the identifer converted to a single case, otherwise return the identifier as entered.
* @return the value of the identifer or a delimited identifier
* @throws IOException Error accessing value
* @throws StandardException Error parsing identifier.
*/
private static String parseUnQId(StringReader r, boolean normalize)
throws IOException,StandardException
{
StringBuilder b = new StringBuilder();
int c;
boolean first;
//
for(first = true; ; first=false)
{
r.mark(0);
if (idChar(first,c=r.read()))
b.append((char)c);
else
break;
}
if (c != -1) r.reset();
String id = b.toString();
if (normalize)
return StringUtil.SQLToUpperCase(id);
else
return id;
}
private static int skipLws(StringReader input, boolean withReset)
throws IOException {
if (withReset) {
input.mark(1);
}
int c = input.read();
while (c == 32 || c == 9 || c == 10 || c == 13) {
if (withReset) {
input.mark(1);
}
c = input.read();
}
if (withReset) {
input.reset();
}
return c;
}
private SortedMap parseDictionary(StringReader is) throws IOException {
SortedMap<ByteBuffer, Object> map = new TreeMap<>(new DictionaryComparator());
is.mark(0);
int readChar = is.read();
while (readChar != 'e') {
if (readChar < 0) {
throw new IOException("Unexpected EOF found");
}
is.reset();
map.put(parseByteString(is), parse(is));
is.mark(0);
readChar = is.read();
}
return map;
}
private static String nextKey(StringReader reader) throws IOException {
reader.mark(1);
JsonString val = parseString(reader);
if (val == null) {
reader.reset();
}
// find delimiter
int intch;
char ch = ' ';
while ((intch = reader.read()) != -1) {
ch = (char) intch;
if (ch == StructuralToken.COLON.CHAR) {
// key/value delimiter found
break;
} else if (ch == StructuralToken.RCRBRACKET.CHAR) {
// end of document
break;
} else if (!whitespaceChars.contains(ch)) {
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.1", new Character[] { ch }));
}
}
if (ch != StructuralToken.COLON.CHAR && val != null && val.getString().length() > 0) {
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.4", new String[] { val.getString() }));
}
return val != null ? val.getString() : null;
}
/**
Parse a list of comma separated SQL identifiers returning
them a as elements in an array.
@param normalize true means return ids in nomral form, false means
return them as they were entered.
@exception StandardException Oops
*/
private static String[] parseIdList(StringReader r, boolean normalize)
throws StandardException
{
Vector v = new Vector();
while (true)
{
int delim;
try {
String thisId = IdUtil.parseId(r,normalize);
v.add(thisId);
r.mark(0);
delim = r.read();
if (delim != ',')
{
if (delim!=-1) r.reset();
break;
}
}
catch (StandardException se){
if (se.getMessageId().equals(SQLState.ID_LIST_PARSE_ERROR))
throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR,se);
else
throw se;
}
catch (IOException ioe){
throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR,ioe);
}
}
if (v.isEmpty()) return null;
String[] result = new String[v.size()];
v.copyInto(result);
return result;
}
/**
@param r The multi-part identifier to be parsed
@return An array of strings made by breaking the input string at its dots, '.'.
@exception StandardException Oops
*/
private static String[] parseMultiPartSQLIdentifier(StringReader r)
throws StandardException
{
Vector v = new Vector();
while (true)
{
String thisId = parseId(r,true);
v.addElement(thisId);
int dot;
try {
r.mark(0);
dot = r.read();
if (dot != '.')
{
if (dot!=-1) r.reset();
break;
}
}
catch (IOException ioe){
throw StandardException.newException(SQLState.ID_PARSE_ERROR,ioe);
}
}
String[] result = new String[v.size()];
v.copyInto(result);
return result;
}
/**
* Parse a delimited (quoted) identifier returning either
* the value of the identifier or a delimited identifier.
* @param r Quoted identifier to parse.
* @param normalize If true return a delimited identifer, otherwise return the identifier's value.
* @return the value of the identifer or a delimited identifier
* @throws IOException Error accessing value
* @throws StandardException Error parsing identifier.
*/
private static String parseQId(StringReader r,boolean normalize)
throws IOException,StandardException
{
StringBuilder b = new StringBuilder();
int c = r.read();
if (c != '"') throw StandardException.newException(SQLState.ID_PARSE_ERROR);
while (true)
{
c=r.read();
if (c == '"')
{
r.mark(0);
int c2 = r.read();
if (c2 != '"')
{
if (c2!=-1)r.reset();
break;
}
}
else if (c == -1)
throw StandardException.newException(SQLState.ID_PARSE_ERROR);
b.append((char)c);
}
if (b.length() == 0) //id can't be 0-length
throw StandardException.newException(SQLState.ID_PARSE_ERROR);
if (normalize)
return b.toString();
else
return normalToDelimited(b.toString()); //Put the quotes back.
}
/**
Parse a list of comma separated SQL identifiers returning
them a as elements in an array.
@param normalize true means return ids in nomral form, false means
return them as they were entered.
@exception StandardException Oops
*/
private static String[] parseIdList(StringReader r, boolean normalize)
throws StandardException
{
Vector v = new Vector();
while (true)
{
int delim;
try {
String thisId = IdUtil.parseId(r,normalize);
v.addElement(thisId);
r.mark(0);
delim = r.read();
if (delim != ',')
{
if (delim!=-1) r.reset();
break;
}
}
catch (StandardException se){
if (se.getMessageId().equals(SQLState.ID_LIST_PARSE_ERROR))
throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR,se);
else
throw se;
}
catch (IOException ioe){
throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR,ioe);
}
}
if (v.size() == 0) return null;
String[] result = new String[v.size()];
v.copyInto(result);
return result;
}
private static String nextKey(StringReader reader) throws IOException {
reader.mark(1);
JsonString val = parseString(reader);
if (val == null) {
reader.reset();
}
// find delimiter
int intch;
char ch = ' ';
while ((intch = reader.read()) != -1) {
ch = (char) intch;
if (ch == StructuralToken.COLON.CHAR) {
// key/value delimiter found
break;
} else if (ch == StructuralToken.RCRBRACKET.CHAR) {
// end of document
break;
} else if (!whitespaceChars.contains(ch)) {
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.1", new Character[] { ch }));
}
}
if (ch != StructuralToken.COLON.CHAR && val != null && val.getString().length() > 0) {
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.4", new String[] { val.getString() }));
}
return val != null ? val.getString() : null;
}
/**
@param r The multi-part identifier to be parsed
@return An array of strings made by breaking the input string at its dots, '.'.
@exception StandardException Oops
*/
private static String[] parseMultiPartSQLIdentifier(StringReader r)
throws StandardException
{
Vector v = new Vector();
while (true)
{
String thisId = parseId(r,true);
v.addElement(thisId);
int dot;
try {
r.mark(0);
dot = r.read();
if (dot != '.')
{
if (dot!=-1) r.reset();
break;
}
}
catch (IOException ioe){
throw StandardException.newException(SQLState.ID_PARSE_ERROR,ioe);
}
}
String[] result = new String[v.size()];
v.copyInto(result);
return result;
}
/**
@param r The multi-part identifier to be parsed
@return An array of strings made by breaking the input string at its dots, '.'.
@exception StandardException Oops
*/
private static String[] parseMultiPartSQLIdentifier(StringReader r)
throws StandardException
{
Vector v = new Vector();
while (true)
{
String thisId = parseId(r,true);
v.add(thisId);
int dot;
try {
r.mark(0);
dot = r.read();
if (dot != '.')
{
if (dot!=-1) r.reset();
break;
}
}
catch (IOException ioe){
throw StandardException.newException(SQLState.ID_PARSE_ERROR,ioe);
}
}
String[] result = new String[v.size()];
v.copyInto(result);
return result;
}
/**
* Parse a delimited (quoted) identifier returning either
* the value of the identifier or a delimited identifier.
* @param r Quoted identifier to parse.
* @param normalize If true return a delimited identifer, otherwise return the identifier's value.
* @return the value of the identifer or a delimited identifier
* @throws IOException Error accessing value
* @throws StandardException Error parsing identifier.
*/
private static String parseQId(StringReader r,boolean normalize)
throws IOException,StandardException
{
StringBuilder b = new StringBuilder();
int c = r.read();
if (c != '"') throw StandardException.newException(SQLState.ID_PARSE_ERROR);
while (true)
{
c=r.read();
if (c == '"')
{
r.mark(0);
int c2 = r.read();
if (c2 != '"')
{
if (c2!=-1)r.reset();
break;
}
}
else if (c == -1)
throw StandardException.newException(SQLState.ID_PARSE_ERROR);
b.append((char)c);
}
if (b.length() == 0) //id can't be 0-length
throw StandardException.newException(SQLState.ID_PARSE_ERROR);
if (normalize)
return b.toString();
else
return normalToDelimited(b.toString()); //Put the quotes back.
}
/**
Parse a list of comma separated SQL identifiers returning
them a as elements in an array.
@param normalize true means return ids in nomral form, false means
return them as they were entered.
@exception StandardException Oops
*/
private static String[] parseIdList(StringReader r, boolean normalize)
throws StandardException
{
Vector v = new Vector();
while (true)
{
int delim;
try {
String thisId = IdUtil.parseId(r,normalize);
v.addElement(thisId);
r.mark(0);
delim = r.read();
if (delim != ',')
{
if (delim!=-1) r.reset();
break;
}
}
catch (StandardException se){
if (se.getMessageId().equals(SQLState.ID_LIST_PARSE_ERROR))
throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR,se);
else
throw se;
}
catch (IOException ioe){
throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR,ioe);
}
}
if (v.size() == 0) return null;
String[] result = new String[v.size()];
v.copyInto(result);
return result;
}
public void test_markI() throws IOException {
sr = new StringReader(testString);
try {
sr.mark(-1);
fail("IllegalArgumentException not thrown!");
} catch (IllegalArgumentException e) {
}
}
private static JsonValue nextValue(StringReader reader) throws IOException {
reader.mark(1);
int intch;
while ((intch = reader.read()) != -1) {
char ch = (char) intch;
if (ch == EscapeChar.QUOTE.CHAR) {
// String detected
reader.reset();
return parseString(reader);
} else if (ch == StructuralToken.LSQBRACKET.CHAR) {
// array detected
reader.reset();
return parseArray(reader);
} else if (ch == StructuralToken.LCRBRACKET.CHAR) {
// inner Object detected
reader.reset();
return parseDoc(reader);
} else if (ch == '\u002D' || (ch >= '\u0030' && ch <= '\u0039')) { // {-,0-9}
// Number detected
reader.reset();
return parseNumber(reader);
} else if (ch == JsonLiteral.TRUE.value.charAt(0)) {
// "true" literal detected
reader.reset();
return parseLiteral(reader);
} else if (ch == JsonLiteral.FALSE.value.charAt(0)) {
// "false" literal detected
reader.reset();
return parseLiteral(reader);
} else if (ch == JsonLiteral.NULL.value.charAt(0)) {
// "null" literal detected
reader.reset();
return parseLiteral(reader);
} else if (ch == StructuralToken.RSQBRACKET.CHAR) {
// empty array
return null;
} else if (!whitespaceChars.contains(ch)) {
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.1", new Character[] { ch }));
}
reader.mark(1);
}
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.5"));
}
/**
* Create {@link JsonLiteral} object from JSON string provided by reader.
*
* @param reader
* JSON string reader.
* @return
* New {@link JsonLiteral} object initialized by parsed JSON string.
* @throws IOException
* if can't read
*/
static JsonLiteral parseLiteral(StringReader reader) throws IOException {
StringBuilder sb = null;
JsonLiteral res = null;
int literalIndex = 0;
int intch;
while ((intch = reader.read()) != -1) {
char ch = (char) intch;
if (sb == null) {
// literal is still not found
if (ch == JsonLiteral.TRUE.value.charAt(0)) {
// first char of "true" literal is found
res = JsonLiteral.TRUE;
sb = new StringBuilder();
sb.append(ch);
literalIndex++;
} else if (ch == JsonLiteral.FALSE.value.charAt(0)) {
// first char of "false" literal is found
res = JsonLiteral.FALSE;
sb = new StringBuilder();
sb.append(ch);
literalIndex++;
} else if (ch == JsonLiteral.NULL.value.charAt(0)) {
// first char of "null" literal is found
res = JsonLiteral.NULL;
sb = new StringBuilder();
sb.append(ch);
literalIndex++;
} else if (!whitespaceChars.contains(ch)) {
// only whitespace chars are allowed before value
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.1", new Character[] { ch }));
}
} else if (literalIndex < res.value.length() && ch == res.value.charAt(literalIndex)) {
sb.append(ch);
literalIndex++;
} else if (whitespaceChars.contains(ch) || isValidEndOfValue(ch)) {
// any whitespace, colon or right bracket character means the end of literal in case we already placed something to buffer
reader.reset(); // set reader position to last number char
break;
} else {
// no other characters are allowed after value
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.1", new Character[] { ch }));
}
// it's safe to mark() here because the "higher" level marks won't be reset() once we start reading a number
reader.mark(1);
}
if (sb == null) {
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.5"));
}
if (literalIndex == res.value.length()) {
return res;
}
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.12", new String[] { sb.toString() }));
}