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:edu.harvard.hul.ois.fits.junit.VideoStdSchemaTestXmlUnit.java

@Test
public void testVideoXmlUnitFitsOutput_AVC() 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.xml"));
    String expectedXmlStr = scan.useDelimiter("\\Z").next();
    scan.close();

    // Set up XMLUnit
    XMLUnit.setIgnoreWhitespace(true);//from w ww  .ja  v a2s .co  m
    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:com.github.matthesrieke.realty.CrawlerServlet.java

private void readCrawlingLinks() {
    InputStream is = getClass().getResourceAsStream("/links.txt");
    if (is != null) {
        Scanner sc = new Scanner(is);
        String l;/*w  w w  .j  a v a 2 s.  co m*/
        while (sc.hasNext()) {
            l = sc.nextLine().trim();
            if (!l.startsWith("#")) {
                this.crawlLinks.add(l);
            }
        }
        sc.close();
    }
}

From source file:org.apache.uima.ruta.resource.CSVTable.java

private void buildTable(InputStream stream) {
    Scanner sc = new Scanner(stream, Charset.forName("UTF-8").name());
    sc.useDelimiter("\\n");
    tableData = new ArrayList<List<String>>();
    while (sc.hasNext()) {
        String line = sc.next().trim();
        line = line.replaceAll(";;", "; ;");
        String[] lineElements = line.split(";");
        List<String> row = Arrays.asList(lineElements);
        tableData.add(row);/*from w ww  .j  a  v a  2  s .c o m*/
    }
    sc.close();
}

From source file:org.kuali.mobility.knowledgebase.service.KnowledgeBaseServiceImpl.java

private String inputStreamToString(InputStream in) {
    StringBuffer sb = new StringBuffer();
    try {// ww  w  .ja  v  a  2  s.co m
        Scanner scanner = new Scanner(in);
        try {
            while (scanner.hasNextLine()) {
                sb.append(scanner.nextLine() + "\r\n");
            }
        } finally {
            scanner.close();
        }
    } catch (Exception e) {
        //         LOG.error("Error retrieving XSL: ", e);
    }
    return sb.toString();
}

From source file:com.continuent.tungsten.common.security.KeystoreManagerCtrl.java

/**
 * Reads user input from stdin//from ww w. j av  a2 s .  c o  m
 * 
 * @param listPrompts list of prompts to display, asking for user input
 * @return a list containing user inputs
 */
private List<String> getUserInputFromStdin(List<String> listPrompts) {
    List<String> listUserInput = new ArrayList<String>();

    Scanner console = new Scanner(System.in);
    Scanner lineTokenizer = null;

    for (String prompt : listPrompts) {
        System.out.println(prompt);

        try {
            lineTokenizer = new Scanner(console.nextLine());
        } catch (NoSuchElementException nse) {
            console.close();
            throw new NoSuchElementException(MessageFormat.format("Missing user input=  {0}", prompt));
        }

        if (lineTokenizer.hasNext()) {
            String userInput = lineTokenizer.next();
            listUserInput.add(userInput);
        }
    }

    console.close();
    return listUserInput;
}

From source file:br.com.sicoob.cro.cop.batch.core.FileReadTasklet.java

@Override
public void process() {
    try {/*  w w w  .jav a  2s . c om*/
        List<Operation> operacoes = new ArrayList();
        InputStream source = this.getClass()
                .getResourceAsStream(this.context.getParameters().get("nomeArquivo").toString());
        Scanner scan = new Scanner(source);
        while (scan.hasNext()) {
            String[] dados = scan.next().split(";");
            Operation operacao = new Operation(dados[0], new BigDecimal(dados[1]), dados[2],
                    new BigDecimal(dados[3]));
            operacoes.add(operacao);
        }

        // calcula o percentual de provisionamento
        for (Operation operacao : operacoes) {
            LOG.info(operacao.toString());
        }
        scan.close();
        source.close();
    } catch (IOException ex) {
        Logger.getLogger(FileReadTasklet.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:de.betterform.agent.web.resources.ResourceServlet.java

private long getLastModifiedValue() {
    if (this.lastModified == 0) {
        long bfTimestamp;
        try {/*from  w  ww  . j  a v a 2s.co m*/
            String path = WebFactory.getRealPath("/WEB-INF/betterform-version.info", this.getServletContext());
            StringBuilder versionInfo = new StringBuilder();
            String NL = System.getProperty("line.separator");
            Scanner scanner = new Scanner(new FileInputStream(path), "UTF-8");
            try {
                while (scanner.hasNextLine()) {
                    versionInfo.append(scanner.nextLine() + NL);
                }
            } finally {
                scanner.close();
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("VersionInfo: " + versionInfo);
            }
            // String APP_NAME = APP_INFO.substring(0, APP_INFO.indexOf(" "));
            // String APP_VERSION = APP_INFO.substring(APP_INFO.indexOf(" ") + 1, APP_INFO.indexOf("-") - 1);
            String timestamp = versionInfo.substring(versionInfo.indexOf("Timestamp:") + 10,
                    versionInfo.length());
            String df = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(df);
            Date date = sdf.parse(timestamp);
            bfTimestamp = date.getTime();
        } catch (Exception e) {
            LOG.error("Error setting HTTP Header 'Last Modified', could not parse the given date.");
            bfTimestamp = 0;
        }
        this.lastModified = bfTimestamp;
    }
    return lastModified;
}

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

@Test
public void testVideoXmlUnitCombinedOutput_AVC() throws Exception {

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

    // Output stream for FITS to write to 
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    // Create combined output in the stream passed in
    Fits.outputStandardCombinedFormat(fitsOut, out);

    // Turn output stream into a String HtmlUnit can use
    String actualXmlStr = new String(out.toByteArray(), "UTF-8");

    // 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_Combined.xml"));
    String expectedXmlStr = scan.useDelimiter("\\Z").next();
    scan.close();

    // Set up XMLUnit
    XMLUnit.setIgnoreWhitespace(true);//from  w  ww.j  av  a2 s .  co  m
    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", "ebucore:locator"));

    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:uk.ac.ebi.phenotype.web.util.BioMartBot.java

public String getQueryFromFile(String filename) {

    StringBuilder text = new StringBuilder();
    String NL = System.getProperty("line.separator");
    Scanner scanner = null;

    try {//from  w w w .jav  a2  s  .c o  m
        scanner = new Scanner(new FileInputStream(filename));

        while (scanner.hasNextLine()) {
            text.append(scanner.nextLine() + NL);
        }
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } finally {
        scanner.close();
    }
    return text.toString();
}

From source file:io.selendroid.standalone.android.impl.DefaultAndroidEmulator.java

private static Map<String, Integer> mapDeviceNamesToSerial() {
    Map<String, Integer> mapping = new HashMap<String, Integer>();
    CommandLine command = new CommandLine(AndroidSdk.adb());
    command.addArgument("devices");
    Scanner scanner;
    try {/*  w w  w .j  av  a2s.co m*/
        scanner = new Scanner(ShellCommand.exec(command));
    } catch (ShellCommandException e) {
        return mapping;
    }
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        Pattern pattern = Pattern.compile("emulator-\\d\\d\\d\\d");
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            String serial = matcher.group(0);

            Integer port = Integer.valueOf(serial.replaceAll("emulator-", ""));
            TelnetClient client = null;
            try {
                client = new TelnetClient(port);
                String avdName = client.sendCommand("avd name");
                mapping.put(avdName, port);
            } catch (AndroidDeviceException e) {
                // ignore
            } finally {
                if (client != null) {
                    client.close();
                }
            }
        }
    }
    scanner.close();

    return mapping;
}