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

The following examples show how to use java.io.StringReader#reset() . 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: CSSStyleSheet.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the contents of the specified input source, ignoring any {@link IOException}s.
 * @param source the input source from which to read
 * @return the contents of the specified input source, or an empty string if an {@link IOException} occurs
 */
private static String toString(final InputSource source) {
    try {
        final Reader reader = source.getReader();
        if (null != reader) {
            // try to reset to produce some output
            if (reader instanceof StringReader) {
                final StringReader sr = (StringReader) reader;
                sr.reset();
            }
            return IOUtils.toString(reader);
        }
        return "";
    }
    catch (final IOException e) {
        LOG.error(e.getMessage(), e);
        return "";
    }
}
 
Example 2
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 3
Source File: CSSStyleSheet.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the contents of the specified input source, ignoring any {@link IOException}s.
 * @param source the input source from which to read
 * @return the contents of the specified input source, or an empty string if an {@link IOException} occurs
 */
private static String toString(final InputSource source) {
    try {
        final Reader reader = source.getReader();
        if (null != reader) {
            // try to reset to produce some output
            if (reader instanceof StringReader) {
                final StringReader sr = (StringReader) reader;
                sr.reset();
            }
            return IOUtils.toString(reader);
        }
        return "";
    }
    catch (final IOException e) {
        return "";
    }
}
 
Example 4
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 5
Source File: IdUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
  Read an id from the StringReader provided.


  @param normalize true means return ids in nomral form, false means
        return them as they were entered.

  <P>
  Raise an exception if the first thing in the StringReader
  is not a valid id.

  @exception StandardException Ooops.
  */
private static String parseId(StringReader r, boolean normalize)
	 throws StandardException
{
	try {
		r.mark(0);
		int c = r.read();
		if (c == -1)  //id can't be 0-length
			throw StandardException.newException(SQLState.ID_PARSE_ERROR);
			r.reset();
		if (c == '"')
			return parseQId(r,normalize);
		else
			return parseUnQId(r,normalize);
	}

	catch (IOException ioe){
		throw StandardException.newException(SQLState.ID_PARSE_ERROR,ioe);
	}
}
 
Example 6
Source File: IdUtil.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
  Read an id from the StringReader provided.


  @param normalize true means return ids in nomral form, false means
        return them as they were entered.

  <P>
  Raise an exception if the first thing in the StringReader
  is not a valid id.

  @exception StandardException Ooops.
  */
private static String parseId(StringReader r, boolean normalize)
	 throws StandardException
{
	try {
		r.mark(0);
		int c = r.read();
		if (c == -1)  //id can't be 0-length
			throw StandardException.newException(SQLState.ID_PARSE_ERROR);
			r.reset();
		if (c == '"')
			return parseQId(r,normalize);
		else
			return parseUnQId(r,normalize);
	}

	catch (IOException ioe){
		throw StandardException.newException(SQLState.ID_PARSE_ERROR,ioe);
	}
}
 
Example 7
Source File: IdUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
  Read an id from the StringReader provided.


  @param normalize true means return ids in nomral form, false means
        return them as they were entered.

  <P>
  Raise an exception if the first thing in the StringReader
  is not a valid id.

  @exception StandardException Ooops.
  */
private static String parseId(StringReader r, boolean normalize)
	 throws StandardException
{
	try {
		r.mark(0);
		int c = r.read();
		if (c == -1)  //id can't be 0-length
			throw StandardException.newException(SQLState.ID_PARSE_ERROR);
			r.reset();
		if (c == '"')
			return parseQId(r,normalize);
		else
			return parseUnQId(r,normalize);
	}

	catch (IOException ioe){
		throw StandardException.newException(SQLState.ID_PARSE_ERROR,ioe);
	}
}
 
Example 8
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 9
Source File: LexerFrame.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void scanScript(final StringReader reader) throws Exception {
    scriptPane.read(reader, null);
    reader.reset();

    // create lexer
    final GroovyLangLexer lexer = new GroovyLangLexer(reader);

    tokenPane.setEditable(true);
    tokenPane.setText("");

    int line = 1;
    final ButtonGroup bg = new ButtonGroup();
    Token token;

    while (true) {
        token = lexer.nextToken();
        JToggleButton tokenButton = new JToggleButton(tokens.get(token.getType()));
        bg.add(tokenButton);
        tokenButton.addActionListener(this);
        tokenButton.setToolTipText(token.getText());
        tokenButton.putClientProperty("token", token);
        tokenButton.setMargin(new Insets(0, 1, 0, 1));
        tokenButton.setFocusPainted(false);
        if (token.getLine() > line) {
            tokenPane.getDocument().insertString(tokenPane.getDocument().getLength(), "\n", null);
            line = token.getLine();
        }
        insertComponent(tokenButton);
        if (eof().equals(token.getType())) {
            break;
        }
    }

    tokenPane.setEditable(false);
    tokenPane.setCaretPosition(0);
    reader.close();
}
 
Example 10
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 11
Source File: IdUtil.java    From spliceengine with GNU Affero General Public License v3.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 12
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 13
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 14
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 15
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 16
Source File: DiffUtils.java    From dts with Apache License 2.0 5 votes vote down vote up
public static String read(StringReader stringReader) {
    StringBuilder stringBuilder = new StringBuilder();
    try {
        stringReader.reset();
        int c;
        while ((c = stringReader.read()) != -1) {
            stringBuilder.append((char)c);
        }
        return stringBuilder.toString();
    } catch (IOException e) {
        return "";
    }
}
 
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: JsonParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 
 * @param reader
 *            JSON string reader.
 * @return
 *         New {@link JsonArray} object initialized by parsed JSON string.
 * @throws IOException
 *             if can't read
 */
public static JsonArray parseArray(StringReader reader) throws IOException {

    JsonArray arr = new JsonArray();
    JsonValue val;
    int openings = 0;

    int intch;
    while ((intch = reader.read()) != -1) {
        char ch = (char) intch;
        if (ch == StructuralToken.LSQBRACKET.CHAR || ch == StructuralToken.COMMA.CHAR) {
            if (ch == StructuralToken.LSQBRACKET.CHAR) {
                openings++;
            }
            if ((val = nextValue(reader)) != null) {
                arr.add(val);
            } else {
                reader.reset();
            }

        } else if (ch == StructuralToken.RSQBRACKET.CHAR) {
            openings--;
            break;

        } else if (!whitespaceChars.contains(ch)) {
            throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.1", new Character[] { ch }));

        }
    }

    if (openings > 0) {
        throw ExceptionFactory.createException(WrongArgumentException.class,
                Messages.getString("JsonParser.3", new Character[] { StructuralToken.RSQBRACKET.CHAR }));
    }

    return arr;
}
 
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: Pager.java    From remotekeyboard with Apache License 2.0 votes vote down vote up
/**
   * Method that pages the String to the client terminal,
   * being aware of its geometry, and its geometry changes.
   *
   * @param str String to be paged.
   */
  public void page(String str) throws IOException {
    terminalGeometryChanged();
    boolean autoflush = m_IO.isAutoflushing();
    m_IO.setAutoflushing(true);
    //store raw
    m_Source = new StringReader(str);
    //do renderchunks
    m_ChunkPos = 0;
    m_LastNewChunk = 0;
    m_EOS = false;
    m_NoPrompt = false;

    renderChunks();
    if (m_Chunks.size() == 1) {
      m_IO.write((String) m_Chunks.elementAt(0));
    } else {
      m_IO.homeCursor();
      m_IO.eraseScreen();
      m_IO.write((String) m_Chunks.elementAt(m_ChunkPos));
      updateStatus();
      m_Status.draw();
      //storage for read byte
      int in = 0;
      do {
        m_NoPrompt = false;

        //get next key
        in = m_IO.read();
        if (terminalGeometryChanged()) {
          try {
            m_Source.reset();
          } catch (Exception ex) {
          }
          renderChunks();
          m_ChunkPos = 0;
          m_LastNewChunk = 0;
          m_EOS = false;
          m_NoPrompt = false;
          m_IO.homeCursor();
          m_IO.eraseScreen();
          m_IO.write((String) m_Chunks.elementAt(m_ChunkPos));
          updateStatus();
          m_Status.draw();
          continue;
        }
        switch (in) {
          case BasicTerminalIO.UP:
            drawPreviousPage();
            break;
          case BasicTerminalIO.DOWN:
            drawNextPage();
            break;
          case SPACE:
            drawNextPage();
            break;
          default:
            //test for stopkey, cant be switched because not constant
            if (in == m_StopKey) {
              //flag loop over
              in = -1;
              continue; //so that we omit prompt and return
            } else {
              m_IO.bell();
              continue;
            }
        }
        if (m_EOS) {
          in = -1;
          continue;
        }
        //prompt
        if (!m_NoPrompt) {
          updateStatus();
          m_Status.draw();
        }
      } while (in != -1);
      m_IO.eraseToEndOfLine();

    }
    m_IO.write("\n");
    m_Source.close();
    m_IO.setAutoflushing(autoflush);
  }