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:de.schildbach.wallet.elysium.ExchangeRatesProvider.java

private static Map<String, ExchangeRate> getElysiumCharts() {
    final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();
    // Keep the BTC rate around for a bit
    Double btcRate = 0.0;/*  w w w .  ja  va2 s . co m*/
    try {
        String currencies[] = { "USD", "BTC", "RUR" };
        String urls[] = { "https://btc-e.com/api/2/14/ticker", "https://btc-e.com/api/2/10/ticker",
                "https://btc-e.com/api/2/elsm_rur/ticker" };
        for (int i = 0; i < currencies.length; ++i) {
            final String currencyCode = currencies[i];
            final URL URL = new URL(urls[i]);
            final URLConnection connection = URL.openConnection();
            connection.setConnectTimeout(TIMEOUT_MS);
            connection.setReadTimeout(TIMEOUT_MS);
            connection.connect();
            final StringBuilder content = new StringBuilder();

            Reader reader = null;
            try {
                reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024));
                IOUtils.copy(reader, content);
                final JSONObject head = new JSONObject(content.toString());
                JSONObject ticker = head.getJSONObject("ticker");
                Double avg = ticker.getDouble("avg");
                String euros = String.format("%.4f", avg);
                // Fix things like 3,1250
                euros = euros.replace(",", ".");
                rates.put(currencyCode,
                        new ExchangeRate(currencyCode, Utils.toNanoCoins(euros), URL.getHost()));
                if (currencyCode.equalsIgnoreCase("BTC"))
                    btcRate = avg;
            } finally {
                if (reader != null)
                    reader.close();
            }
        }
        // Handle ELSM/EUR special since we have to do maths
        final URL URL = new URL("https://btc-e.com/api/2/btc_eur/ticker");
        final URLConnection connection = URL.openConnection();
        connection.setConnectTimeout(TIMEOUT_MS);
        connection.setReadTimeout(TIMEOUT_MS);
        connection.connect();
        final StringBuilder content = new StringBuilder();

        Reader reader = null;
        try {
            reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024));
            IOUtils.copy(reader, content);
            final JSONObject head = new JSONObject(content.toString());
            JSONObject ticker = head.getJSONObject("ticker");
            Double avg = ticker.getDouble("avg");
            // This is elysiums priced in euros.  We want ELSM!
            avg *= btcRate;
            String s_avg = String.format("%.4f", avg).replace(',', '.');
            rates.put("EUR", new ExchangeRate("EUR", Utils.toNanoCoins(s_avg), URL.getHost()));
        } finally {
            if (reader != null)
                reader.close();
        }
        return rates;
    } catch (final IOException x) {
        x.printStackTrace();
    } catch (final JSONException x) {
        x.printStackTrace();
    }

    return null;
}

From source file:edu.cornell.mannlib.vitro.webapp.config.RevisionInfoSetup.java

private void closeReader(Reader reader) {
    if (reader != null) {
        try {//  w  w  w  . ja  v a  2  s  .  c o  m
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:name.martingeisse.webide.features.java.compiler.classpath.ReadOnlyRegularFileObject.java

@Override
public CharSequence getCharContent(final boolean ignoreEncodingErrors) throws IOException {
    final Reader r = openReader(ignoreEncodingErrors);
    final String content = IOUtils.toString(r);
    r.close();
    return content;
}

From source file:name.martingeisse.webide.features.java.compiler.classpath.JarFileObject.java

@Override
public CharSequence getCharContent(final boolean ignoreEncodingErrors) throws IOException {
    Reader r = openReader(ignoreEncodingErrors);
    String content = IOUtils.toString(r);
    r.close();
    return content;
}

From source file:com.cloudera.cdk.morphline.metrics.servlets.HttpMetricsMorphlineTest.java

private String httpGet(int port, String path) throws IOException {
    URL url = new URL("http://localhost:" + port + path);
    URLConnection conn = url.openConnection();
    Reader reader = new InputStreamReader(conn.getInputStream());
    String response = CharStreams.toString(reader);
    reader.close();
    return response;
}

From source file:co.id.app.sys.util.StringUtils.java

/**
 * Close the given reader and ignore any exceptions thrown.
 *
 * @param reader the reader to close./*from   w w  w.j a v a2s  .co  m*/
 */
public static void close(Reader reader) {
    if (reader != null) {
        try {
            reader.close();
        } catch (IOException ioe) {
            // Ignore
        }
    }
}

From source file:UnpackedJarFile.java

public static void close(Reader thing) {
    if (thing != null) {
        try {//  ww  w .  j ava  2 s .co  m
            thing.close();
        } catch (Exception ignored) {
        }
    }
}

From source file:com.bstek.dorado.core.xml.XercesXmlDocumentBuilder.java

public Document loadDocument(Resource resource, String charset) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("Loading XML from " + resource);
    }/*from   www  . jav  a  2  s  .com*/

    if (StringUtils.isEmpty(charset)) {
        charset = Constants.DEFAULT_CHARSET;
    }

    InputStream in = resource.getInputStream();
    Reader reader = new InputStreamReader(in, charset);
    try {
        return getDocumentBuilder().parse(new InputSource(reader));
    } finally {
        reader.close();
        in.close();
    }
}

From source file:com.google.mr4c.content.AbstractContentFactory.java

public void readContent(URI uri, Writer writer) throws IOException {
    Reader reader = readContentAsReader(uri);
    try {/*  w  ww .ja v a2s .  c  o m*/
        IOUtils.copy(reader, writer);
    } finally {
        reader.close();
    }
}

From source file:com.cannabiscoin.wallet.ExchangeRatesProvider.java

private static Object getCoinValueBTC_BTER() {
    Date date = new Date();
    long now = date.getTime();

    //final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();
    // Keep the LTC rate around for a bit
    Double btcRate = 0.0;//  w w w .  j  a  v  a2 s.  c om
    String currency = CoinDefinition.cryptsyMarketCurrency;
    String url = "https://bittrex.com/api/v1.1/public/getticker?market=BTC-CANN";

    HttpURLConnection connection = null;
    try {
        // final String currencyCode = currencies[i];
        final URL URL_bter = new URL(url);
        connection = (HttpURLConnection) URL_bter.openConnection();
        connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS * 2);
        connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS * 2);
        connection.connect();

        final StringBuilder content = new StringBuilder();

        Reader reader = null;
        try {
            reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024));
            Io.copy(reader, content);
            final JSONObject head = new JSONObject(content.toString());
            String result = head.getString("result");
            if (result.equals("true")) {

                Double averageTrade = head.getDouble("Bid");

                if (currency.equalsIgnoreCase("BTC"))
                    btcRate = averageTrade;
            }

        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException x) {
                    // swallow
                }
            }
        }
        return btcRate;
    } catch (final IOException x) {
        x.printStackTrace();
    } catch (final JSONException x) {
        x.printStackTrace();
    } finally {
        if (connection != null)
            connection.disconnect();
    }

    return null;
}