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:com.blacklocus.jres.Jres.java

public static <T> T load(URL script, Class<T> klass) {
    try {//  w w  w.  j a va 2  s  .c  om
        // Don't use ObjectMapper.readValue(URL, ?), doesn't seem to be able to find local resources.
        return ObjectMappers.fromJson(script.openStream(), klass);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.gerrit.server.documentation.MarkdownFormatter.java

private static String readPegdownCss(AtomicBoolean file) throws IOException {
    String name = "pegdown.css";
    URL url = MarkdownFormatter.class.getResource(name);
    if (url == null) {
        throw new FileNotFoundException("Resource " + name);
    }//from  w  w  w  .j  av a  2 s. c o m
    file.set("file".equals(url.getProtocol()));
    try (InputStream in = url.openStream(); TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(128 * 1024)) {
        tmp.copy(in);
        return new String(tmp.toByteArray(), UTF_8);
    }
}

From source file:examples.utils.CifarReader.java

public static void downloadAndExtract() {

    if (new File("data", TEST_DATA_FILE).exists() == false) {
        try {//from  w ww.  j  a va2  s  .c  om
            if (new File("data", ARCHIVE_BINARY_FILE).exists() == false) {
                URL website = new URL("http://www.cs.toronto.edu/~kriz/" + ARCHIVE_BINARY_FILE);
                FileOutputStream fos = new FileOutputStream("data/" + ARCHIVE_BINARY_FILE);
                fos.getChannel().transferFrom(Channels.newChannel(website.openStream()), 0, Long.MAX_VALUE);
                fos.close();
            }
            TarArchiveInputStream tar = new TarArchiveInputStream(
                    new GZIPInputStream(new FileInputStream("data/" + ARCHIVE_BINARY_FILE)));
            TarArchiveEntry entry = null;
            while ((entry = tar.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    new File("data", entry.getName()).mkdirs();
                } else {
                    byte data[] = new byte[2048];
                    int count;
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(new File("data/", entry.getName())), 2048);

                    while ((count = tar.read(data, 0, 2048)) != -1) {
                        bos.write(data, 0, count);
                    }
                    bos.close();
                }
            }
            tar.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:io.apiman.test.common.util.TestUtil.java

/**
 * Loads a rest test from a classpath resource.
 * @param resourcePath/*  w w w. j a  va  2 s . co m*/
 * @param cl
 */
public static final RestTest loadRestTest(String resourcePath, ClassLoader cl) {
    InputStream is = null;
    try {
        URL url = cl.getResource(resourcePath);
        if (url == null)
            throw new RuntimeException("Rest Test not found: " + resourcePath); //$NON-NLS-1$
        is = url.openStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        return parseRestTest(reader);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.offbynull.coroutines.instrumenter.testhelpers.TestUtils.java

/**
 * Loads up a resource from the classpath as a byte array.
 * @param path path of resource//from w  ww.j  a  v a  2  s  .  co  m
 * @return contents of resource
 * @throws IOException if any IO error occurs
 * @throws IllegalArgumentException if {@code path} cannot be found
 */
public static byte[] getResource(String path) throws IOException {
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    URL url = cl.getResource(path);
    Validate.isTrue(url != null);
    try (InputStream in = url.openStream()) {
        return IOUtils.toByteArray(in);
    }
}

From source file:com.nesscomputing.tinyhttp.ssl.HttpsTrustManagerFactory.java

@Nonnull
private static KeyStore loadKeystore(@Nonnull String location, @Nonnull String keystoreType,
        @Nonnull String keystorePassword) throws GeneralSecurityException, IOException {
    final KeyStore keystore = KeyStore.getInstance(keystoreType);
    URL keystoreUrl;
    if (StringUtils.startsWithIgnoreCase(location, "classpath:")) {
        keystoreUrl = Resources.getResource(HttpsTrustManagerFactory.class, location.substring(10));
    } else {//from w  w  w  . j a v a 2  s .co  m
        keystoreUrl = new URL(location);
    }
    keystore.load(keystoreUrl.openStream(), keystorePassword.toCharArray());
    return keystore;
}

From source file:edu.duke.cabig.c3pr.utils.DaoTestCase.java

/**
 * For Oracle typically it is "C3PR_DEV" (note- upper case). For Postgres - "public". Dont
 * forget to override this depending on your database
 * //from w ww  . j  a v  a  2 s .  c  o  m
 * @return
 */
protected static String getSchema() {
    URL url = ClassLoader.getSystemResource("context/datasource.properties");
    Properties p = new Properties();
    try {
        //p.load(new FileInputStream(new File(url.getFile())));
        p.load(url.openStream());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return p.getProperty("datasource.testschema");
}

From source file:eu.openanalytics.rsb.config.ConfigurationFactory.java

static PersistedConfigurationAdapter load(final URL configurationUrl) throws IOException {
    Validate.notNull(configurationUrl, "Impossible to find " + configurationUrl);

    final InputStream is = configurationUrl.openStream();
    return loadConfigurationStream(configurationUrl, is);
}

From source file:Main.java

public static void writeUrlToFile(String urlToRead, String folderToWrite, String fileName)
        throws MalformedURLException, IOException {
    URL urlIn = new URL(urlToRead);
    File folderOut = new File(folderToWrite);
    if (!(folderOut.exists() || folderOut.mkdirs())) {
        throw new RuntimeException("could not create folder " + folderToWrite);
    }//from w  w w .  j a va2 s .c o  m
    File fileOut = new File(folderOut, fileName);
    try (InputStream in = new BufferedInputStream(urlIn.openStream());
            OutputStream out = new BufferedOutputStream(new FileOutputStream(fileOut));) {
        transfer(in, out);
    }
}

From source file:com.chiralBehaviors.autoconfigure.AutoConfigureService.java

/**
 * @param configurationResource/*from  w w  w .ja  va  2 s  .co m*/
 * @return
 * @throws IOException
 * @throws JsonMappingException
 * @throws JsonParseException
 */
public static Configuration configurationFrom(String configurationResource)
        throws JsonParseException, JsonMappingException, IOException {
    File configFile = new File(configurationResource);
    if (configFile.exists()) {
        return YamlHelper.fromYaml(configFile);
    }
    URL url = getURL(configurationResource);
    if (url == null) {
        throw new IllegalArgumentException(
                String.format("No such configuration resource: %s", configurationResource));
    }
    return YamlHelper.fromYaml(url.openStream());
}