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:it.cnr.ilc.ilcioutils.IlcIOUtils.java

/**
 * Read content from URL and creates a list of lines. use this method when
 * the result of the service are accessed via URL
 *
 * @param theUrl the url where the content is
 * @return an ArrayList from the content
 *//*  ww  w.jav  a 2 s .  c  o  m*/
public static ArrayList<String> readStreamAndCreateAListOfStringFromUrl(String theUrl) {

    ArrayList<String> lines = new ArrayList<>();
    String message;
    String encoding = "UTF-8";

    BufferedReader in = null;
    try {
        URL input = new URL(theUrl);
        in = new BufferedReader(new InputStreamReader(input.openStream(), encoding));
        if (in != null) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                lines.add(inputLine);
            }
        }

    } catch (Exception e) {
        message = String.format("IOException in reading the stream from the url %s %s" + theUrl,
                e.getMessage());
        Logger.getLogger(CLASS_NAME).log(Level.SEVERE, message);
    }
    IOUtils.closeQuietly(in);

    return lines;
}

From source file:net.ftb.util.AppUtils.java

/**
 * Downloads data from the given URL and returns it as a Document
 * @param url the URL to fetch//w  w w . j  av a  2 s .com
 * @return The document
 * @throws IOException, SAXException if an error occurs when reading from the stream
 */
public static Document downloadXML(URL url) throws IOException, SAXException {
    return getXML(url.openStream());
}

From source file:Main.java

public static Document getXMLDOM(URL url) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = dbf.newDocumentBuilder();

    return db.parse(url.openStream());
}

From source file:com.marklogic.tableauextract.ExtractFromJSON.java

/**
 * Read JSON output from MarkLogic REST extension or *.xqy file and insert
 * the output into a Tabeleau table/*  ww w.  j  a v a 2  s.  c  om*/
 * 
 * @param table
 * @throws TableauException
 */
private static void insertData(Table table)
        throws TableauException, FileNotFoundException, ParseException, IOException {
    TableDefinition tableDef = table.getTableDefinition();
    Row row = new Row(tableDef);

    URL url = new URL("http://localhost:8060/json.xqy");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
    JSONParser jsonParser = new JSONParser();
    JSONObject jsonObject = (JSONObject) jsonParser.parse(bufferedReader);

    JSONArray claims = (JSONArray) jsonObject.get("claims");

    @SuppressWarnings("unchecked")
    Iterator<JSONObject> i = claims.iterator();

    while (i.hasNext()) {
        JSONObject innerObject = i.next();

        String idString = (String) innerObject.get("id");
        row.setInteger(0, Integer.parseInt(idString.substring(0, 6)));
        row.setCharString(1, (String) innerObject.get("ssn"));
        row.setCharString(2, (String) innerObject.get("type"));
        String payString = (String) innerObject.get("payment_amount");
        if (payString == null || payString.isEmpty())
            payString = "0.0";
        row.setDouble(3, Double.parseDouble(payString));
        String dtString = (String) innerObject.get("claim_date");
        if (dtString == null || dtString.isEmpty())
            dtString = "1999-01-01";
        LocalDate claimDate = (LocalDate.parse(dtString));
        row.setDate(4, claimDate.getYear(), claimDate.getMonthValue(), claimDate.getDayOfMonth());
        table.insert(row);

    }

    /*
    row.setDateTime(  0, 2012, 7, 3, 11, 40, 12, 4550); // Purchased
    row.setCharString(1, "Beans");                      // Product
    row.setString(    2, "uniBeans");                   // uProduct
    row.setDouble(    3, 1.08);                         // Price
    row.setDate(      6, 2029, 1, 1);                   // Expiration date
    row.setCharString(7, "Bohnen");                     // Produkt
            
    for ( int i = 0; i < 10; ++i ) {
    row.setInteger(4, i * 10);                      // Quantity
    row.setBoolean(5, i % 2 == 1);                  // Taxed
    }
    */
}

From source file:net.redstonelamp.gui.RedstoneLampGUI.java

private static void installCallback(JFrame frame, File selected) {
    JDialog dialog = new JDialog(frame);
    JLabel status = new JLabel("Downloading build...");
    JProgressBar progress = new JProgressBar();
    dialog.pack();/*w ww.ja v  a 2 s . c om*/
    dialog.setVisible(true);
    try {
        URL url = new URL("http://download.redstonelamp.net/?file=LatestBuild");
        InputStream is = url.openStream();
        int size = is.available();
        progress.setMinimum(0);
        progress.setMaximum(size);
        int size2 = size;
        OutputStream os = new FileOutputStream(new File(selected, "RedstoneLamp.jar"));
        while (size2 > 0) {
            int length = Math.min(4096, size2);
            byte[] buffer = new byte[length];
            size2 -= length;
            is.read(buffer);
            progress.setValue(size2);
            os.write(buffer);
        }
        is.close();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.sf.jabref.exporter.OpenDocumentSpreadsheetCreator.java

private static void addFromResource(String resource, OutputStream out) {
    URL url = OpenDocumentSpreadsheetCreator.class.getResource(resource);
    try (InputStream in = url.openStream()) {
        byte[] buffer = new byte[256];
        synchronized (out) {
            while (true) {
                int bytesRead = in.read(buffer);
                if (bytesRead == -1) {
                    break;
                }/*from  www  .ja va  2 s. c  om*/
                out.write(buffer, 0, bytesRead);
            }
        }
    } catch (IOException e) {
        LOGGER.warn("Cannot get resource", e);
    }
}

From source file:be.fedict.eid.applet.service.signer.odf.ODFSignatureVerifier.java

/**
 * Checks whether the document available on the given URL is an ODF document
 * or not.//from ww  w  . j  av a  2s .co m
 *
 * @param url
 * @return
 * @throws IOException
 */
public static boolean isODF(URL url) throws IOException {
    InputStream resStream = ODFUtil.findDataInputStream(url.openStream(), ODFUtil.MIMETYPE_FILE);
    if (null == resStream) {
        /*
         * Some ODF implementations do not include a mimetype file
         * TODO: try harder to check if a file is ODF or not
         */
        LOG.debug("mimetype stream not found in ODF package");
        return false;
    }
    String mimetypeContent = IOUtils.toString(resStream);
    return mimetypeContent.startsWith(ODFUtil.MIMETYPE_START);
}

From source file:Utils.java

/**
 * Unpack an archive from a URL//from w  w  w . ja  v  a  2 s. c  o m
 * 
 * @param url
 * @param targetDir
 * @return the file to the url
 * @throws IOException
 */
public static File unpackArchive(URL url, File targetDir) throws IOException {
    if (!targetDir.exists()) {
        targetDir.mkdirs();
    }
    InputStream in = new BufferedInputStream(url.openStream(), 1024);
    // make sure we get the actual file
    File zip = File.createTempFile("arc", ".zip", targetDir);
    OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
    copyInputStream(in, out);
    out.close();
    return unpackArchive(zip, targetDir);
}

From source file:com.netflix.config.ClasspathPropertiesConfiguration.java

private static Properties loadProperties(URL from) throws IOException {
    Properties into = new Properties();
    InputStream in = from.openStream();
    try {//from ww  w.j ava  2s  .c  om
        into.load(in);
    } catch (Exception e) {
        log.error("Exception while loading properties from URL:" + from);
    } finally {
        in.close();
    }
    return into;
}

From source file:fr.treeptik.cloudunit.utils.TestUtils.java

/**
 * Download from github binaries and deploy file
 *
 * @param path/*from   w w w .  j a  v  a  2s .c om*/
 * @return
 * @throws IOException
 */
public static MockMultipartFile downloadAndPrepareFileToDeploy(String remoteFile, String path)
        throws IOException {
    URL url;
    OutputStream outputStream = null;
    File file = new File(remoteFile);
    try {
        url = new URL(path);
        InputStream input = url.openStream();

        outputStream = new FileOutputStream(file);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = input.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } catch (IOException e) {
        StringBuilder msgError = new StringBuilder(512);
        msgError.append(remoteFile);
        msgError.append(",");
        msgError.append(path);
        logger.debug(msgError.toString(), e);
    } finally {
        outputStream.close();
    }
    return new MockMultipartFile("file", file.getName(), "multipart/form-data", new FileInputStream(file));

}