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:net.sf.jasperreports.engine.util.JRLoader.java

/**
 * Tries to open an input stream for an URL.
 * //from   w  ww. ja  v a2s  . c om
 * @param spec the string to parse as an URL
 * @return an input stream for the URL or null if <code>spec</code> is not a valid URL
 * @throws JRException
 */
public static InputStream getURLInputStream(String spec) throws JRException {
    InputStream is = null;

    try {
        URL url = new URL(spec);
        is = url.openStream();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_URL_OPEN_ERROR, new Object[] { spec }, e);
    }

    return is;
}

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

private static <T> DataModelSourceFactory load0(DataModelDefinition<T> definition,
        HadoopFileFormat<? super T> format, URL source) throws IOException, InterruptedException {
    List<String> segments = Arrays.stream(source.getPath().split("/")) //$NON-NLS-1$
            .map(String::trim).filter(s -> s.isEmpty() == false).collect(Collectors.toList());
    String name;/*from w ww . j av  a  2s  .c  o m*/
    if (segments.isEmpty()) {
        name = "testing.file"; //$NON-NLS-1$
    } else {
        name = segments.get(segments.size() - 1);
    }
    Path tmpdir = Files.createTempDirectory("asakusa-"); //$NON-NLS-1$
    try (InputStream in = source.openStream()) {
        Path target = tmpdir.resolve(name);
        Files.copy(in, target);
        return load0(definition, format, target.toFile());
    } finally {
        File dir = tmpdir.toFile();
        if (FileUtils.deleteQuietly(dir) == false && dir.exists()) {
            LOG.warn(MessageFormat.format("failed to delete a temporary file: {0}", tmpdir));
        }
    }
}

From source file:com.amazon.janusgraph.example.MarvelGraphFactory.java

public static void load(final JanusGraph graph, final int rowsToLoad, final boolean report) throws Exception {

    JanusGraphManagement mgmt = graph.openManagement();
    if (mgmt.getGraphIndex(CHARACTER) == null) {
        final PropertyKey characterKey = mgmt.makePropertyKey(CHARACTER).dataType(String.class).make();
        mgmt.buildIndex(CHARACTER, Vertex.class).addKey(characterKey).unique().buildCompositeIndex();
    }/*from  ww  w . j  av a2 s  . c  om*/
    if (mgmt.getGraphIndex(COMIC_BOOK) == null) {
        final PropertyKey comicBookKey = mgmt.makePropertyKey(COMIC_BOOK).dataType(String.class).make();
        mgmt.buildIndex(COMIC_BOOK, Vertex.class).addKey(comicBookKey).unique().buildCompositeIndex();
        mgmt.makePropertyKey(WEAPON).dataType(String.class).make();
        mgmt.makeEdgeLabel(APPEARED).multiplicity(Multiplicity.MULTI).make();
    }
    mgmt.commit();

    ClassLoader classLoader = MarvelGraphFactory.class.getClassLoader();
    URL resource = classLoader.getResource("META-INF/marvel.csv");
    int line = 0;
    Map<String, Set<String>> comicToCharacter = new HashMap<>();
    Map<String, Set<String>> characterToComic = new HashMap<>();
    Set<String> characters = new HashSet<>();
    BlockingQueue<Runnable> creationQueue = new LinkedBlockingQueue<>();
    try (CSVReader reader = new CSVReader(new InputStreamReader(resource.openStream()))) {
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null && line < rowsToLoad) {
            line++;
            String comicBook = nextLine[1];
            String[] characterNames = nextLine[0].split("/");
            if (!comicToCharacter.containsKey(comicBook)) {
                comicToCharacter.put(comicBook, new HashSet<String>());
            }
            List<String> comicCharacters = Arrays.asList(characterNames);
            comicToCharacter.get(comicBook).addAll(comicCharacters);
            characters.addAll(comicCharacters);

        }
    }

    for (String character : characters) {
        creationQueue.add(new CharacterCreationCommand(character, graph));
    }

    BlockingQueue<Runnable> appearedQueue = new LinkedBlockingQueue<>();
    for (String comicBook : comicToCharacter.keySet()) {
        creationQueue.add(new ComicBookCreationCommand(comicBook, graph));
        Set<String> comicCharacters = comicToCharacter.get(comicBook);
        for (String character : comicCharacters) {
            AppearedCommand lineCommand = new AppearedCommand(graph, new Appeared(character, comicBook));
            appearedQueue.add(lineCommand);
            if (!characterToComic.containsKey(character)) {
                characterToComic.put(character, new HashSet<String>());
            }
            characterToComic.get(character).add(comicBook);
        }
        REGISTRY.histogram("histogram.comic-to-character").update(comicCharacters.size());
    }

    int maxAppearances = 0;
    String maxCharacter = "";
    for (String character : characterToComic.keySet()) {
        Set<String> comicBookSet = characterToComic.get(character);
        int numberOfAppearances = comicBookSet.size();
        REGISTRY.histogram("histogram.character-to-comic").update(numberOfAppearances);
        if (numberOfAppearances > maxAppearances) {
            maxCharacter = character;
            maxAppearances = numberOfAppearances;
        }
    }
    LOG.info("Character {} has most appearances at {}", maxCharacter, maxAppearances);

    ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
    for (int i = 0; i < POOL_SIZE; i++) {
        executor.execute(new BatchCommand(graph, creationQueue));
    }
    executor.shutdown();
    while (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
        LOG.info("Awaiting:" + creationQueue.size());
        if (report) {
            REPORTER.report();
        }
    }

    executor = Executors.newSingleThreadExecutor();
    executor.execute(new BatchCommand(graph, appearedQueue));

    executor.shutdown();
    while (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
        LOG.info("Awaiting:" + appearedQueue.size());
        if (report) {
            REPORTER.report();
        }
    }
    LOG.info("MarvelGraphFactory.load complete");
}

From source file:com.app.mvc.http.ext.AuthSSLProtocolSocketFactory.java

private static KeyStore createKeyStore(final URL url, final String password)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    if (url == null) {
        throw new IllegalArgumentException("Keystore url may not be null");
    }/*from www .  j  a  v a 2 s  .c  o  m*/
    log.debug("Initializing key store");
    KeyStore keystore = KeyStore.getInstance("jks");
    InputStream is = null;
    try {
        is = url.openStream();
        keystore.load(is, password != null ? password.toCharArray() : null);
    } finally {
        if (is != null)
            is.close();
    }
    return keystore;
}

From source file:com.marklogic.xcc.ContentFactory.java

/**
 * Create a new, non-rewindable {@link Content} object from a {@link URL}.
 * // www. java2s  . c o m
 * @param uri
 *            The URI (name) with which the document will be inserted into the content store. If
 *            the URI already exists in the store, it will be replaced with the new content.
 * @param documentUrl
 *            A {@link URL} object that represents the location from which the content can be
 *            fetched.
 * @param createOptions
 *            Creation meta-information to be applied when the content is inserted into the
 *            contentbase. These options control the document format (json, xml, text, binary) and
 *            access permissions.
 * @return A {@link Content} object suitable for passing to
 *         {@link Session#insertContent(Content)}
 * @throws IOException
 *             If there is a problem creating a {@link URL} or opening a data stream.
 */
public static Content newUnBufferedContent(String uri, URL documentUrl, ContentCreateOptions createOptions)
        throws IOException {
    return new InputStreamContent(uri, documentUrl.openStream(), createOptions);
}

From source file:gate.corpora.CSVImporter.java

/**
 * Create a new document from each row and push it into the specified corpus
 * //from  w w  w .j  ava2s  . co m
 * @param corpus
 *          the Corpus to add documents to
 * @param csv
 *          the URL of the CSV file to processes
 * @param column
 *          the (zero index based) column which contains the text content
 * @param colLabels
 *          true if the first row contains column labels, true otherwise
 * @param separator
 *          the character that is used to separate columns (usually ,)
 * @param quote
 *          the character used to quote data that includes the column
 *          separator (usually ")
 */
public static void populate(Corpus corpus, URL csv, String encoding, int column, boolean colLabels,
        char separator, char quote) {
    CSVReader reader = null;
    try {
        // open a CSVReader over the URL
        reader = new CSVReader(new InputStreamReader(csv.openStream(), encoding), separator, quote);

        // if we are adding features read the first line
        String[] features = (colLabels ? reader.readNext() : null);

        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            // for each line in the file...

            // skip the line if there are less columns than we need to get to the
            // content
            if (column >= nextLine.length)
                continue;

            // skip the line if the column with the content is empty
            if (nextLine[column].trim().equals(""))
                continue;

            FeatureMap fmap = Factory.newFeatureMap();
            if (colLabels) {
                // copy all the features from the row into a FeatureMap using the
                // labels from the first line
                for (int i = 0; i < features.length; ++i) {
                    if (i != column && i < nextLine.length) {
                        fmap.put(features[i], nextLine[i]);
                    }
                }
            }

            // setup the initialization params for the document
            FeatureMap params = Factory.newFeatureMap();
            params.put(Document.DOCUMENT_STRING_CONTENT_PARAMETER_NAME, nextLine[column]);

            // create the document
            Document doc = (Document) Factory.createResource(gate.corpora.DocumentImpl.class.getName(), params,
                    fmap);

            // add the document to the corpus
            corpus.add(doc);

            if (corpus.getLRPersistenceId() != null) {
                // persistent corpus -> unload the document
                corpus.unloadDocument(doc);
                Factory.deleteResource(doc);
            }

        }

        if (corpus.getDataStore() != null) {
            // if this corpus is in a datastore make sure we sync it back
            corpus.getDataStore().sync(corpus);
        }
    } catch (Exception e) {
        // not much we can do other than report the exception
        throw new RuntimeException("Unable to open CSV file: " + csv, e);
    } finally {
        // if we opened the reader successfully then close it so we don't leak
        // file handles
        if (reader != null)
            IOUtils.closeQuietly(reader);
    }
}

From source file:com.data.pack.Util.java

public static String readversion() {
    String str = "";
    try {/*from  ww w  . j  a va2s . co m*/
        // Create a URL for the desired page
        URL url = new URL("http://fitness4.me/versioninfo.txt");

        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

        String line;
        while ((line = in.readLine()) != null) {
            str = str + line + "\n";
        }
        in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    return str;
}

From source file:edu.cornell.mannlib.semservices.util.SKOSUtils.java

public static String getConceptXML(String conceptUriString) {
    URL conceptURL = null;
    try {//  w  w w.ja  v  a2s .c  o m
        conceptURL = new URL(conceptUriString);
    } catch (Exception e) {
        log.error("Exception occurred in instantiating URL for " + conceptUriString, e);
        // If the url is having trouble, just return null for the concept
        return null;
    }
    log.debug("loading concept uri " + conceptUriString);

    String results = null;
    try {

        StringWriter sw = new StringWriter();

        BufferedReader in = new BufferedReader(new InputStreamReader(conceptURL.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            sw.write(inputLine);
        }
        in.close();

        results = sw.toString();
        log.debug(results);
    } catch (Exception ex) {
        log.error("Error occurred in getting concept from the URL " + conceptUriString, ex);
        return null;
    }
    return results;
}

From source file:be.fedict.eid.applet.service.signer.ooxml.OOXMLSignatureVerifier.java

/**
 * Checks whether the file referred by the given URL is an OOXML document.
 * /*from   w  w w. j a  v a2  s .c o  m*/
 * @param url
 * @return
 * @throws IOException
 */
public static boolean isOOXML(URL url) throws IOException {
    ZipInputStream zipInputStream = new ZipInputStream(url.openStream());
    ZipEntry zipEntry;
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (false == "[Content_Types].xml".equals(zipEntry.getName())) {
            continue;
        }
        return true;
    }
    return false;
}