Example usage for java.util Scanner close

List of usage examples for java.util Scanner close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes this scanner.

Usage

From source file:com.mirth.connect.client.ui.components.rsta.ac.js.PartialHashMap.java

private String[] splitKey(String key) {
    List<String> list = new ArrayList<String>();

    Scanner scanner = new Scanner(key);
    scanner.useDelimiter(SPLIT_PATTERN);
    while (scanner.hasNext()) {
        list.add(scanner.next().toLowerCase());
    }// www  .j av a  2  s. c o  m
    scanner.close();

    return list.toArray(new String[list.size()]);
}

From source file:edu.harvard.hul.ois.fits.junit.VideoStdSchemaTestXmlUnit_NoMD5.java

@Test
public void testVideoXmlUnitFitsOutput_AVC_NO_MD5() throws Exception {

    Fits fits = new Fits();

    // First generate the FITS output
    File input = new File("testfiles/FITS-SAMPLE-44_1_1_4_4_4_6_1_1_2_3_1.mp4");
    FitsOutput fitsOut = fits.examine(input);

    XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat());
    String actualXmlStr = serializer.outputString(fitsOut.getFitsXml());

    // Read in the expected XML file
    Scanner scan = new Scanner(
            new File("testfiles/output/FITS-SAMPLE-44_1_1_4_4_4_6_1_1_2_3_1_mp4_FITS_NO_MD5.xml"));
    String expectedXmlStr = scan.useDelimiter("\\Z").next();
    scan.close();

    // Set up XMLUnit
    XMLUnit.setIgnoreWhitespace(true);//w  ww.  j av a  2s  . com
    XMLUnit.setNormalizeWhitespace(true);

    Diff diff = new Diff(expectedXmlStr, actualXmlStr);

    // Initialize attributes or elements to ignore for difference checking
    diff.overrideDifferenceListener(new IgnoreNamedElementsDifferenceListener("version", "toolversion",
            "dateModified", "fslastmodified", "startDate", "startTime", "timestamp", "fitsExecutionTime",
            "executionTime", "filepath", "location"));

    DetailedDiff detailedDiff = new DetailedDiff(diff);

    // Display any Differences
    List<Difference> diffs = detailedDiff.getAllDifferences();
    if (!diff.identical()) {
        StringBuffer differenceDescription = new StringBuffer();
        differenceDescription.append(diffs.size()).append(" differences");

        System.out.println(differenceDescription.toString());
        for (Difference difference : diffs) {
            System.out.println(difference.toString());
        }

    }

    assertTrue("Differences in XML", diff.identical());
}

From source file:net.bashtech.geobot.BotManager.java

public static String postRemoteDataSongRequest(String urlString, String channel, String requester) {
    if (BotManager.getInstance().CoeBotTVAPIKey.length() > 4) {
        URL url;/*w  ww .  j  av a2  s.  com*/
        HttpURLConnection conn;

        try {
            url = new URL("http://coebot.tv/api/v1/reqsongs/add/" + channel.toLowerCase() + "$"
                    + BotManager.getInstance().CoeBotTVAPIKey + "$" + BotManager.getInstance().nick);

            String postData = "url=" + URLEncoder.encode(urlString, "UTF-8") + "&requestedBy="
                    + URLEncoder.encode(requester, "UTF-8");
            conn = (HttpURLConnection) url.openConnection();
            System.out.println(postData);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("User-Agent", "CoeBot");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length));

            // conn.setConnectTimeout(5 * 1000);
            // conn.setReadTimeout(5 * 1000);

            PrintWriter out = new PrintWriter(conn.getOutputStream());
            out.print(postData);
            out.close();
            String response = "";
            if (conn.getResponseCode() < 400) {

                Scanner inStream = new Scanner(conn.getInputStream());

                while (inStream.hasNextLine()) {
                    response += (inStream.nextLine());
                }
                inStream.close();
            } else {
                Scanner inStream = new Scanner(conn.getErrorStream());

                while (inStream.hasNextLine()) {
                    response += (inStream.nextLine());
                }
                inStream.close();
            }
            System.out.println(response);

            return response;

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();

        }

        return null;
    } else {
        return null;
    }

}

From source file:org.envirocar.harvest.TrackPublisher.java

protected String readContent(InputStream content) throws IOException {
    Scanner sc = new Scanner(content);
    StringBuilder sb = new StringBuilder(content.available());
    while (sc.hasNext()) {
        sb.append(sc.nextLine());//from  www  .  jav a2 s  .  co m
    }
    sc.close();
    return sb.toString();
}

From source file:csns.importer.parser.csula.RosterParserImpl.java

@Override
public List<ImportedUser> parse(String text) {
    Scanner scanner = new Scanner(text);
    scanner.useDelimiter("\\s+|\\r\\n|\\r|\\n");
    String first = scanner.next();
    scanner.close();

    return first.equals("1") ? parse1(text) : parse2(text);
}

From source file:com.networknt.light.server.handler.loader.MenuLoader.java

private static void loadMenuFile(File file) {
    Scanner scan = null;
    try {//from   w  ww  . j a  va2 s .com
        scan = new Scanner(file, Loader.encoding);
        // the content is only the data portion. convert to map
        String content = scan.useDelimiter("\\Z").next();
        HttpPost httpPost = new HttpPost("http://injector:8080/api/rs");
        StringEntity input = new StringEntity(content);
        input.setContentType("application/json");
        httpPost.setEntity(input);
        CloseableHttpResponse response = httpclient.execute(httpPost);

        try {
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
            String json = "";
            String line = "";
            while ((line = rd.readLine()) != null) {
                json = json + line;
            }
            System.out.println("json = " + json);
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (scan != null)
            scan.close();
    }
}

From source file:me.figo.FigoConnection.java

/***
 * Handle the response of a request by decoding its JSON payload
 * @param stream Stream containing the JSON data
 * @param typeOfT Type of the data to be expected
 * @return Decoded data//from   w ww .ja va 2  s.c o m
 */
private <T> T handleResponse(InputStream stream, Type typeOfT) {
    // check whether decoding is actual requested
    if (typeOfT == null)
        return null;

    // read stream body
    Scanner s = new Scanner(stream, "UTF-8");
    s.useDelimiter("\\A");
    String body = s.hasNext() ? s.next() : "";
    s.close();

    // decode JSON payload
    Gson gson = GsonAdapter.createGson();
    return gson.fromJson(body, typeOfT);
}

From source file:ThreadPoolTest.java

/**
 * Searches a file for a given keyword.//from w  ww.  j av a  2s  . c om
 * 
 * @param file
 *          the file to search
 * @return true if the keyword is contained in the file
 */
public boolean search(File file) {
    try {
        Scanner in = new Scanner(new FileInputStream(file));
        boolean found = false;
        while (!found && in.hasNextLine()) {
            String line = in.nextLine();
            if (line.contains(keyword))
                found = true;
        }
        in.close();
        return found;
    } catch (IOException e) {
        return false;
    }
}

From source file:com.greenpepper.html.HtmlExample.java

private String firstPattern(String tags) {
    Scanner scanner = new Scanner(tags);
    try {//from ww  w  .  j a v  a  2 s.c o  m
        return scanner.next();
    } finally {
        scanner.close();
    }
}

From source file:com.palantir.gerrit.gerritci.servlets.JobsServlet.java

public static ArrayList<String> updateProjectJobFiles(File projectFile, File projectConfigDirectory,
        ArrayList<String> receivedJobNames) throws IOException {

    Scanner scanner = new Scanner(projectFile);

    ArrayList<String> updatedJobs = new ArrayList<String>();
    ArrayList<String> newJobs = new ArrayList<String>();
    ArrayList<String> deletedJobs = new ArrayList<String>();

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if (receivedJobNames.contains(line)) {
            updatedJobs.add(line);//  w  w  w .  jav  a2  s.c o m
            receivedJobNames.remove(line);
        } else {
            deletedJobs.add(line);
        }
    }
    logger.info("There are " + receivedJobNames.size() + " new jobs");
    logger.info("There are " + deletedJobs.size() + " deleted jobs");
    logger.info("There are " + updatedJobs.size() + " updated jobs");
    for (String s : receivedJobNames) {
        newJobs.add(s);
    }

    scanner.close();
    FileWriter writer = new FileWriter(projectFile, false);
    for (String s : updatedJobs) {
        writer.write(s);
        writer.write("\n");
    }
    for (String s : newJobs) {
        writer.write(s);
        writer.write("\n");
    }
    writer.close();
    return deletedJobs;
}