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:net.ftb.util.AppUtils.java

/**
 * Reads all of the data from the given stream and returns it as a string.
 * @param stream the stream to read from.
 * @return the data read from the given stream as a string.
 *//*from   w  w w . j av  a 2 s  .co  m*/
public static String readString(InputStream stream) {
    Scanner scanner = new Scanner(stream).useDelimiter("\\A");
    return scanner.hasNext() ? scanner.next() : "";
}

From source file:org.lightcouch.CouchDbUtil.java

public static String streamToString(InputStream in) {
    Scanner s = new Scanner(in).useDelimiter("\\A");
    String str = s.hasNext() ? s.next() : null;
    close(in);/* ww w . j  av a  2 s .  c om*/
    return str;
}

From source file:com.alibaba.openapi.client.util.URLEncodedUtils.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 NameValuePair NameValuePairs} a=1, b=2, and c=3 to the
 * list of parameters./* w  w  w  .  j a  va 2  s.  c o  m*/
 * 
 * @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<NameValuePair> 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 BasicNameValuePair(name, value));
    }
}

From source file:org.omg.bpmn.miwg.testresult.TestResults.java

public static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = null;
    try {// w ww .ja v  a 2s . c  o  m
        s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    } finally {
        s.close();
    }
}

From source file:de.ifgi.mosia.wpswfs.Util.java

public static String readContent(InputStream is, String enc) {
    if (is == null) {
        return null;
    }//from  w  w w.  j  a  va  2 s .com

    Scanner sc = new Scanner(is, enc == null ? CharEncoding.ISO_8859_1 : enc);
    StringBuilder sb = new StringBuilder();

    String sep = System.getProperty("line.separator");
    while (sc.hasNext()) {
        sb.append(sc.nextLine());
        sb.append(sep);
    }

    sc.close();
    return sb.toString();
}

From source file:apiPull.getItem.java

public static ArrayList numberOfIds() {
    // go to gw2 https://api.guildwars2.com/v2/items and get a list of items
    // return the arraylist, use to loop through implimentations

    ArrayList ids = new ArrayList();

    // Make a URL to the web page
    URL url = null;//  w w w .  ja  va  2 s . c om
    try {
        url = new URL("https://api.guildwars2.com/v2/items");
    } catch (MalformedURLException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Get the input stream through URL Connection
    URLConnection con = null;
    try {
        con = url.openConnection();
    } catch (IOException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }
    InputStream is = null;
    try {
        is = con.getInputStream();
        // Once you have the Input Stream, it's just plain old Java IO stuff.
        // For this case, since you are interested in getting plain-text web page
        // I'll use a reader and output the text content to System.out.
        // For binary content, it's better to directly read the bytes from stream and write
        // to the target file.

    } catch (IOException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line = null;

    try {
        // read each line 
        while ((line = br.readLine()) != null) {

            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");
            while (scanner.hasNext()) {
                ids.add(scanner.next());
            }
            scanner.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }

    return ids;
}

From source file:SerialPortHandler.java

public static String convertStreamToStringIn(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    System.out.println("Made it here!!");
    return s.hasNext() ? s.next() : "";
}

From source file:com.doplgangr.secrecy.utils.Util.java

public static Map<String, File> getAllStorageLocations() {
    Map<String, File> map = new TreeMap<String, File>();

    List<String> mMounts = new ArrayList<String>(99);
    //List<String> mVold = new ArrayList<String>(99);
    mMounts.add(Environment.getExternalStorageDirectory().getAbsolutePath());
    try {/* ww w.  j  ava  2 s .c om*/
        File mountFile = new File("/proc/mounts");
        if (mountFile.exists()) {
            Scanner scanner = new Scanner(mountFile);
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                //if (line.startsWith("/dev/block/vold/")) {
                String[] lineElements = line.split(" ");
                String element = lineElements[1];
                mMounts.add(element);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    List<String> mountHash = new ArrayList<String>(99);

    for (String mount : mMounts) {
        File root = new File(mount);
        Util.log(mount, "is checked");
        Util.log(mount, root.exists(), root.isDirectory(), canWrite(root));
        if (canWrite(root)) {
            Util.log(mount, "is writable");
            File[] list = root.listFiles();
            String hash = "[";
            if (list != null)
                for (File f : list)
                    hash += f.getName().hashCode() + ":" + f.length() + ", ";
            hash += "]";
            if (!mountHash.contains(hash)) {
                String key = root.getAbsolutePath() + " ("
                        + org.apache.commons.io.FileUtils.byteCountToDisplaySize(root.getUsableSpace())
                        + " free space)";
                mountHash.add(hash);
                map.put(key, root);
            }
        }
    }

    mMounts.clear();
    return map;
}

From source file:cz.autoclient.github.json.GitHubJson.java

public static JSONObject fromURL(URL url) {
    Scanner s;
    try {//from w w  w  .j  av  a2  s . c  o m
        s = new Scanner(url.openStream(), "UTF-8");
    } catch (IOException ex) {
        throw new IllegalArgumentException("JSON file not found at " + url);
    }
    s.useDelimiter("\\A");
    if (s.hasNext()) {
        String out = s.next();
        try {
            return new JSONObject(out);
        } catch (JSONException ex) {
            throw new IllegalArgumentException(
                    "Invalid JSON contents at " + url + " - \n         JSON error:" + ex);
            //Logger.getLogger(DataLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else
        throw new IllegalArgumentException("JSON file not found at " + url);
}

From source file:com.yoavst.quickapps.URLEncodedUtils.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 NameValuePair NameValuePairs} a=1, b=2, and c=3 to the
 * list of parameters.//from   ww w.  ja  va  2s. com
 *
 * @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<NameValuePair> 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 BasicNameValuePair(name, value));
    }
}