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

The following are Jave code examples for showing how to use read() of the java.io.StringReader class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: openjdk-jdk10   File: RFC2253Parser.java   Source Code and License Vote up 6 votes
/**
 * Method changeLess32toXML
 *
 * @param string
 * @return normalized string
 * @throws IOException
 */
static String changeLess32toXML(String string) throws IOException {
    StringBuilder sb = new StringBuilder();
    StringReader sr = new StringReader(string);
    int i = 0;

    while ((i = sr.read()) > -1) {
        if (i < 32) {
            sb.append('\\');
            sb.append(Integer.toHexString(i));
        } else {
            sb.append((char) i);
        }
    }

    return sb.toString();
}
 
Example 2
Project: openjdk-jdk10   File: TestTransmit.java   Source Code and License Vote up 6 votes
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 3
Project: tomcat7   File: HttpParser.java   Source Code and License Vote up 6 votes
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
Project: tomcat7   File: HttpParser.java   Source Code and License Vote up 6 votes
private static SkipConstantResult skipConstant(StringReader input,
        String constant) throws IOException {
    int len = constant.length();

    int c = skipLws(input, false);

    for (int i = 0; i < len; i++) {
        if (i == 0 && c == -1) {
            return SkipConstantResult.EOF;
        }
        if (c != constant.charAt(i)) {
            input.skip(-(i + 1));
            return SkipConstantResult.NOT_FOUND;
        }
        if (i != (len - 1)) {
            c = input.read();
        }
    }
    return SkipConstantResult.FOUND;
}
 
Example 5
Project: lazycat   File: HttpParser.java   Source Code and License Vote up 6 votes
/**
 * @return the token if one was found, the empty string if no data was
 *         available to read or <code>null</code> if data other than a token
 *         was found
 */
private static String readToken(StringReader input) throws IOException {
	StringBuilder result = new StringBuilder();

	int c = skipLws(input, false);

	while (c != -1 && isToken(c)) {
		result.append((char) c);
		c = input.read();
	}
	// Skip back so non-token character is available for next read
	input.skip(-1);

	if (c != -1 && result.length() == 0) {
		return null;
	} else {
		return result.toString();
	}
}
 
Example 6
Project: lazycat   File: HttpParser.java   Source Code and License Vote up 6 votes
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 7
Project: OpenJSharp   File: RFC2253Parser.java   Source Code and License Vote up 5 votes
/**
 * 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 8
Project: Conan   File: HelpView.java   Source Code and License Vote up 5 votes
/**
 * @param A string containing a comma separated list of formulas
 * @return an array with the input formulas split up
 * @throws IOException
 */
public String[] splitFormulas(String str) throws IOException {
    StringReader strR = new StringReader(str);
    ArrayList<String> formulaList = new ArrayList<String>();

    int next;
    StringBuilder strB = new StringBuilder();
    int unmatchedLeftPar = 0;
    while ((next = strR.read()) != -1) {
        char ch = (char) next;

        switch (ch) {
            case ',':
                if (unmatchedLeftPar == 0) {
                    formulaList.add(strB + "");
                    strB = new StringBuilder();
                    continue;
                }
                break;

            case '(':
                unmatchedLeftPar++;
                break;

            case ')':
                unmatchedLeftPar = (unmatchedLeftPar == 0) ? 0 : --unmatchedLeftPar;
                break;

            default:
                break;
        }
        if(ch != ' '){
        	strB.append(ch);
        }

    }
    formulaList.add(strB + "");
    return formulaList.toArray(new String[formulaList.size()]);
}
 
Example 9
Project: jdk8u-jdk   File: RFC2253Parser.java   Source Code and License Vote up 5 votes
/**
 * 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 10
Project: JavaRushTasks   File: Solution.java   Source Code and License Vote up 5 votes
public static String decode(StringReader reader, int key) throws IOException {
    int a;
    StringBuffer res = new StringBuffer();

    try {
        while ((a = reader.read()) != -1) {
            res.append(Character.toString((char) (a + key)));
        }
    } catch (Exception e) {
        return new String();
    }

    return res.toString();
}
 
Example 11
Project: LearningOfThinkInJava   File: MemoryInput.java   Source Code and License Vote up 5 votes
public static void main(String[] args)
throws IOException{
    StringReader in=new StringReader(
            BufferedInputFile.read("./src/main/java/io/source/hello.txt"));
    int c;
    while ((c=in.read())!=-1){
        System.out.print((char)c);
    }
}
 
Example 12
Project: Tarski   File: RelationParser.java   Source Code and License Vote up 5 votes
public void init() throws IOException {
  final StringReader reader = new StringReader(this.relationData);

  int open = 0;
  int offset = 1;
  int c = reader.read();
  while (c != -1) {
    if (c == '[' || c == '{') {
      open++;
    } else if (c == ']' || c == '}') {
      open--;
    }

    if (open == 1 && c == ',') {
      reader.close();
      break;
    }

    offset++;
    c = reader.read();
  }
  reader.close();

  if (offset > this.relationData.length()) {
    this.lowerBoundData =
        this.relationData.substring(this.relationData.indexOf("[") + 1, offset - 2);
    this.upperBoundData = this.relationData.substring(offset - 2, this.relationData.length() - 1);
  } else {
    this.lowerBoundData =
        this.relationData.substring(this.relationData.indexOf("[") + 1, offset - 1);
    this.upperBoundData = this.relationData.substring(offset, this.relationData.length() - 1);
  }

  this.parse2Tuples();
}
 
Example 13
Project: openjdk-jdk10   File: RFC2253Parser.java   Source Code and License Vote up 5 votes
/**
 * 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
Project: tomcat7   File: HttpParser.java   Source Code and License Vote up 5 votes
/**
 * @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
Project: tomcat7   File: HttpParser.java   Source Code and License Vote up 5 votes
/**
 * Token can be read unambiguously with or without surrounding quotes so
 * this parsing method for token permits optional surrounding double quotes.
 * This is not defined in any RFC. It is a special case to handle data from
 * buggy clients (known buggy clients for DIGEST auth include Microsoft IE 8
 * &amp; 9, Apple Safari for OSX and iOS) that add quotes to values that
 * should be tokens.
 *
 * @return the token if one was found, null if data other than a token or
 *         quoted token was found or null if the end of data was reached
 *         before a quoted token was terminated
 */
private static String readQuotedToken(StringReader input)
        throws IOException {

    StringBuilder result = new StringBuilder();
    boolean quoted = false;

    int c = skipLws(input, false);

    if (c == '"') {
        quoted = true;
    } else if (c == -1 || !isToken(c)) {
        return null;
    } else {
        result.append((char) c);
    }
    c = input.read();

    while (c != -1 && isToken(c)) {
        result.append((char) c);
        c = input.read();
    }

    if (quoted) {
        if (c != '"') {
            return null;
        }
    } else {
        // Skip back so non-token character is available for next read
        input.skip(-1);
    }

    if (c != -1 && result.length() == 0) {
        return null;
    } else {
        return result.toString();
    }
}
 
Example 16
Project: iTAP-controller   File: SyncClient.java   Source Code and License Vote up 5 votes
@Override
public boolean execute(String[] tokens, String line) throws Exception {
    if (tokens.length < 3) {
        err.println("Usage: " + syntaxString());
        return false;

    }
    if (!checkStoreSettings()) return false;

    StringReader sr = new StringReader(line);
    while (sr.read() != ' ');
    JsonParser jp = mjf.createJsonParser(sr);
    
    JsonNode keyNode = validateJson(jp);
    if (keyNode == null) return false;
    JsonNode valueNode = validateJson(jp);
    if (valueNode == null) return false;

    ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
    out.println("Putting Key:");
    out.println(writer.writeValueAsString(keyNode));
    out.println("\nValue:");
    out.println(writer.writeValueAsString(valueNode));
    out.flush();

    storeClient.put(keyNode, valueNode);
    out.println("Success");
                
    return false;
}
 
Example 17
Project: sirocco   File: FieldSerializer.java   Source Code and License Vote up 5 votes
public static int readInt32(StringReader rd) throws Exception {
    // if there is an error in encoding, let the base functions throw an exception
    CSList<Character> clist = new CSList<Character>();
    int nextchar;
    while (((nextchar = rd.read()) != -1) && ((char)nextchar != '|'))
    {
    	clist.add((char)nextchar);
    }
     	
    // ignore
    String number = clist.toArray().toString();
    return Integer.valueOf(number);
}
 
Example 18
Project: apache-tomcat-7.0.73-with-comment   File: HttpParser.java   Source Code and License Vote up 5 votes
/**
 * Token can be read unambiguously with or without surrounding quotes so
 * this parsing method for token permits optional surrounding double quotes.
 * This is not defined in any RFC. It is a special case to handle data from
 * buggy clients (known buggy clients for DIGEST auth include Microsoft IE 8
 * &amp; 9, Apple Safari for OSX and iOS) that add quotes to values that
 * should be tokens.
 *
 * @return the token if one was found, null if data other than a token or
 *         quoted token was found or null if the end of data was reached
 *         before a quoted token was terminated
 */
private static String readQuotedToken(StringReader input)
        throws IOException {

    StringBuilder result = new StringBuilder();
    boolean quoted = false;

    int c = skipLws(input, false);

    if (c == '"') {
        quoted = true;
    } else if (c == -1 || !isToken(c)) {
        return null;
    } else {
        result.append((char) c);
    }
    c = input.read();

    while (c != -1 && isToken(c)) {
        result.append((char) c);
        c = input.read();
    }

    if (quoted) {
        if (c != '"') {
            return null;
        }
    } else {
        // Skip back so non-token character is available for next read
        input.skip(-1);
    }

    if (c != -1 && result.length() == 0) {
        return null;
    } else {
        return result.toString();
    }
}
 
Example 19
Project: sirocco   File: LabelledPositionsV2.java   Source Code and License Vote up 5 votes
public static LabelledSpan readLabelledSpan(StringReader rd) throws Exception {
    int start = FieldSerializer.readInt32(rd);
    int end = FieldSerializer.readInt32(rd);
    int labelcount = FieldSerializer.readInt32(rd);
    CSList<Character> labels = new CSList<Character>(labelcount);
    for (int j = labelcount;j > 0;j--)
    {
        char label = (char)rd.read();
        labels.add(label);
    }
    return new LabelledSpan(start,end,labels);
}
 
Example 20
Project: tomcat7   File: HttpParser.java   Source Code and License Vote up 4 votes
/**
 * LHEX can be read unambiguously with or without surrounding quotes so this
 * parsing method for LHEX permits optional surrounding double quotes. Some
 * buggy clients (libwww-perl for DIGEST auth) are known to send quoted LHEX
 * when the specification requires just LHEX.
 *
 * <p>
 * LHEX are, literally, lower-case hexadecimal digits. This implementation
 * allows for upper-case digits as well, converting the returned value to
 * lower-case.
 *
 * @return  the sequence of LHEX (minus any surrounding quotes) if any was
 *          found, or <code>null</code> if data other LHEX was found
 */
private static String readLhex(StringReader input)
        throws IOException {

    StringBuilder result = new StringBuilder();
    boolean quoted = false;

    int c = skipLws(input, false);

    if (c == '"') {
        quoted = true;
    } else if (c == -1 || !isHex(c)) {
        return null;
    } else {
        if ('A' <= c && c <= 'F') {
            c -= ('A' - 'a');
        }
        result.append((char) c);
    }
    c = input.read();

    while (c != -1 && isHex(c)) {
        if ('A' <= c && c <= 'F') {
            c -= ('A' - 'a');
        }
        result.append((char) c);
        c = input.read();
    }

    if (quoted) {
        if (c != '"') {
            return null;
        }
    } else {
        // Skip back so non-hex character is available for next read
        input.skip(-1);
    }

    if (c != -1 && result.length() == 0) {
        return null;
    } else {
        return result.toString();
    }
}