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

The following examples show how to use java.io.StringReader#read() . 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: TestTransmit.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static int nextNibble(StringReader r) throws IOException {
    while (true) {
        int ch = r.read();
        if (ch == -1) {
            return -1;
        } else if ((ch >= '0') && (ch <= '9')) {
            return ch - '0';
        } else if ((ch >= 'a') && (ch <= 'f')) {
            return ch - 'a' + 10;
        } else if ((ch >= 'A') && (ch <= 'F')) {
            return ch - 'A' + 10;
        } else if (ch == 'X') {
            return -2;
        }
    }
}
 
Example 2
Source File: ConflictDescriptionParser.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void parse (String description) throws IOException {
    StringReader sr = new StringReader(description);
    if (sr.read() == DELIMITER_OPEN_BRACKET) {
        while (true) {
            int c;
            while ((c = sr.read()) != -1 && c != DELIMITER_OPEN_BRACKET && c != DELIMITER_CLOSING_BRACKET); // wait for a bracket opening new conflict
            if (c == DELIMITER_CLOSING_BRACKET) { // end of description
                break;
            } else if (c != DELIMITER_OPEN_BRACKET) { // error
                throw new IOException("Error parsing description: " + description); //NOI18N
            }
            ParserConflictDescriptor conflict = readConflict(sr);
            if (conflict != null) {
                conflicts.add(conflict);
            }
        }
    }
}
 
Example 3
Source File: TestTransmit.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static int nextNibble(StringReader r) throws IOException {
    while (true) {
        int ch = r.read();
        if (ch == -1) {
            return -1;
        } else if ((ch >= '0') && (ch <= '9')) {
            return ch - '0';
        } else if ((ch >= 'a') && (ch <= 'f')) {
            return ch - 'a' + 10;
        } else if ((ch >= 'A') && (ch <= 'F')) {
            return ch - 'A' + 10;
        } else if (ch == 'X') {
            return -2;
        }
    }
}
 
Example 4
Source File: TestTransmit.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static int nextNibble(StringReader r) throws IOException {
    while (true) {
        int ch = r.read();
        if (ch == -1) {
            return -1;
        } else if ((ch >= '0') && (ch <= '9')) {
            return ch - '0';
        } else if ((ch >= 'a') && (ch <= 'f')) {
            return ch - 'a' + 10;
        } else if ((ch >= 'A') && (ch <= 'F')) {
            return ch - 'A' + 10;
        } else if (ch == 'X') {
            return -2;
        }
    }
}
 
Example 5
Source File: RFC2253Parser.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Method changeWStoXML
 *
 * @param string
 * @return normalized string
 * @throws IOException
 */
static String changeWStoXML(String string) throws IOException {
    StringBuilder sb = new StringBuilder();
    StringReader sr = new StringReader(string);
    int i = 0;
    char c;

    while ((i = sr.read()) > -1) {
        c = (char) i;

        if (c == '\\') {
            char c1 = (char) sr.read();

            if (c1 == ' ') {
                sb.append('\\');

                String s = "20";

                sb.append(s);
            } else {
                sb.append('\\');
                sb.append(c1);
            }
        } else {
            sb.append(c);
        }
    }

    return sb.toString();
}
 
Example 6
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 7
Source File: Utils.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static int nextNibble(StringReader r) throws IOException {
    while (true) {
        int ch = r.read();
        if (ch == -1) {
            return -1;
        } else if ((ch >= '0') && (ch <= '9')) {
            return ch - '0';
        } else if ((ch >= 'a') && (ch <= 'f')) {
            return ch - 'a' + 10;
        } else if ((ch >= 'A') && (ch <= 'F')) {
            return ch - 'A' + 10;
        }
    }
}
 
Example 8
Source File: Utils.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static int nextNibble(StringReader r) throws IOException {
    while (true) {
        int ch = r.read();
        if (ch == -1) {
            return -1;
        } else if ((ch >= '0') && (ch <= '9')) {
            return ch - '0';
        } else if ((ch >= 'a') && (ch <= 'f')) {
            return ch - 'a' + 10;
        } else if ((ch >= 'A') && (ch <= 'F')) {
            return ch - 'A' + 10;
        }
    }
}
 
Example 9
Source File: Utils.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static int nextNibble(StringReader r) throws IOException {
    while (true) {
        int ch = r.read();
        if (ch == -1) {
            return -1;
        } else if ((ch >= '0') && (ch <= '9')) {
            return ch - '0';
        } else if ((ch >= 'a') && (ch <= 'f')) {
            return ch - 'a' + 10;
        } else if ((ch >= 'A') && (ch <= 'F')) {
            return ch - 'A' + 10;
        }
    }
}
 
Example 10
Source File: RFC2253Parser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method changeLess32toRFC
 *
 * @param string
 * @return normalized string
 * @throws IOException
 */
static String changeLess32toRFC(String string) throws IOException {
    StringBuilder sb = new StringBuilder();
    StringReader sr = new StringReader(string);
    int i = 0;
    char c;

    while ((i = sr.read()) > -1) {
        c = (char) i;

        if (c == '\\') {
            sb.append(c);

            char c1 = (char) sr.read();
            char c2 = (char) sr.read();

            //65 (A) 97 (a)
            if ((((c1 >= 48) && (c1 <= 57)) || ((c1 >= 65) && (c1 <= 70)) || ((c1 >= 97) && (c1 <= 102)))
                && (((c2 >= 48) && (c2 <= 57))
                    || ((c2 >= 65) && (c2 <= 70))
                    || ((c2 >= 97) && (c2 <= 102)))) {
                char ch = (char) Byte.parseByte("" + c1 + c2, 16);

                sb.append(ch);
            } else {
                sb.append(c1);
                sb.append(c2);
            }
        } else {
            sb.append(c);
        }
    }

    return sb.toString();
}
 
Example 11
Source File: RFC2253Parser.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method changeLess32toRFC
 *
 * @param string
 * @return normalized string
 * @throws IOException
 */
static String changeLess32toRFC(String string) throws IOException {
    StringBuilder sb = new StringBuilder();
    StringReader sr = new StringReader(string);
    int i = 0;
    char c;

    while ((i = sr.read()) > -1) {
        c = (char) i;

        if (c == '\\') {
            sb.append(c);

            char c1 = (char) sr.read();
            char c2 = (char) sr.read();

            //65 (A) 97 (a)
            if ((((c1 >= 48) && (c1 <= 57)) || ((c1 >= 65) && (c1 <= 70)) || ((c1 >= 97) && (c1 <= 102)))
                && (((c2 >= 48) && (c2 <= 57))
                    || ((c2 >= 65) && (c2 <= 70))
                    || ((c2 >= 97) && (c2 <= 102)))) {
                char ch = (char) Byte.parseByte("" + c1 + c2, 16);

                sb.append(ch);
            } else {
                sb.append(c1);
                sb.append(c2);
            }
        } else {
            sb.append(c);
        }
    }

    return sb.toString();
}
 
Example 12
Source File: BasicUUID.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
	Read a long value, msb first, from its character 
	representation in the string reader, using '-' or
	end of string to delimit.
**/
private static long readMSB(StringReader sr)
   {
	long value = 0;

	try
	{
		int c;
		while ((c = sr.read()) != -1)
		{
			if (c == '-')
				break;
			value <<= 4;

			int nibble;
			if (c <= '9')
				nibble = c - '0';
			else if (c <= 'F')
				nibble = c - 'A' + 10;
			else
				nibble = c - 'a' + 10;
			value += nibble;
		}
	}
	catch (Exception e)
	{
	}

	return value;
   }
 
Example 13
Source File: RFC2253Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method changeWStoXML
 *
 * @param string
 * @return normalized string
 * @throws IOException
 */
static String changeWStoXML(String string) throws IOException {
    StringBuilder sb = new StringBuilder();
    StringReader sr = new StringReader(string);
    int i = 0;
    char c;

    while ((i = sr.read()) > -1) {
        c = (char) i;

        if (c == '\\') {
            char c1 = (char) sr.read();

            if (c1 == ' ') {
                sb.append('\\');

                String s = "20";

                sb.append(s);
            } else {
                sb.append('\\');
                sb.append(c1);
            }
        } else {
            sb.append(c);
        }
    }

    return sb.toString();
}
 
Example 14
Source File: HttpParser.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * @return the quoted string if one was found, null if data other than a
 *         quoted string was found or null if the end of data was reached
 *         before the quoted string was terminated
 */
private static String readQuotedString(StringReader input,
        boolean returnQuoted) throws IOException {

    int c = skipLws(input, false);

    if (c != '"') {
        return null;
    }

    StringBuilder result = new StringBuilder();
    if (returnQuoted) {
        result.append('\"');
    }
    c = input.read();

    while (c != '"') {
        if (c == -1) {
            return null;
        } else if (c == '\\') {
            c = input.read();
            if (returnQuoted) {
                result.append('\\');
            }
            result.append(c);
        } else {
            result.append((char) c);
        }
        c = input.read();
    }
    if (returnQuoted) {
        result.append('\"');
    }

    return result.toString();
}
 
Example 15
Source File: RC2AlgorithmParameters.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static int nextNibble(StringReader r) throws IOException {
    while (true) {
        int ch = r.read();
        if (ch == -1) {
            return -1;
        } else if ((ch >= '0') && (ch <= '9')) {
            return ch - '0';
        } else if ((ch >= 'a') && (ch <= 'f')) {
            return ch - 'a' + 10;
        } else if ((ch >= 'A') && (ch <= 'F')) {
            return ch - 'A' + 10;
        }
    }
}
 
Example 16
Source File: RFC2253Parser.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Method normalizeV
 *
 * @param str
 * @param toXml
 * @return normalized string
 * @throws IOException
 */
static String normalizeV(String str, boolean toXml) throws IOException {
    String value = trim(str);

    if (value.startsWith("\"")) {
        StringBuilder sb = new StringBuilder();
        StringReader sr = new StringReader(value.substring(1, value.length() - 1));
        int i = 0;
        char c;

        while ((i = sr.read()) > -1) {
            c = (char) i;

            //the following char is defined at 4.Relationship with RFC1779 and LDAPv2 inrfc2253
            if ((c == ',') || (c == '=') || (c == '+') || (c == '<')
                || (c == '>') || (c == '#') || (c == ';')) {
                sb.append('\\');
            }

            sb.append(c);
        }

        value = trim(sb.toString());
    }

    if (toXml) {
        if (value.startsWith("#")) {
            value = '\\' + value;
        }
    } else {
        if (value.startsWith("\\#")) {
            value = value.substring(1);
        }
    }

    return value;
}
 
Example 17
Source File: SqlValuesHighlight.java    From hop with Apache License 2.0 4 votes vote down vote up
public void parseBlockComments( String text ) {
  blockComments = new Vector<int[]>();
  StringReader buffer = new StringReader( text );
  int ch;
  boolean blkComment = false;
  int cnt = 0;
  int[] offsets = new int[ 2 ];
  boolean done = false;

  try {
    while ( !done ) {
      switch ( ch = buffer.read() ) {
        case -1: {
          if ( blkComment ) {
            offsets[ 1 ] = cnt;
            blockComments.addElement( offsets );
          }
          done = true;
          break;
        }
        case '/': {
          ch = buffer.read();
          if ( ( ch == '*' ) && ( !blkComment ) ) {
            offsets = new int[ 2 ];
            offsets[ 0 ] = cnt;
            blkComment = true;
            cnt++;
          } else {
            cnt++;
          }
          cnt++;
          break;
        }
        case '*': {
          if ( blkComment ) {
            ch = buffer.read();
            cnt++;
            if ( ch == '/' ) {
              blkComment = false;
              offsets[ 1 ] = cnt;
              blockComments.addElement( offsets );
            }
          }
          cnt++;
          break;
        }
        default: {
          cnt++;
          break;
        }
      }
    }
  } catch ( IOException e ) {
    // ignore errors
  }
}
 
Example 18
Source File: JsonParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 
 * @param reader
 *            JSON string reader.
 * @return
 *         New {@link JsonString} object initialized by parsed JSON string or <code>null</code> if no JSON string was found.
 * @throws IOException
 *             if can't read
 */
static JsonString parseString(StringReader reader) throws IOException {
    int quotes = 0;
    boolean escapeNextChar = false;

    StringBuilder sb = null; // stays null until starting quotation mark is found

    int intch;
    while ((intch = reader.read()) != -1) {
        char ch = (char) intch;
        if (escapeNextChar) {
            if (unescapeChars.containsKey(ch)) {
                appendChar(sb, unescapeChars.get(ch));
            } else {
                throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.7", new Character[] { ch }));
            }
            escapeNextChar = false;

        } else if (ch == EscapeChar.QUOTE.CHAR) {
            if (sb == null) {
                // start of string detected
                sb = new StringBuilder();
                quotes++;
            } else {
                // end of string detected
                quotes--;
                break;
            }

        } else if (quotes == 0 && ch == StructuralToken.RCRBRACKET.CHAR) {
            // end of document
            break;

        } else if (ch == EscapeChar.RSOLIDUS.CHAR) {
            escapeNextChar = true;

        } else {
            appendChar(sb, ch);
        }
    }

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

    return sb == null ? null : new JsonString().setValue(sb.toString());
}
 
Example 19
Source File: JsonParser.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Create {@link JsonString} object from JSON string provided by reader.
 * 
 * @param reader
 *            JSON string reader.
 * @return
 *         New {@link JsonString} object initialized by parsed JSON string or <code>null</code> if no JSON string was found.
 * @throws IOException
 *             if can't read
 */
static JsonString parseString(StringReader reader) throws IOException {
    int quotes = 0;
    boolean escapeNextChar = false;

    StringBuilder sb = null; // stays null until starting quotation mark is found

    int intch;
    while ((intch = reader.read()) != -1) {
        char ch = (char) intch;
        if (escapeNextChar) {
            if (unescapeChars.containsKey(ch)) {
                appendChar(sb, unescapeChars.get(ch));
            } else {
                throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("JsonParser.7", new Character[] { ch }));
            }
            escapeNextChar = false;

        } else if (ch == EscapeChar.QUOTE.CHAR) {
            if (sb == null) {
                // start of string detected
                sb = new StringBuilder();
                quotes++;
            } else {
                // end of string detected
                quotes--;
                break;
            }

        } else if (quotes == 0 && ch == StructuralToken.RCRBRACKET.CHAR) {
            // end of document
            break;

        } else if (ch == EscapeChar.RSOLIDUS.CHAR) {
            escapeNextChar = true;

        } else {
            appendChar(sb, ch);
        }
    }

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

    return sb == null ? null : new JsonString().setValue(sb.toString());
}
 
Example 20
Source File: MDXValuesHighlight.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public void parseBlockComments( String text ) {
  blockComments = new Vector<int[]>();
  StringReader buffer = new StringReader( text );
  int ch;
  boolean blkComment = false;
  int cnt = 0;
  int[] offsets = new int[2];
  boolean done = false;

  try {
    while ( !done ) {
      switch ( ch = buffer.read() ) {
        case -1: {
          if ( blkComment ) {
            offsets[1] = cnt;
            blockComments.addElement( offsets );
          }
          done = true;
          break;
        }
        case '/': {
          ch = buffer.read();
          if ( ( ch == '*' ) && ( !blkComment ) ) {
            offsets = new int[2];
            offsets[0] = cnt;
            blkComment = true;
            cnt++;
          } else {
            cnt++;
          }
          cnt++;
          break;
        }
        case '*': {
          if ( blkComment ) {
            ch = buffer.read();
            cnt++;
            if ( ch == '/' ) {
              blkComment = false;
              offsets[1] = cnt;
              blockComments.addElement( offsets );
            }
          }
          cnt++;
          break;
        }
        default: {
          cnt++;
          break;
        }
      }
    }
  } catch ( IOException e ) {
    // ignore errors
  }
}