Example usage for java.io Reader close

List of usage examples for java.io Reader close

Introduction

In this page you can find the example usage for java.io Reader close.

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:org.callimachusproject.auth.DetachedRealm.java

private String readString(FileObject file) {
    try {/*from  ww w .ja  v a  2  s.  c o  m*/
        Reader reader = file.openReader(true);
        if (reader == null)
            return null;
        try {
            return new Scanner(reader).next();
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        logger.error(e.toString(), e);
        return null;
    }
}

From source file:com.bigdata.rdf.sail.webapp.AbstractTestNanoSparqlClient.java

protected static String getResponseBody(final HttpURLConnection conn) throws IOException {

    final Reader r = new InputStreamReader(conn.getInputStream());

    try {/*from  w w  w  . j a  v a 2 s .c  om*/

        final StringWriter w = new StringWriter();

        int ch;
        while ((ch = r.read()) != -1) {

            w.append((char) ch);

        }

        return w.toString();

    } finally {

        r.close();

    }

}

From source file:com.samebug.clients.search.api.client.SamebugClient.java

<T> T readJsonResponse(HttpResponse response, Type classOfT) throws HttpError, JsonParseException {
    InputStream content = null;//from  ww  w . ja v a  2s .co m
    Reader reader = null;
    try {
        content = response.getEntity().getContent();
        reader = new InputStreamReader(content);
        return gson.fromJson(reader, classOfT);
    } catch (com.google.gson.JsonParseException e) {
        throw new JsonParseException("Failed to parse json response", e);
    } catch (IOException e) {
        throw new HttpError(e);
    } finally {
        try {
            if (content != null)
                content.close();
            if (reader != null)
                reader.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:com.advdb.footballclub.FootBallClub.java

private void createOpponent(Session session) {

    Transaction transaction = null;/*from www  . ja  v a  2  s. c  o  m*/
    try {
        System.out.println("start createOpponent.");
        transaction = session.beginTransaction();
        Reader in = new FileReader("/Users/apichart/Documents/DW_opponent/DimOpponent-Table 1.csv");
        Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
        for (CSVRecord record : records) {
            String longName = record.get(2);
            String shortName = record.get(3);
            DimOpponent d = new DimOpponent(longName, shortName);
            session.save(d);
        }
        in.close();
        session.flush();
        session.clear();
        transaction.commit();

        System.out.println("finish createOpponent.");
    } catch (HibernateException e) {
        if (transaction != null) {
            transaction.rollback();
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FootBallClub.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(FootBallClub.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.stimulus.archiva.extraction.RTFExtractor.java

public Reader getText(InputStream is, Charset charset, IndexInfo indexInfo) throws ExtractionException {

    Reader reader = null;
    FileWriter writer = null;//from ww w .  j av a2  s  . c o m
    File file = null;
    try {
        reader = new InputStreamReader(is);
        file = File.createTempFile("extract_rtf", ".tmp");
        indexInfo.addDeleteFile(file);
        writer = new FileWriter(file);
        DefaultStyledDocument doc = new DefaultStyledDocument();
        new RTFEditorKit().read(reader, doc, 0);
        writer.write(doc.getText(0, doc.getLength()));
    } catch (Throwable ioe) {
        throw new ExtractionException("failed to parse rtf document", ioe, logger);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ioe) {
            }
        }

        if (writer != null) {
            try {
                writer.close();
            } catch (IOException ioe) {
            }
        }
    }
    try {
        Reader outReader = new FileReader(file);
        indexInfo.addReader(outReader);
        return outReader;
    } catch (Exception ex) {
        throw new ExtractionException("failed to extract text from powerpoint document", ex, logger,
                ChainedException.Level.DEBUG);
    }

}

From source file:edu.uthscsa.ric.papaya.builder.Builder.java

public void compressCSS(final File inputFile, final File outputFile, final YuiCompressorOptions o)
        throws IOException {
    Reader in = null;
    Writer out = null;/*w ww.  j a  va  2  s. c  om*/
    try {
        in = new InputStreamReader(new FileInputStream(inputFile), o.charset);

        final CssCompressor compressor = new CssCompressor(in);
        in.close();
        in = null;

        out = new OutputStreamWriter(new FileOutputStream(outputFile, true), o.charset);
        compressor.compress(out, o.lineBreakPos);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:com.lexicalintelligence.search.SearchRequest.java

public SearchResponse execute() {
    Reader reader = null;
    SearchResponse searchResponse = new SearchResponse(true);
    try {//  w  w  w .j  a va 2 s.c  om
        HttpResponse response = client.getHttpClient()
                .execute(new HttpGet(client.getUrl() + PATH + "?prefix=" + URLEncoder.encode(query, "UTF-8")));
        reader = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8);
        List<String> items = client.getObjectMapper().readValue(reader, new TypeReference<List<String>>() {
        });
        searchResponse.setItems(items);
    } catch (Exception e) {
        searchResponse = new SearchResponse(false);
        log.error(e);
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
            log.error(e);
        }
    }
    return searchResponse;
}

From source file:com.cotrino.langnet.GenerateVisualization.java

private void generateLanguages(String summaryFile, String languagesFile) throws IOException {

    HashMap<String, Integer> list = new HashMap<String, Integer>();
    Reader reader = new FileReader(summaryFile);
    CSVParser parser = new CSVParser(reader, csvFormat);
    for (CSVRecord record : parser) {
        String languageA = record.get("LanguageA");
        String languageB = record.get("LanguageB");
        int words = Integer.parseInt(record.get("ExecutedComparisons"));
        list.put(languageA, Math.max(words, list.getOrDefault(languageA, 0)));
        list.put(languageB, Math.max(words, list.getOrDefault(languageB, 0)));
    }// w w  w.  ja  v  a 2  s  . c  om
    parser.close();
    reader.close();

    String content = "Language;Words;Family;\n";
    for (String language : list.keySet()) {
        content += language + ";" + list.get(language) + ";Romance;\n";
    }
    IOUtil.write(languagesFile, content);
}

From source file:apiconnector.TestDataFunctionality.java

@Ignore
@Test//from w  ww. j  a v  a  2  s .  c  om
public void testGetDataAsCsv() throws Exception {
    //client_read.setVerboseLevel(1);
    Random random = new Random();

    Map<String, String> filters = new TreeMap<String, String>();
    filters.put("tag", "study_14");

    DataSet[] all = client_read.dataList(filters).getData();

    for (int i = 0; i < 5;) {
        DataSet current = all[random.nextInt(all.length)];

        String numInst = current.getQualityMap().get("NumberOfInstances");

        if (current.getFileId() == null || !current.getFormat().toLowerCase().equals("arff")) {
            continue;
        }

        String fullUrl = url + "data/get_csv/" + current.getFileId() + "/" + current.getName() + ".csv";
        System.out.println(fullUrl);
        final URL url = new URL(fullUrl);
        final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
        final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT);
        try {
            if (numInst != null) {
                int numberOfInstances = (int) Double.parseDouble(numInst);
                assertEquals(parser.getRecords().size(), numberOfInstances);
            }
        } finally {
            parser.close();
            reader.close();
        }

        // important
        i += 1;
    }

}