Example usage for java.util Scanner hasNext

List of usage examples for java.util Scanner hasNext

Introduction

In this page you can find the example usage for java.util Scanner hasNext.

Prototype

public boolean hasNext() 

Source Link

Document

Returns true if this scanner has another token in its input.

Usage

From source file:org.rexify.eclipse.helper.InternetHelper.java

static public String convertStreamToString(InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

From source file:com.switchfly.inputvalidation.sanitizer.UrlStripHtmlSanitizer.java

private static void parse(List<NameValuePair> parameters, Scanner scanner) {
    scanner.useDelimiter(PARAMETER_SEPARATOR);
    while (scanner.hasNext()) {
        String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR, 2);
        if (nameValue.length == 0 || nameValue.length > 2) {
            throw new IllegalArgumentException("bad parameter");
        }//  w  ww  .j  a  va  2 s.  c o m
        String name = ValidationStrategy.PROPERTY_NAME.cleanStrategy().validate(nameValue[0]);
        String value = null;
        if (nameValue.length == 2) {
            value = ValidationStrategy.DEFAULT_WEB_CONTENT_VALIDATION.validate(nameValue[1]);
        }
        parameters.add(new BasicNameValuePair(name, value));
    }
}

From source file:org.camunda.bpm.elasticsearch.util.JsonHelper.java

public static String readJsonFromClasspathAsString(String fileName) {
    InputStream inputStream = null;

    try {/*  w w w .j av  a  2  s . co m*/
        inputStream = IoUtil.getResourceAsStream(fileName);
        if (inputStream == null) {
            throw new RuntimeException("File '" + fileName + "' not found!");
        }

        Scanner s = new java.util.Scanner(inputStream, "UTF-8").useDelimiter("\\A");
        return s.hasNext() ? s.next() : null;
    } finally {
        IoUtil.closeSilently(inputStream);
    }
}

From source file:Main.java

/**
 * Adds all parameters within the Scanner to the list of <code>parameters</code>.
 * For example,a scanner containing the string <code>a=1&b=2&c=3</code> would
 * add the {@link org.apache.http.NameValuePair NameValuePairs} a=1, b=2, and c=3 to the
 * list of parameters./*from  w ww. j  a  v a  2 s .co m*/
 *
 * @param parameters List to add parameters to.
 * @param scanner    Input that contains the parameters to parse.
 */
public static void parse(final List<NameValuePair> parameters, final Scanner scanner) {
    scanner.useDelimiter(PARAMETER_SEPARATOR);
    while (scanner.hasNext()) {
        String name = null;
        String value = null;
        String token = scanner.next();
        int i = token.indexOf(NAME_VALUE_SEPARATOR);
        if (i != -1) {
            name = token.substring(0, i).trim();
            value = token.substring(i + 1).trim();
        } else {
            name = token.trim();
        }
        parameters.add(new BasicNameValuePair(name, value));
    }
}

From source file:com.msopentech.thali.utilities.webviewbridge.BridgeManager.java

/**
 * A useful utility for small streams. The stream will be closed by this method.
 *
 * Taken from http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
 * @param inputStream//from   w  w w  .  ja  v  a 2s  .  c o m
 * @return Stream as a string
 */
public static String turnUTF8InputStreamToString(InputStream inputStream) {
    try {
        Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A");
        return scanner.hasNext() ? scanner.next() : "";
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

/**
 * Adds all parameters within the Scanner to the list of
 * <code>parameters</code>, as encoded by <code>encoding</code>. For
 * example, a scanner containing the string <code>a=1&b=2&c=3</code> would
 * add the {@link Pair<String, String> NameValuePairs} a=1, b=2, and c=3 to the
 * list of parameters./*from   w w w .ja v  a2s  .  c  om*/
 *
 * @param parameters
 *            List to add parameters to.
 * @param scanner
 *            Input that contains the parameters to parse.
 * @param encoding
 *            Encoding to use when decoding the parameters.
 */
public static void parse(final List<Pair<String, String>> parameters, final Scanner scanner,
        final String encoding) {
    scanner.useDelimiter(PARAMETER_SEPARATOR);
    while (scanner.hasNext()) {
        final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
        if (nameValue.length == 0 || nameValue.length > 2)
            throw new IllegalArgumentException("bad parameter");

        final String name = decode(nameValue[0], encoding);
        String value = null;
        if (nameValue.length == 2)
            value = decode(nameValue[1], encoding);
        parameters.add(new Pair<String, String>(name, value));
    }
}

From source file:pl.lewica.util.FileUtil.java

/**
 * Reads contents of a stream (e.g. SQL script) and splits it into separate statements.
 * IMPORTANT: The assumption is the statements are delimited by semicolons followed by new lines.
 * //  ww  w  . ja v  a 2 s .  c o  m
 * If you are using this method to convert a string with multiple SQL queries to individual statements,
 * make sure the semicolon-new line sequence doesn't exist anywhere inside the SQL statements, perhaps
 * somewhere in the middle of long varchars or text fields as they would be treated as SQL statement
 * delimiters and you would therefore get unexpected results. 
 * 
 * The method might be useful on Android that is unable to e.g. create multiple tables in one go.
 */
public static List<String> convertStreamToStrings(InputStream is, String delimiter) {
    List<String> result = new ArrayList<String>();
    Scanner s = new Scanner(is);
    s.useDelimiter(delimiter);

    while (s.hasNext()) {
        String line = s.next().trim();
        if (line.length() > 0) {
            result.add(line);
        }
    }
    s.close();

    return result;
}

From source file:jmap2gml.ItemImage.java

private static HashMap<String, Image> readConfig() {
    HashMap<String, Image> out = new HashMap<>();
    JSONTEXT = "";
    Image img;/*from   w ww . j  a va 2s.  c o m*/

    Scanner scan;
    try {
        scan = new Scanner(new File("ImageItemConfig"));

        while (scan.hasNext()) {
            JSONTEXT += scan.nextLine();
        }

        config = new JSONObject(JSONTEXT);

        for (String str : config.keySet()) {
            if (!str.contains("XOFFSET") && !str.contains("YOFFSET")) {
                img = (new ImageIcon(config.getString(str))).getImage();
                out.put(str, img);
            }
        }
    } catch (Exception ex) {
        Logger.getLogger(ItemImage.class.getName()).log(Level.SEVERE, null, ex);
    }

    return out;
}

From source file:com.github.opengarageapp.GarageService.java

protected static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is, "UTF-8").useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

From source file:com.noisepages.nettoyeur.usb.DeviceInfo.java

private static String getName(String url) throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);
    Scanner scanner = new Scanner(response.getEntity().getContent());
    while (scanner.hasNext()) {
        String line = scanner.nextLine();
        int start = line.indexOf("Name:") + 6;
        if (start > 5) {
            int end = line.indexOf("<", start);
            if (end > start) {
                return line.substring(start, end);
            }// w  w w .  j ava2 s  .  c o m
        }
    }
    return null;
}