Example usage for java.net URLConnection getInputStream

List of usage examples for java.net URLConnection getInputStream

Introduction

In this page you can find the example usage for java.net URLConnection getInputStream.

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:com.janoz.usenet.suppliers.impl.UrlBasedSupplier.java

@Override
public byte[] getData(NZB nzb) {
    if (nzb.getUrl() == null) {
        throw new LazyInitializationException("Unable to fetch data. URL missing.");
    }//from www  .  j  ava 2s.  co m
    if (throttler != null) {
        throttler.throttle();
    }
    InputStream is = null;
    try {
        URLConnection connection = new URL(nzb.getUrl()).openConnection();
        is = connection.getInputStream();
        int contentLength = connection.getContentLength();
        ByteArrayOutputStream baos;
        if (contentLength != -1) {
            baos = new ByteArrayOutputStream(contentLength);
        } else {
            baos = new ByteArrayOutputStream(16384); // Pick some appropriate size
        }

        byte[] buf = new byte[512];
        while (true) {
            int len = is.read(buf);
            if (len == -1) {
                break;
            }
            baos.write(buf, 0, len);
        }
        return baos.toByteArray();
    } catch (MalformedURLException e) {
        LOG.error("Error in URL.", e);
        throw new LazyInitializationException("Error fetching NZB with URL  " + nzb.getUrl() + ".", e);
    } catch (IOException e) {
        LOG.error("IO error fetching lazy data.", e);
        throw new LazyInitializationException("IO error fetching NZB with URL  " + nzb.getUrl() + ".", e);
    } finally {
        if (throttler != null) {
            throttler.setThrottleForNextAction();
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                LOG.info("error closing stream.", e);
            }
        }
    }
}

From source file:com.baeldung.file.FileOperationsUnitTest.java

@Test
public void givenURLName_whenUsingURL_thenFileData() throws IOException {
    String expectedData = "Example Domain";

    URL urlObject = new URL("http://www.example.com/");

    URLConnection urlConnection = urlObject.openConnection();

    InputStream inputStream = urlConnection.getInputStream();
    String data = readFromInputStream(inputStream);

    Assert.assertThat(data.trim(), CoreMatchers.containsString(expectedData));
}

From source file:com.fluidops.iwb.provider.GoogleRefineProvider.java

@Override
public void gather(List<Statement> res) throws Exception {
    Map<String, String> parameter = new HashMap<String, String>();
    parameter.put("engine", "{\"facets\":[],\"mode\":\"row-based\"}");
    parameter.put("project", config.project);
    parameter.put("format", "rdf");

    URL url = new URL(config.url);
    URLConnection con = doPost(url, parameter);

    InputStream is = con.getInputStream();
    res.addAll(/*from  ww w.  j  a v a  2  s.co m*/
            readStatements(is, EndpointImpl.api().getNamespaceService().defaultNamespace(), RDFFormat.RDFXML));
}

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();//from   w ww . j  av  a2s .  c  o  m
    return response;
}

From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java

private static Double getGoldCoinValueLTC() {
    Date date = new Date();
    long now = date.getTime();

    if ((now < (lastLitecoinValueCheck + 10 * 60)) && lastLitecoinValue > 0.0)
        return lastLitecoinValue;

    //final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();
    // Keep the LTC rate around for a bit
    Double ltcRate = 0.0;//from ww w.  j av a2  s  .  c  o  m
    String currencyCryptsy = "LTC";
    String urlCryptsy = "http://pubapi.cryptsy.com/api.php?method=marketdata";

    try {
        // final String currencyCode = currencies[i];
        final URL URLCryptsy = new URL(urlCryptsy);
        final URLConnection connectionCryptsy = URLCryptsy.openConnection();
        connectionCryptsy.setConnectTimeout(TIMEOUT_MS);
        connectionCryptsy.setReadTimeout(TIMEOUT_MS);
        connectionCryptsy.connect();

        final StringBuilder contentCryptsy = new StringBuilder();

        Reader reader = null;
        try {
            reader = new InputStreamReader(new BufferedInputStream(connectionCryptsy.getInputStream(), 1024));
            IOUtils.copy(reader, contentCryptsy);
            final JSONObject head = new JSONObject(contentCryptsy.toString());
            JSONObject returnObject = head.getJSONObject("return");
            JSONObject markets = returnObject.getJSONObject("markets");
            JSONObject GLD = markets.getJSONObject("GLD");

            Double lastTrade = GLD.getDouble("lasttradeprice");

            //String euros = String.format("%.7f", lastTrade);
            // Fix things like 3,1250
            //euros = euros.replace(",", ".");
            //rates.put(currencyCryptsy, new ExchangeRate(currencyCryptsy, Utils.toNanoCoins(euros), URLCryptsy.getHost()));
            if (currencyCryptsy.equalsIgnoreCase("LTC"))
                ltcRate = lastTrade;

            lastLitecoinValue = ltcRate;
            lastLitecoinValueCheck = now;
        } finally {
            if (reader != null)
                reader.close();
        }
        return ltcRate;
    } catch (final IOException x) {
        x.printStackTrace();
    } catch (final JSONException x) {
        x.printStackTrace();
    }

    return null;
}

From source file:br.com.esign.google.geocode.GoogleGeocode.java

public String getJsonString() throws IOException {
    StringBuilder httpUrl = new StringBuilder("http://maps.googleapis.com/maps/api/geocode/json?sensor=false&");
    if (languageCode != null && !languageCode.isEmpty()) {
        httpUrl.append("language=").append(languageCode).append("&");
    }//  w ww .j ava 2  s.c o  m
    if (reverse) {
        httpUrl.append("latlng=").append(lat).append(",").append(lng);
    } else {
        httpUrl.append("address=").append(URLEncoder.encode(address, "utf-8"));
    }

    URL url = new URL(httpUrl.toString());
    URLConnection conn = url.openConnection();

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
    StringBuilder out = new StringBuilder();
    String line;
    while ((line = in.readLine()) != null) {
        out.append(line);
    }
    return out.toString();
}

From source file:org.jrecruiter.service.impl.DataServiceImpl.java

/** {@inheritDoc} */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Image getGoogleMapImage(final BigDecimal latitude, final BigDecimal longitude, final Integer zoomLevel) {

    if (longitude == null) {
        throw new IllegalArgumentException("Longitude cannot be null.");
    }/* w ww.j  a va  2  s.  c o m*/

    if (latitude == null) {
        throw new IllegalArgumentException("Latitude cannot be null.");
    }

    if (zoomLevel == null) {
        throw new IllegalArgumentException("ZoomLevel cannot be null.");
    }

    final URI url = GoogleMapsUtils.buildGoogleMapsStaticUrl(latitude, longitude, zoomLevel);

    BufferedImage img;
    try {
        URLConnection conn = url.toURL().openConnection();
        img = ImageIO.read(conn.getInputStream());
    } catch (UnknownHostException e) {
        LOGGER.error("Google static MAPS web service is not reachable (UnknownHostException).", e);
        img = new BufferedImage(GoogleMapsUtils.defaultWidth, 100, BufferedImage.TYPE_INT_RGB);

        final Graphics2D graphics = img.createGraphics();

        final Map<Object, Object> renderingHints = CollectionUtils.getHashMap();
        renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.addRenderingHints(renderingHints);
        graphics.setBackground(Color.WHITE);
        graphics.setColor(Color.GRAY);
        graphics.clearRect(0, 0, GoogleMapsUtils.defaultWidth, 100);

        graphics.drawString("Not Available", 30, 30);

    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    return img;
}

From source file:com.baeldung.file.FileOperationsTest.java

@Test
public void givenURLName_whenUsingURL_thenFileData() throws IOException {
    String expectedData = "Baeldung";

    URL urlObject = new URL("http://www.baeldung.com/");

    URLConnection urlConnection = urlObject.openConnection();

    InputStream inputStream = urlConnection.getInputStream();
    String data = readFromInputStream(inputStream);

    Assert.assertThat(data.trim(), CoreMatchers.containsString(expectedData));
}

From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java

private static Map<String, ExchangeRate> getLitecoinChartsOld() {
    final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();
    // Keep the BTC rate around for a bit
    Double ltcRate = getGoldCoinValueLTC();

    Double btcRate = 0.0;//from   w  w w. j  av a2 s. c  o 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/ltc_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") * ltcRate;
                String euros = String.format("%.7f", 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 LTC/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 bitcoins priced in euros.  We want GLD!
            avg *= btcRate;
            String s_avg = String.format("%.8f", avg).replace(',', '.');
            rates.put("EUR", new ExchangeRate("EUR", Utils.toNanoCoins(s_avg), URL.getHost()));

            //Add LTC information
            s_avg = String.format("%.8f", ltcRate);
            rates.put("LTC",
                    new ExchangeRate("LTC", Utils.toNanoCoins(s_avg.replace(",", ".")), 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:com.baeldung.file.FileOperationsManualTest.java

@Test
public void givenURLName_whenUsingURL_thenFileData() throws IOException {
    String expectedData = "Example Domain";

    URL urlObject = new URL("http://www.example.com/");

    URLConnection urlConnection = urlObject.openConnection();

    InputStream inputStream = urlConnection.getInputStream();
    String data = readFromInputStream(inputStream);

    assertThat(data.trim(), CoreMatchers.containsString(expectedData));
}