Java Code Examples for java.io.StringReader#mark()

The following examples show how to use java.io.StringReader#mark() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpParser.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: Bencode.java    From android with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: HttpParser.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: IdUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
    * 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;
}
 
Example 5
Source File: Bencode.java    From android with GNU General Public License v3.0 6 votes vote down vote up
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");
    }
}
 
Example 6
Source File: IdUtil.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
    * 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;
}
 
Example 7
Source File: MediaType.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
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;
        }
 
Example 8
Source File: IdUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
   @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;
}
 
Example 9
Source File: IdUtil.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
  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;
}
 
Example 10
Source File: IdUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
    * 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.
}
 
Example 11
Source File: IdUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
  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;
}
 
Example 12
Source File: JsonParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: JsonParser.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: IdUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
   @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;
}
 
Example 15
Source File: IdUtil.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
   @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;
}
 
Example 16
Source File: IdUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
    * 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.
}
 
Example 17
Source File: IdUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
  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;
}
 
Example 18
Source File: OldStringReaderTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_markI() throws IOException {
    sr = new StringReader(testString);
    try {
        sr.mark(-1);
        fail("IllegalArgumentException not thrown!");
    } catch (IllegalArgumentException e) {
    }
}
 
Example 19
Source File: JsonParser.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
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"));
}
 
Example 20
Source File: JsonParser.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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() }));
}