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:adalid.util.info.JavaInfo.java

public static void printManifestInfo(String extension, boolean details, URL url) throws IOException {
    InputStream stream = url.openStream();
    if (stream == null) {
        return;//from  www .  j ava  2  s  .com
    }
    //      String file = url.getFile();
    String path = StringUtils.removeEndIgnoreCase(url.getPath(), "!/" + JarFile.MANIFEST_NAME);
    String name = StringUtils.substringAfterLast(path, "/");
    Manifest manifest = new Manifest(stream);
    if (!extensionNameMatches(manifest, extension)) {
        return;
    }
    //      out.println(file);
    printManifestInfo(path, name, details, manifest);
}

From source file:com.jts.main.helper.Http.java

public static String get(String urlString) {
    BufferedReader reader = null;
    try {/*from w ww  . j a va  2  s .co  m*/
        URL url = new URL(urlString);
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuilder buffer = new StringBuilder();
        int read;
        char[] chars = new char[1024];
        while ((read = reader.read(chars)) != -1) {
            buffer.append(chars, 0, read);
        }
        return buffer.toString();
    } catch (MalformedURLException ex) {
        errMsg = ex.getMessage();
    } catch (IOException ex) {
        errMsg = ex.getMessage();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                errMsg = ex.getMessage();
            }
        }
    }
    return "";
}

From source file:com.cedarsoft.serialization.test.utils.AbstractJsonVersionTest2.java

@Nonnull
protected static VersionEntry create(@Nonnull Version version, @Nonnull URL expected) {
    try {//w ww .  j av  a 2 s.  c om
        return new JsonVersionEntry(version, IOUtils.toByteArray(expected.openStream()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:architecture.common.util.ClassUtils.java

public static InputStream getResourceAsStream(String resourceName, Class callingClass) {
    URL url = getResource(resourceName, callingClass);
    try {/*from  ww w .j  av a  2  s.  c o m*/
        return url == null ? null : url.openStream();
    } catch (IOException e) {
        return null;
    }
}

From source file:javacommon.excel.ExcelUtils.java

/**
 * ?Excel//from w  ww. java2  s . c  om
 * @param type excel0:xls,1:xlsx
 * @param url
 * @param handler
 * @throws IOException 
 */
public static void read(int type, URL url, Handler handler) throws IOException {
    read(type, url.openStream(), handler);
}

From source file:com.cedarsoft.serialization.test.utils.AbstractSerializerTest2.java

@Nonnull
public static <T> Entry<? extends T> create(@Nonnull T object, @Nonnull URL expected) {
    try {//from w w w  .  j  a  v a  2s  .c  o  m
        return new Entry<T>(object, IOUtils.toByteArray(expected.openStream()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.cornell.med.icb.util.VersionUtils.java

/**
 * Gets the Implementation-Version attribute from the manifest of the jar file a class
 * is loaded from./* ww w. ja va2s  . co m*/
 * @param clazz The class to get the version for
 * @return The value of the Implementation-Version attribute or "UNKNOWN" if the
 * jar file cannot be read.
 */
public static String getImplementationVersion(final Class<?> clazz) {
    String version;
    try {
        final String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
        final URL manifestUrl = new URL("jar:" + classContainer + "!/" + JarFile.MANIFEST_NAME);
        final Manifest manifest = new Manifest(manifestUrl.openStream());
        final Attributes attributes = manifest.getMainAttributes();
        version = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
    } catch (Exception e) {
        // pretty much any error here is ok since we may not even have a jar to read from
        version = "UNKNOWN";
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug(Attributes.Name.IMPLEMENTATION_VERSION + ": " + version);
    }
    return StringUtils.defaultString(version);
}

From source file:Main.java

/**
 * This API compares if two files content is identical. It ignores extra
 * spaces and new lines while comparing/*from   ww w  .j a v a 2  s.com*/
 *
 * @param sourceFile
 * @param destFile
 * @return
 * @throws Exception
 */
public static boolean isFilesIdentical(URL sourceFile, File destFile) throws Exception {
    try {
        Scanner sourceFileScanner = new Scanner(sourceFile.openStream());
        Scanner destFileScanner = new Scanner(destFile);

        while (sourceFileScanner.hasNext()) {
            /*
             * If source file is having next token then destination file
             * also should have next token, else they are not identical.
             */
            if (!destFileScanner.hasNext()) {
                destFileScanner.close();
                sourceFileScanner.close();
                return false;
            }
            if (!sourceFileScanner.next().equals(destFileScanner.next())) {
                sourceFileScanner.close();
                destFileScanner.close();
                return false;
            }
        }
        /*
         * Handling the case where source file is empty and destination file
         * is having text
         */
        if (destFileScanner.hasNext()) {
            destFileScanner.close();
            sourceFileScanner.close();
            return false;
        } else {
            destFileScanner.close();
            sourceFileScanner.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } /*finally {
      sourceFile.close();
      }*/
}

From source file:es.tekniker.framework.ktek.commons.config.ConfigFile.java

protected static void setFilePath(String defaultFilePath) throws IOException {
    ConfigFile.filePath = defaultFilePath;
    log.debug("Loading properties for file " + filePath);

    ClassLoader loader = ConfigFile.class.getClassLoader();
    if (loader == null)
        loader = ClassLoader.getSystemClassLoader();

    java.net.URL url = loader.getResource(filePath);
    props.load(url.openStream());
    log.debug("Properties loaded");
}

From source file:de.tudarmstadt.ukp.dkpro.tc.api.features.util.FeatureUtil.java

/**
 * @param inputFile Location of the file that contains the stopwords. One stopword per line.
 * @param toLowerCase Whether the stopwords should be converted to lowercase or not.
 * @return A set of stopwords.//from   ww  w. j  a v  a  2s .  c o  m
 * @throws IOException
 */
public static Set<String> getStopwords(String inputFile, boolean toLowerCase) throws IOException {
    Set<String> stopwords = new HashSet<String>();
    if (inputFile != null) {
        URL stopUrl = ResourceUtils.resolveLocation(inputFile, null);
        InputStream is = stopUrl.openStream();
        for (String stopword : IOUtils.readLines(is, "UTF-8")) {
            if (toLowerCase) {
                stopwords.add(stopword.toLowerCase());
            } else {
                stopwords.add(stopword);
            }
        }
    }

    return stopwords;
}