Example usage for java.io StringReader read

List of usage examples for java.io StringReader read

Introduction

In this page you can find the example usage for java.io StringReader read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:it.vige.greenarea.geo.GoogleGis.java

private static String removeSpaces(String inputString) throws IOException {
        char ch;/*from   ww  w  .j ava  2  s.co  m*/
        StringReader legge = new StringReader(inputString);
        String risultato = "";
        if (inputString == null) {
            return null;
        }
        while ((ch = (char) legge.read()) != StringCharacterIterator.DONE) {
            if (ch == ' ') {
                risultato += "%20";
            } else {
                risultato += ch;
            }
        }
        return risultato;
    }

From source file:things.common.tools.FileTools.java

/**
 * This will detect if the path has any relative pointers (such as ..).  
 * @param path The path/*w  w  w.  j av a 2  s  .c o m*/
 * @return true if it does, otherwise false.
 */
public static boolean detectRelativePath(String path) {
    if (path != null) {
        try {
            // detect no root
            String trimmed = path.trim();
            if (((trimmed.charAt(1) != ':') && (trimmed.charAt(0) != '/') && (trimmed.charAt(0) != '\\'))
                    || (trimmed.charAt(0) == '.'))
                return true;

            // find dots
            StringReader rin = new StringReader(path);
            State rpState = State.SEEK;
            int currentChar = rin.read();
            while (currentChar >= 0) {
                switch (rpState) {
                case SEEK:
                    if (currentChar == '\\' || currentChar == '/')
                        rpState = State.START;
                    break;
                case START:
                    if (currentChar == '.')
                        rpState = State.DOT;
                    else if (currentChar != '\\' && currentChar != '/')
                        rpState = State.SEEK;
                    break;
                case DOT:
                    if (currentChar == '\\' || currentChar == '/')
                        return true;
                    if (currentChar != '.')
                        rpState = State.START;
                    break;
                }
                currentChar = rin.read();
            }
        } catch (Exception ee) {
            // Don't care.  Should only be a spurious EOF
        }
    }
    return false;
}

From source file:org.apache.xml.security.utils.RFC2253Parser.java

/**
 * Method changeLess32toXML//from   ww  w. jav a2  s.  c  o  m
 *
 * @param string
 * @return normalized string
 * @throws IOException
 */
static String changeLess32toXML(String string) throws IOException {

    StringBuffer sb = new StringBuffer();
    StringReader sr = new StringReader(string);
    int i = 0;

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

    return sb.toString();
}

From source file:org.apache.xml.security.utils.RFC2253Parser.java

/**
 * Method changeWStoXML/*from w  w w .  j  a  v a2  s.  c  om*/
 *
 * @param string
 * @return normalized string
 * @throws IOException
 */
static String changeWStoXML(String string) throws IOException {

    StringBuffer sb = new StringBuffer();
    StringReader sr = new StringReader(string);
    int i = 0;
    char c;

    for (; (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();
}

From source file:org.apache.xml.security.utils.RFC2253Parser.java

/**
 * Method changeLess32toRFC// ww  w .  j  ava2s. c  o  m
 *
 * @param string
 * @return normalized string
 * @throws IOException
 */
static String changeLess32toRFC(String string) throws IOException {

    StringBuffer sb = new StringBuffer();
    StringReader sr = new StringReader(string);
    int i = 0;
    char c;

    for (; (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();
}

From source file:org.bibsonomy.util.WebUtils.java

/** Writes the given string to the stream.
 * //from w ww .ja  v a2  s.  c o m
 * @param s
 * @param outputStream
 * @throws IOException
 */
private static void writeStringToStream(final String s, final OutputStream outputStream) throws IOException {
    final StringReader reader = new StringReader(s);
    int b;
    while ((b = reader.read()) >= 0) {
        outputStream.write(b);
    }
    outputStream.flush();
}

From source file:org.apache.xml.security.utils.RFC2253Parser.java

/**
 * Method normalizeV/*from   ww  w. ja v a2 s  .co m*/
 *
 * @param str
 * @return normalized string
 * @throws IOException
 */
static String normalizeV(String str) throws IOException {

    String value = trim(str);

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

        for (; (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 == true) {
        if (value.startsWith("#")) {
            value = '\\' + value;
        }
    } else {
        if (value.startsWith("\\#")) {
            value = value.substring(1);
        }
    }

    return value;
}

From source file:com.unboundid.scim2.common.utils.Parser.java

/**
 * Read a path token. A token is either:
 * <ul>/* w w  w  .j a v a 2s  .  co m*/
 *   <li>
 *     An attribute name terminated by a period.
 *   </li>
 *   <li>
 *     An attribute name terminated by an opening brace.
 *   </li>
 *   <li>
 * </ul>
 *
 * @param reader The reader to read from.
 *
 * @return The token at the current position, or {@code null} if the end of
 *         the input has been reached.
 * @throws BadRequestException If the path string could not be parsed.
 */
private static String readPathToken(final StringReader reader) throws BadRequestException {
    reader.mark(0);
    int c = reader.read();

    StringBuilder b = new StringBuilder();
    while (c > 0) {
        if (c == '.') {
            if (reader.pos >= reader.string.length()) {
                // There is nothing after the period.
                throw BadRequestException.invalidPath("Unexpected end of path string");
            }
            // Terminating period. Consume it and return token.
            return b.toString();
        }
        if (c == '[') {
            // Terminating opening brace. Consume it and return token.
            b.append((char) c);
            return b.toString();
        }
        if (c == '-' || c == '_' || c == '$' || Character.isLetterOrDigit(c)) {
            b.append((char) c);
        } else {
            final String msg = String.format(
                    "Unexpected character '%s' at position %d for token starting at %d", (char) c,
                    reader.pos - 1, reader.mark);
            throw BadRequestException.invalidPath(msg);
        }
        c = reader.read();
    }

    if (b.length() > 0) {
        return b.toString();
    }
    return null;
}

From source file:org.andrewberman.sync.InheritMe.java

public static void writeFile(File f, String text) throws Exception {
    FileWriter fw = new FileWriter(f);
    StringReader read = new StringReader(text);
    int c;// w w  w  . j a  v  a2  s  .c  o m
    while ((c = read.read()) != -1) {
        fw.write(c);
    }
    fw.close();
    read.close();
}

From source file:com.unboundid.scim2.common.utils.Parser.java

/**
 * Read a filter token. A token is either:
 * <ul>// w w w .j  av  a  2  s  .  c o m
 *   <li>
 *     An attribute path terminated by a space or an opening parenthesis.
 *   </li>
 *   <li>
 *     An attribute path terminated by an opening brace.
 *   </li>
 *   <li>
 *     An operator terminated by a space or an opening parenthesis.
 *   </li>
 *   <li>
 *     An opening parenthesis.
 *   </li>
 *   <li>
 *     An closing parenthesis.
 *   </li>
 *   <li>
 *     An closing brace.
 *   </li>
 *   <li>
 *
 *   </li>
 * </ul>
 *
 * @param reader The reader to read from.
 * @param isValueFilter Whether to read the token for a value filter.
 *
 * @return The token at the current position, or {@code null} if the end of
 *         the input has been reached.
 * @throws BadRequestException If the filter string could not be parsed.
 */
private static String readFilterToken(final StringReader reader, final boolean isValueFilter)
        throws BadRequestException {
    int c;
    do {
        // Skip over any leading spaces.
        reader.mark(0);
        c = reader.read();
    } while (c == ' ');

    StringBuilder b = new StringBuilder();
    while (c > 0) {
        if (c == ' ') {
            // Terminating space. Consume it and return token.
            return b.toString();
        }
        if (c == '(' || c == ')') {
            if (b.length() > 0) {
                // Do not consume the parenthesis.
                reader.unread();
            } else {
                b.append((char) c);
            }
            return b.toString();
        }
        if (!isValueFilter && c == '[') {
            // Terminating opening brace. Consume it and return token.
            b.append((char) c);
            return b.toString();
        }
        if (isValueFilter && c == ']') {
            if (b.length() > 0) {
                // Do not consume the closing brace.
                reader.unread();
            } else {
                b.append((char) c);
            }
            return b.toString();
        }
        if (c == '-' || c == '_' || c == '.' || c == ':' || c == '$' || Character.isLetterOrDigit(c)) {
            b.append((char) c);
        } else {
            final String msg = String.format(
                    "Unexpected character '%s' at position %d for token starting at %d", (char) c,
                    reader.pos - 1, reader.mark);
            throw BadRequestException.invalidFilter(msg);
        }
        c = reader.read();
    }

    if (b.length() > 0) {
        return b.toString();
    }
    return null;
}