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.sf.jaceko.mock.resource.BasicSetupResource.java

static Map<String, String> parseHeadersToPrime(String headersToPrime) {
    Map<String, String> headersMap = new HashMap<String, String>();

    if (headersToPrime != null) {
        Scanner headersScanner = new Scanner(headersToPrime).useDelimiter(HEADERS_DELIMITER);
        while (headersScanner.hasNext()) {
            String header = headersScanner.next();
            String[] headerPart = header.split(HEADER_DELIMITER);
            String headerName = headerPart[HEADER_KEY_INDEX];
            String headerValue = headerPart[HEADER_VALUE_INDEX];
            headersMap.put(headerName, headerValue);
        }/*from  w w  w  . j a va  2  s.c o  m*/
    }

    return headersMap;
}

From source file:com.microsoft.tooling.msservices.helpers.ServiceCodeReferenceHelper.java

public static String getStringAndCloseStream(InputStream is) throws IOException {
    //Using the trick described in this link to read whole streams in one operation:
    //http://stackoverflow.com/a/5445161
    try {/*from  ww  w. jav  a  2s .  c om*/
        Scanner s = new Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    } finally {
        is.close();
    }
}

From source file:nu.yona.server.rest.RestClientErrorHandler.java

private static String convertStreamToString(InputStream is) {
    @SuppressWarnings("resource") // It's not on us to close this stream
    java.util.Scanner s = new Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

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

public static Set<String> readConfigFilePerLine(String resourcePath) throws IOException {
    URL resURL = Util.class.getResource(resourcePath);
    URLConnection resConn = resURL.openConnection();
    resConn.setUseCaches(false);/*from  w w  w.  j a  va2  s  .  c  o m*/
    InputStream contents = resConn.getInputStream();

    Scanner sc = new Scanner(contents);
    Set<String> result = new HashSet<String>();
    String line;
    while (sc.hasNext()) {
        line = sc.nextLine();
        if (line != null && !line.isEmpty() && !line.startsWith("#")) {
            result.add(line.trim());
        }
    }
    sc.close();

    return result;
}

From source file:com.microsoft.tooling.msservices.helpers.ServiceCodeReferenceHelper.java

@NotNull
public static String getStringAndCloseStream(@NotNull InputStream is, @NotNull String charsetName)
        throws IOException {
    //Using the trick described in this link to read whole streams in one operation:
    //http://stackoverflow.com/a/5445161
    try {//from   w  w w. j a v  a2 s  .c  o  m
        Scanner s = new Scanner(is, charsetName).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    } finally {
        is.close();
    }
}

From source file:org.jenkinsci.test.acceptance.docker.fixtures.JavaContainerTest.java

@BeforeClass
public static void cleanOldImages() throws Exception {
    Process dockerImages;/* w  w w . j  a  va 2  s  .  c o  m*/
    try {
        dockerImages = new ProcessBuilder("docker", "images", "--format", "{{.Repository}}:{{.Tag}}").start();
    } catch (IOException x) {
        throw new AssumptionViolatedException("could not run docker", x);
    }
    Scanner scanner = new Scanner(dockerImages.getInputStream());
    List<String> toRemove = new ArrayList<>();
    while (scanner.hasNext()) {
        String image = scanner.next();
        if (image.startsWith("jenkins/")) {
            toRemove.add(image);
        }
    }
    dockerImages.waitFor();
    if (!toRemove.isEmpty()) {
        toRemove.add(0, "docker");
        toRemove.add(1, "rmi");
        Process dockerRmi = new ProcessBuilder(toRemove).redirectErrorStream(true).start();
        // Cannot use inheritIO from Surefire.
        IOUtils.copy(dockerRmi.getInputStream(), System.err);
        dockerRmi.waitFor();
    }
}

From source file:cpd3314.buildit12.CPD3314BuildIt12.java

/**
 * Build a sample method that saves a handful of car instances as Serialized
 * objects, and as JSON objects, then re-open the saved versions in a
 * different method/*from   w  w  w  . j  a  va  2s.co m*/
 */
public static void doBuildIt2Input() {
    // Unserialize the Objects -- Save them to an ArrayList and Output
    try {
        FileInputStream objFile = new FileInputStream("cars.obj");
        ObjectInputStream objStream = new ObjectInputStream(objFile);

        ArrayList<Car> cars = new ArrayList<>();

        boolean eof = false;
        while (!eof) {
            try {
                cars.add((Car) objStream.readObject());
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
            } catch (EOFException ex) {
                // We must catch the EOF exception to leave the loop
                eof = true;
            }
        }

        System.out.println("Unserialized Data:");
        for (Car car : cars) {
            System.out.printf("Make: %s, Model: %s, Year: %d\n", car.getMake(), car.getModel(), car.getYear());
        }
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Read from JSON and Output
    try {
        File file = new File("cars.json");
        Scanner input = new Scanner(file);
        while (input.hasNext()) {
            JSONParser parse = new JSONParser();
            try {
                // This should be the root JSON Object, which has an array of Car objects
                JSONObject json = (JSONObject) parse.parse(input.nextLine());

                // We pull the array out to work with it
                JSONArray cars = (JSONArray) json.get("cars");

                System.out.println("Un-JSON-ed Data:");
                for (Object car : cars) {
                    // Convert each Object to a JSONObject
                    JSONObject carJSON = (JSONObject) car;
                    // Convert each JSONObject to an actual Car
                    Car carObj = new Car(carJSON);
                    // Output from the Actual Car class                        
                    System.out.printf("Make: %s, Model: %s, Year: %d\n", carObj.getMake(), carObj.getModel(),
                            carObj.getYear());
                }
            } catch (ParseException ex) {
                Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.calamp.services.kinesis.events.writer.CalAmpEventWriter.java

public static List<CalAmpEvent> readEventsFromFile(String filePath) throws IOException {
    // Repeatedly send stock trades with a some milliseconds wait in between
    ArrayList<CalAmpEvent> eList = new ArrayList<CalAmpEvent>();
    Scanner scan = new Scanner(new FileReader(filePath));
    int count = 0;
    while (scan.hasNext()) {
        CalAmpEvent cae = CalAmpEvent.fromJsonAsString(scan.nextLine());
        eList.add(cae);/*from ww w. j  av a2s.co  m*/

        if (count % CalAmpParameters.maxRecordsPerPut == 0) {
            System.out.println(cae.toJsonAsString());
        }
        count++;
        //System.out.println( CalAmpEvent.fromJsonAsString( cae.toJsonAsString()) );
    }
    if (scan != null) {
        scan.close();
    }
    return eList;
}

From source file:nl.esciencecenter.xenon.adaptors.schedulers.gridengine.GridEngineXmlParserTest.java

private static String readFile(String pathName) {
    InputStream is = GridEngineXmlParserTest.class.getResourceAsStream(pathName);
    // we read until end of file, delimited by \\A
    Scanner s = new Scanner(is, "UTF-8").useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

From source file:Main.java

/**
 * This API compares if two files content is identical. It ignores extra
 * spaces and new lines while comparing//ww  w  .j  av a2  s  .c  o  m
 *
 * @param sourceFile
 * @param destFile
 * @return
 * @throws Exception
 */
public static boolean isFilesIdentical(URL sourceFile, File destFile) throws Exception {
    try {
        Scanner sourceFileScanner = new Scanner(sourceFile.openStream());
        Scanner destFileScanner = new Scanner(destFile);

        while (sourceFileScanner.hasNext()) {
            /*
             * If source file is having next token then destination file
             * also should have next token, else they are not identical.
             */
            if (!destFileScanner.hasNext()) {
                destFileScanner.close();
                sourceFileScanner.close();
                return false;
            }
            if (!sourceFileScanner.next().equals(destFileScanner.next())) {
                sourceFileScanner.close();
                destFileScanner.close();
                return false;
            }
        }
        /*
         * Handling the case where source file is empty and destination file
         * is having text
         */
        if (destFileScanner.hasNext()) {
            destFileScanner.close();
            sourceFileScanner.close();
            return false;
        } else {
            destFileScanner.close();
            sourceFileScanner.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } /*finally {
      sourceFile.close();
      }*/
}