Example usage for java.net URL openStream

List of usage examples for java.net URL openStream

Introduction

In this page you can find the example usage for java.net URL openStream.

Prototype

public final InputStream openStream() throws java.io.IOException 

Source Link

Document

Opens a connection to this URL and returns an InputStream for reading from that connection.

Usage

From source file:org.stem.ClusterManagerDaemon.java

static URL getConfigUrl() {
    String configPath = System.getProperty(STEM_CONFIG_PROPERTY);
    if (null == configPath)
        configPath = DEFAULT_CONFIG;//w  w w.  ja v a 2  s .  c  o m

    URL url;

    try {
        File file = new File(configPath);
        url = file.toURI().toURL();
        url.openStream().close();
    } catch (Exception e) {
        ClassLoader loader = ClusterManagerDaemon.class.getClassLoader();
        url = loader.getResource(configPath);
        if (null == url)
            throw new RuntimeException("Cannot load " + configPath + ". Ensure \"" + STEM_CONFIG_PROPERTY
                    + "\" system property is set correctly.");
    }

    return url;
}

From source file:com.zack6849.alphabot.api.Utils.java

public static String checkMojangServers() {
    String returns = null;//  w w  w .  j a v  a2  s  . c  om
    try {
        URL url;
        url = new URL("http://status.mojang.com/check");
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
        String result;
        while ((result = reader.readLine()) != null) {
            String a = result.replace("red", Colors.RED + "Offline" + Colors.NORMAL)
                    .replace("green", Colors.GREEN + "Online" + Colors.NORMAL).replace("[", "")
                    .replace("]", "");
            returns = a.replace("{", "").replace("}", "").replace(":", " is currently ").replace("\"", "")
                    .replaceAll(",", ", ");
        }
        reader.close();
    } catch (IOException e) {
        if (e.getMessage().contains("503")) {
            returns = "The minecraft status server is temporarily unavailable, please try again later";
        }
        if (e.getMessage().contains("404")) {
            returns = "Uhoh, it would appear as if the status page has been removed or relocated >_>";
        }

    }
    return returns;
}

From source file:ArchiveUtil.java

/**
 * Copies the contents of the  input url to the output url.
 *///from w  w  w  .j  a  va  2 s. c  o m
public static void copy(URL in, URL out) throws IOException {
    copy(in.openStream(), out.openConnection().getOutputStream());
}

From source file:ArchiveUtil.java

/**
 * Copies the contents of the input url to the output file.
 *//*  w  w w.ja  v  a  2  s  .  c o  m*/
public static void copy(URL in, File out) throws IOException {
    copy(in.openStream(), new FileOutputStream(out));
}

From source file:com.linkedin.pinot.controller.helix.ControllerTest.java

public static String sendGetRequest(String urlString)
        throws UnsupportedEncodingException, IOException, JSONException {
    BufferedReader reader = null;
    final URL url = new URL(urlString);
    reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
    final StringBuilder queryResp = new StringBuilder();
    for (String respLine; (respLine = reader.readLine()) != null;) {
        queryResp.append(respLine);/*from   www.j av a2s  . co  m*/
    }
    return queryResp.toString();
}

From source file:com.net2plan.utils.HTMLUtils.java

/**
 * Converts an XML file to a formatted HTML output via an XSLT definition.
 * //from   w  ww  . j a v  a2 s. c  o  m
 * @param xml String containing an XML file
 * @param xsl URL containing an XSLT definition
 * @return Formatted HTML output
 */
public static String getHTMLFromXML(String xml, URL xsl) {
    try {
        Source xmlDoc = new StreamSource(new StringReader(xml));
        Source xslDoc = new StreamSource(xsl.openStream());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(xslDoc);
        transformer.transform(xmlDoc, new StreamResult(baos));
        String html = baos.toString(StandardCharsets.UTF_8.name());
        html = prepareImagePath(html, xsl);
        return html;
    } catch (IOException | TransformerFactoryConfigurationError | TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * @param url/*from   w  w w. j  a  v  a2 s  .c  o  m*/
 * @param sampleSize
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
public static Bitmap downsampleBitmap(URL url, int sampleSize) throws FileNotFoundException, IOException {
    Bitmap resizedBitmap;
    BitmapFactory.Options outBitmap = new BitmapFactory.Options();
    outBitmap.inJustDecodeBounds = false; // the decoder will return a bitmap
    outBitmap.inSampleSize = sampleSize;

    InputStream is = url.openStream();
    resizedBitmap = BitmapFactory.decodeStream(is, null, outBitmap);
    is.close();

    return resizedBitmap;
}

From source file:com.nwn.NwnFileHandler.java

/**
 * Downloads file from given url//from ww  w.  jav a  2 s. c o  m
 * @param fileUrl String of url to download
 * @param dest Location on system where file should be downloaded
 * @return True if download success, False if download failed
 */
public static boolean downloadFile(String fileUrl, String dest) {
    try {
        URL url = new URL(fileUrl);
        BufferedInputStream bis = new BufferedInputStream(url.openStream());
        FileOutputStream fis = new FileOutputStream(dest);
        String fileSizeString = url.openConnection().getHeaderField("Content-Length");
        double fileSize = Double.parseDouble(fileSizeString);
        byte[] buffer = new byte[1024];
        int count;
        double bytesDownloaded = 0.0;
        while ((count = bis.read(buffer, 0, 1024)) != -1) {
            bytesDownloaded += count;
            fis.write(buffer, 0, count);

            int downloadStatus = (int) ((bytesDownloaded / fileSize) * 100);

            System.out.println("Downloading " + fileUrl + " to " + dest + " " + downloadStatus + "%");
        }
        fis.close();
        bis.close();
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
        return false;
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        return false;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }

    return true;
}

From source file:ArchiveUtil.java

/**
 * Reads the contents of the given file into a byte array.
 *//*from  w  ww  .  j  a v a2s.  c o  m*/
public static byte[] readBytes(URL file) throws IOException {
    return readBytes(file.openStream());
}

From source file:com.asakusafw.testdriver.DirectIoUtil.java

private static <T> DataModelSourceFactory load0(DataModelDefinition<T> definition,
        BinaryStreamFormat<? super T> format, URL source) throws IOException, InterruptedException {
    String path = source.toString();
    try (InputStream stream = source.openStream();
            ModelInput<? super T> input = format.createInput(definition.getModelClass(), path, stream)) {
        return collect(definition, input);
    }/*  ww  w. ja va 2  s .c  om*/
}